簡體   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