【问题标题】:Ansible: How to change active directory in Ansible Playbook?Ansible:如何在 Ansible Playbook 中更改活动目录?
【发布时间】:2013-10-22 14:10:56
【问题描述】:
- name: Go to the folder
  command: chdir=/opt/tools/temp

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

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

非常感谢任何帮助。

【问题讨论】:

  • 您究竟想通过更改活动目录做什么?
  • chdir 也是命令的一个属性。您可以执行命令并声明 chdir command: ls chdir=/path/to/directory
  • 这和微软的ActiveDirectory没有关系吧?

标签: ansible


【解决方案1】:

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

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

【讨论】:

  • 很高兴能够在特定目录中运行所有剧本
  • "There's no concept of current directory in Ansible" 我不认为这是真的,现在很多任务都采用相对路径,例如npm composer bower
【解决方案2】:

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

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

【讨论】:

  • 这也是与作曲家一起使用的命令(如果作曲家模块没有让你到达那里):command: bash -lc "cd /var/www/ && /usr/local/bin/composer install"
  • 您现在可以使用working_dir 指定作曲家路径相对
【解决方案3】:

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

我有

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

它适用于 Ansible > 2,但对于 1.9,我不得不将其更改为。

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

只是想分享。

【讨论】:

    【解决方案4】:

    您可以在使用 chdir 使用 ansible 运行命令之前切换到一个目录。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多