简体   繁体   English

在 ansible 剧本中更改目录

[英]change directory in ansible playbook

i have ansible playbook and i want to look at specified directory (with ls command).我有 ansible 剧本,我想查看指定的目录(使用 ls 命令)。 the directory is in the root, i check with the pwd command:该目录位于根目录中,我使用 pwd 命令检查:

/root/git/ansible/data/example

and the ansible the task looks like this:和 ansible 任务看起来像这样:

name:  open directory
become: yes
become_user: root
command: chdir=/data/example ls

after i execute the playbook i get this error:执行剧本后,我收到此错误:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unable to change directory before execution: [Errno 2] No such file or directory: '/data/example'"}

while the directory actually does exist.虽然该目录确实存在。 i also tried this:我也试过这个:

name:  open directory
become: yes
become_user: root
shell:
  cmd: ls
  chdir: /data/example

and got the same error.并得到同样的错误。 can anyone help?谁能帮忙?

Given the tree鉴于树

shell> tree /data/example
/data/example
├── file1
├── file2
└── file3

0 directories, 3 files

all forms of command and shell所有 forms命令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

work as expected按预期工作

  result.stdout:
    file1
    file2
    file3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM