Home > other >  creating a hidden directory on ansible
creating a hidden directory on ansible

Time:10-01

- name: Creates directory
  become: yes
  become_user: marlon
  become_method: su
  file:
    path: "{{ homedir }}/.kube"
    state: directory
    owner: marlon
    group: wheel
    mode: 0775
    recurse: yes

Got the following task above, however keep getting the following error message:

"msg": "There was an issue creating {'cmd': ['echo', '~'], 'stdout': '/home/marlon', 'stderr': '', 'rc': 0, 'start': '2021-09-30 11:53:54.924421', 'end': '2021-09-30 11:53:54.927095', 'delta': '0:00:00.002674', 'changed': True, 'stdout_lines': [' as requested: [Errno 13] Permission denied: b\"{'cmd': ['echo', '~'], 'stdout': '/home/marlon', 'stderr': '', 'rc': 0, 'start': '2021-09-30 11:53:54.924421', 'end': '2021-09-30 11:53:54.927095', 'delta': '0:00:00.002674', 'changed': True, 'stdout_lines': ['\"",
"path": "{'cmd': ['echo', '~'], 'stdout': '/home/marlon', 'stderr': '', 'rc': 0, 'start': '2021-09-30 11:53:54.924421', 'end': '2021-09-30 11:53:54.927095', 'delta': '0:00:00.002674', 'changed': True, 'stdout_lines': ['/home/marlon'], 'stderr_lines': [], 'failed': False}/.kube"

This is the ansible playbook that I ran:

marlon@ansible:~/.ansible$ ansible-playbook -i inventory/hosts playbooks/settingup_kubernetes_cluster.yaml -e "ansible_sudo_pass:pass" -e "ansible_become_pass=pass" --tags masteronly -vvvv

Tried to scape the hidden directory using a \ but nothing seems to work for me. Any suggestions or ideas? My inventory host files look like this:

[kubernetes-master-nodes]
master.madebeen.com ansible_host=192.168.153.135 ansible_connection=ssh ansible_user=marlon ansible_ssh_pass=test ansible_become_pass=test
[kubernetes-worker-nodes]
node1.madebeen.com ansible_host=192.168.153.136 ansible_connection=ssh ansible_user=marlon ansible_ssh_pass=Tar.Mid.Fun-456 ansible_become_pass=test
node2.madebeen.com ansible_host=192.168.153.137 ansible_connection=ssh ansible_user=marlon ansible_ssh_pass=Tar.Mid.Fun-456 ansible_become_pass=test
[node_and_workers:children]
kubernetes-master-nodes
kubernetes-worker-nodes

CodePudding user response:

As the error message shows you, {{ homedir }} is not a str, it's dict[str, Any]:

"path": "{'cmd': ['echo', '~'], 'stdout': '/home/marlon', 'stderr': '', 'rc': 0, 'start': '2021-09-30 11:53:54.924421', 'end': '2021-09-30 11:53:54.927095', 'delta': '0:00:00.002674', 'changed': True, 'stdout_lines': ['/home/marlon'], 'stderr_lines': [], 'failed': False}/.kube"

What I strongly suspect happened is you had { command: "echo ~", register: homedir } and then just used homedir as if it was the output of echo ~ but that's not the way ansible works

You'll at bare minimum want:

   file:
    path: "{{ homedir.stdout }}/.kube"

and ideally you'd not depend upon running a shell command to find marlon's home directory but rather either {{ ansible_env.HOME }} or read the value out of /etc/passwd on that machine

  • Related