Search code examples
ansibleansible-2.xansible-inventory

Ho do you change directory and run a command in ansible?


Hi I am struggling to understand why this code is saying that the directory does not exist

on the system I see

root@Production:~# ls -l /home/azureuser/directory-with-stuff/

and can see that the path exists

however, in my ansible code, the stat tells me that the directory does not exist the code is as follows

Project_add\roles\common\tasks
main.yml
Developer_project_setup.yml

vars
main.yml

in the Developer_project_setup.yml which runs via an include in the main.yml

- name: Running make libs to build libraries
  command: make libs
  args:                                                                     
    chdir: "{ file_path_home }"
  when: infra_problem_dir_check.stat.exists

- name: Running make clean for static tarballls
  command:  make clean all
  args:                                                                     
    chdir: "{ file_path_home }"
  when: infra_problem_dir_check.stat.exists

within the vars.yml file

file_path_home: /home/azureuser/directory-with-stuff

I have also included a stat to check but this also returns false to my astonishment

- stat:
    path: "{ file_path_home }"
  register: infra_problem_dir_check

I have also used the following to try and debug and the results are interesting in the sense that i can see the folder

- name: checking working directory
  command: pwd
  register: test
- debug: msg={{ test.stdout }}

- name: checking working directory contents
  command: ls -l
  register: test
- debug: msg={{ test.stdout }}

so when I run the playbook I get

TASK [Project_add/roles/common : stat] **************************************************************************************************************************************************************************************************************************************************************************************
ok: [client_Host_9d63] => {"changed": false, "stat": {"exists": false}}



TASK [Project_add/roles/common : checking working directory contents] *******************************************************************************************************************************************************************************************************************************************************
changed: [client_Host_9d63] => {"changed": true, "cmd": ["ls", "-l"], "delta": "0:00:00.003163", "end": "2020-03-11 23:00:50.782252", "rc": 0, "start": "2020-03-11 23:00:50.779089", "stderr": "", "stderr_lines": [], "stdout": "total 4\ndrwxr-xr-x 7 azureuser azureuser 4096 Mar  9 20:14 directory-with-stuff", "stdout_lines": ["total 4", "drwxr-xr-x 7 azureuser azureuser 4096 Mar  9 20:14 directory-with-stuff"]}



TASK [Project_add/roles/common : debug] *************************************************************************************************************************************************************************************************************************************************************************************
ok: [client_Host_9d63] => {
    "msg": "total 4\ndrwxr-xr-x 7 azureuser azureuser 4096 Mar  9 20:14 directory-with-stuff"
}


TASK [Project_add/roles/common : Running make libs to build libraries] ******************************************************************************************************************************************************************************************************************************************************
skipping: [client_Host_9d63] => {"changed": false, "skip_reason": "Conditional result was False"}

TASK [Project_add/roles/common : Running make clean for static tarballls] ***************************************************************************************************************************************************************************************************************************************************
skipping: [client_Host_9d63] => {"changed": false, "skip_reason": "Conditional result was False"}

No idea why it says it says the folder doesn't exist, can anyone shed light on what I'm doing wrong?


Solution

  • I think its syntax issue. Use double instead of single.i.e '{{' instead of '{"

    args:                                                                     
        chdir: "{{ file_path_home }}"
    
    - stat:
        path: "{{ file_path_home }}"
      register: infra_problem_dir_check