我有 ansible playbook,我想查看指定的目錄(使用 ls 命令)。該目錄在根目錄中,我使用 pwd 命令進行檢查:
/root/git/ansible/data/example
和 ansible 任務看起來像這樣:
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
并得到同樣的錯誤。有人可以幫忙嗎?
uj5u.com熱心網友回復:
鑒于樹
shell> tree /data/example
/data/example
├── file1
├── file2
└── file3
0 directories, 3 files
所有形式的命令和shell
- 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://www.uj5u.com/caozuo/382630.html
