繁体   English   中英

在 ansible 剧本中更改目录

[英]change directory in ansible playbook

我有 ansible 剧本,我想查看指定的目录(使用 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

并得到同样的错误。 谁能帮忙?

鉴于树

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

0 directories, 3 files

所有 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

按预期工作

  result.stdout:
    file1
    file2
    file3

暂无
暂无

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

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