Ansible:如何更改Ansible Playbook中的活动目录?

ind*_*ent 57 ansible

- name: Go to the folder
  command: chdir=/opt/tools/temp
Run Code Online (Sandbox Code Playgroud)

当我运行我的剧本时,我得到:

TASK: [Go to the folder] ***************************** 
failed: [host] => {"failed": true, "rc": 256}
msg: no command given
Run Code Online (Sandbox Code Playgroud)

任何帮助深表感谢.

Mar*_*nka 79

Ansible中没有当前目录的概念.您可以为特定任务指定当前目录,就像在剧本中一样.唯一缺少的部分是执行的实际命令.试试这个:

- name: Go to the folder and execute command
  command: chdir=/opt/tools/temp ls
Run Code Online (Sandbox Code Playgroud)

  • `“Ansible 中没有当前目录的概念”` 我认为这不再是真的,现在很多任务都采用相对路径,例如 `npm` `composer` `bower` 等 (2认同)

Jos*_*ard 14

这个问题出现在我试图弄清楚为什么'shell'不尊重我的chdir条目时,我不得不恢复到Ansible 1.9的结果.所以我将发布我的解决方案.

我有

- name: task name
  shell:
    cmd: touch foobar
    creates: foobar
    chdir: /usr/lib/foobar
Run Code Online (Sandbox Code Playgroud)

它适用于Ansible> 2,但对于1.9我必须将其更改为.

- name: task name
  shell: touch foobar
  args:
    creates: foobar
    chdir: /usr/lib/foobar
Run Code Online (Sandbox Code Playgroud)

只是想分享.


Alp*_*per 10

您可以在使用 ansible with 运行命令之前更改为目录chdir

这是我刚刚设置的一个例子:

- name: Run a pipenv install
  environment:
    LANG: "en_GB.UTF-8"
  command: "pipenv install --dev"
  args:
    chdir: "{{ dir }}/proj"
Run Code Online (Sandbox Code Playgroud)


Kev*_*ody 7

如果你需要一个登录控制台(比如Bundler),那么你必须这样做.

command: bash -lc "cd /path/to/folder && bundle install"