In Ansible, How to read configMap file in local system and remove it from remote node?

1/27/2020

I am having a configMap file in my local system. This configMap is created in kubernetes cluster (which is remote machine). Now i am trying to remove the configMap from remote machine using ansible.

   - hosts: k8s
     vars:
       configmap: "./configmap.yml"
       secret: "./secret.yml"

   - name: uninstall configMap file
     shell: "kubectl delete -f {{ configMap }}" 

The error is as below. Seems it looks for the file in k8s nodes. but the file is there in local machine.

"stderr": "error: the path \"./configmap.yml\" does not exist"

I also tried this .

   - hosts: k8s
     vars:
       configmap: "{{ lookup('file', './configmap.yml') }}"

   - name: get ConfigMap
     shell: "cat {{configmap | from_yaml}} | kubectl delete -f -  "

It says changed as if success but the configmap is not removed

How to remove the config map from remote node?

-- Samselvaprabu
ansible
kubernetes

1 Answer

1/27/2020

The easiest way to understand the issue is checking current directory for the playbook. Compare current directory and directory where you saved the file. They are different. To take current directory:

- shell: "pwd"
  register: result
- debug:
    var: result

It's easier to use absolute path for configmap.yml to create/use files on the host, e.g.:

vars:
  configmap: "/root/configmap.yml"
-- Oligzeev
Source: StackOverflow