我有ansible剧本,我想查看指定的目录(使用ls命令)。目录位于根目录中,我使用pwd命令检查:
/root/git/ansible/data/example
任务是这样的:
name: open directory
become: yes
become_user: root
command: chdir=/data/example ls
在我执行了剧本之后,我得到了以下错误:
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unable to change directory before execution: [Errno 2] No such file or directory: '/data/example'"}
而这个目录确实存在。我也试过这个:
name: open directory
become: yes
become_user: root
shell:
cmd: ls
chdir: /data/example
也犯了同样的错误。有人能帮忙吗?
发布于 2021-12-15 23:50:29
给这棵树
shell> tree /data/example
/data/example
├── file1
├── file2
└── file3
0 directories, 3 files
所有形式的命令和空壳
- hosts: localhost
tasks:
- command:
chdir: /data/example
cmd: ls
register: result
tags: t1
- shell:
chdir: /data/example
cmd: ls
register: result
tags: t2
- command: chdir=/data/example ls
register: result
tags: t3
- shell: chdir=/data/example ls
register: result
tags: t4
- debug:
var: result.stdout
tags: always
工作按预期进行
result.stdout:
file1
file2
file3
https://stackoverflow.com/questions/70375177
复制