简体   繁体   English

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

[英]Ansible: How to change active directory in Ansible Playbook?

- name: Go to the folder
  command: chdir=/opt/tools/temp

When I run my playbook, I get:当我运行我的剧本时,我得到:

TASK: [Go to the folder] ***************************** 
failed: [host] => {"failed": true, "rc": 256}
msg: no command given

Any help is much appreciated.任何帮助深表感谢。

There's no concept of current directory in Ansible. Ansible 中没有当前目录的概念。 You can specify current directory for specific task, like you did in your playbook.您可以为特定任务指定当前目录,就像您在剧本中所做的那样。 The only missing part was the actual command to execute.唯一缺少的部分是要执行的实际命令。 Try this:尝试这个:

- name: Go to the folder and execute command
  command: chdir=/opt/tools/temp ls

This question was in the results for when I was trying to figure out why 'shell' was not respecting my chdir entries when I had to revert to Ansible 1.9.当我不得不恢复到 Ansible 1.9 时,当我试图弄清楚为什么“shell”不尊重我的chdir条目时,这个问题出现在结果中。 So I will be posting my solution.所以我将发布我的解决方案。

I had我有

- name: task name
  shell:
    cmd: touch foobar
    creates: foobar
    chdir: /usr/lib/foobar

It worked with Ansible > 2, but for 1.9 I had to change it to.它适用于 Ansible > 2,但对于 1.9,我不得不将其更改为。

- name: task name
  shell: touch foobar
  args:
    creates: foobar
    chdir: /usr/lib/foobar

Just wanted to share.只是想分享。

如果你需要一个登录控制台(比如 bundler),那么你必须像这样执行命令。

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

You can change into a directory before running a command with ansible with chdir .在使用 ansible 和chdir运行命令之前,您可以切换到一个目录。

Here's an example I just setup:这是我刚刚设置的示例:

- name: Run a pipenv install
  environment:
    LANG: "en_GB.UTF-8"
  command: "pipenv install --dev"
  args:
    chdir: "{{ dir }}/proj"

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

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