您如何更改目录并在ansible中运行命令?

问题描述 投票:0回答:1

嗨,我在努力理解为什么这段代码说明目录不存在

在系统上,我看到

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

并且可以看到路径存在

但是,在我的ansant代码中,统计信息告诉我该目录不存在代码如下]

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

vars
main.yml

在Developer_project_setup.yml中,它通过main.yml中的include运行

- 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

在vars.yml文件中]

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

我还提供了要检查的统计信息,但令我惊讶的是,它也会返回false

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

我还使用以下内容尝试调试,从我可以看到该文件夹​​的角度来看,结果很有趣

- 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 }}

因此,当我运行剧本时,会得到

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"}

不知道为什么说文件夹不存在,任何人都可以发现我在做什么错吗?

您好,我很努力地理解为什么这段代码说明系统上不存在该目录,我看到root @ Production:〜#ls -l / home / azureuser / directory-with-stuff /,并且可以看到该目录。 ..

ansible ansible-2.x ansible-inventory
1个回答
1
投票

我认为它的语法问题。使用double而不是single。即用'{{'代替'{“

args:                                                                     
    chdir: "{{ file_path_home }}"
© www.soinside.com 2019 - 2024. All rights reserved.