2014-07-20 32 views
36

我尝试这样做:ansible:如何通过多个命令

- command: ./configure chdir=/src/package/ 
- command: /usr/bin/make chdir=/src/package/ 
- command: /usr/bin/make install chdir=/src/package/ 

其作品,但我想有更多的东西..整齐。

所以,我想这一点:

来自:https://stackoverflow.com/questions/24043561/multiple-commands-in-the-same-line-for-bruker-topspin它给我回来 “没有这样的文件或目录”

- command: ./configure;/usr/bin/make;/usr/bin/make install chdir=/src/package/ 

我想这太:https://u.osu.edu/hasnan.1/2013/12/16/ansible-run-multiple-commands-using-command-module-and-with-items/

,但我找不到正确的语法把:

- command: "{{ item }}" chdir=/src/package/ 
    with_items: 
     ./configure 
     /usr/bin/make 
     /usr/bin/make install 

这是行不通的,s aying有报价问题。

有人吗?

+0

恩,尝试使用'shell'模块。如果你[阅读命令模块上的文档](http://docs.ansible.com/command_module.html),你会明白为什么它不起作用。 (不是一个完整的答案bc我没有测试过) – tedder42

+0

对不起,但我没有阅读shell和命令文档之前,我仍然不知道为什么它不工作。 –

回答

56

如果YAML中的值以大括号开始({),则YAML分析器假定它是a dictionary。因此,对于像这样的情况下,有在值a(Jinja2的)变量,需要以下两个策略之一被采用以避免混淆YAML解析器:

引用整个命令:

- command: "{{ item }} chdir=/src/package/" 
    with_items: 
    - ./configure 
    - /usr/bin/make 
    - /usr/bin/make install  

或改变的参数的顺序:

- command: chdir=/src/package/ {{ item }} 
    with_items: 
    - ./configure 
    - /usr/bin/make 
    - /usr/bin/make install 

感谢@RamondelaFuente替代建议。

+0

例如,您确实需要在某些情况下引用变量,例如,当它们包含空格时。 –

+2

您需要在整个命令周围使用双引号。问题在于值的第一个字符是“{”,它在YAML中有意义。所以它是 - 命令:“{{item}} chdir =/src/package /” –

+0

如果您的某个项目中有变量,该怎么办?即 - cp {{base_dir}} – 4m1r

5

我面临同样的问题。在我的情况下,我的一部分变量是在字典中,即with_dict变量(循环),我必须在每个item.key上运行3个命令。在这里你必须使用with_dict字典与运行多个命令(无需with_items

在一个任务使用with_dict和with_items没有帮助,因为它并没有解决变量该解决方案是更相关。

我的任务是这样的:

- name: Make install git source 
    command: "{{ item }}" 
    with_items: 
    - cd {{ tools_dir }}/{{ item.value.artifact_dir }} 
    - make prefix={{ tools_dir }}/{{ item.value.artifact_dir }} all 
    - make prefix={{ tools_dir }}/{{ item.value.artifact_dir }} install 
    with_dict: "{{ git_versions }}" 

角色/ GIT中/默认/ main.yml是:

--- 
tool: git 
default_git: git_2_6_3 

git_versions: 
    git_2_6_3: 
    git_tar_name: git-2.6.3.tar.gz 
    git_tar_dir: git-2.6.3 
    git_tar_url: https://www.kernel.org/pub/software/scm/git/git-2.6.3.tar.gz 

上面导致类似于用于每个{{项目的下列}的错误}(对于上面提到的3个命令)。正如你所看到的,tools_dir的值没有填充(tools_dir是一个在普通角色的defaults/main.yml中定义的变量,并且item.value.git_tar_dir的值未被填充/解析)。

failed: [server01.poc.jenkins] => (item=cd {# tools_dir #}/{# item.value.git_tar_dir #}) => {"cmd": "cd '{#' tools_dir '#}/{#' item.value.git_tar_dir '#}'", "failed": true, "item": "cd {# tools_dir #}/{# item.value.git_tar_dir #}", "rc": 2} 
msg: [Errno 2] No such file or directory 

解决方案很简单。而不是在Ansible中使用“COMMAND”模块,我使用了“Shell”模块并在角色/创建了一个变量/ git/defaults/main.yml

所以,现在角色/ git/defaults/main。阳明海运的样子:

--- 
tool: git 
default_git: git_2_6_3 

git_versions: 
    git_2_6_3: 
    git_tar_name: git-2.6.3.tar.gz 
    git_tar_dir: git-2.6.3 
    git_tar_url: https://www.kernel.org/pub/software/scm/git/git-2.6.3.tar.gz 

#git_pre_requisites_install_cmds: "cd {{ tools_dir }}/{{ item.value.git_tar_dir }} && make prefix={{ tools_dir }}/{{ item.value.git_tar_dir }} all && make prefix={{ tools_dir }}/{{ item.value.git_tar_dir }} install" 

#or use this if you want git installation to work in ~/tools/git-x.x.x 
git_pre_requisites_install_cmds: "cd {{ tools_dir }}/{{ item.value.git_tar_dir }} && make prefix=`pwd` all && make prefix=`pwd` install" 

#or use this if you want git installation to use the default prefix during make 
#git_pre_requisites_install_cmds: "cd {{ tools_dir }}/{{ item.value.git_tar_dir }} && make all && make install" 

和任务角色/混帐/任务/ main.yml样子:

- name: Make install from git source 
    shell: "{{ git_pre_requisites_install_cmds }}" 
    become_user: "{{ build_user }}" 
    with_dict: "{{ git_versions }}" 
    tags: 
    - koba 

这一次,值得到了成功取代的模块是 “SHELL”并且可靠的输出回应了正确的值。 这不需要with_items:循环。

"cmd": "cd ~/tools/git-2.6.3 && make prefix=/home/giga/tools/git-2.6.3 all && make prefix=/home/giga/tools/git-2.6.3 install", 
2

你也可以这样做:

- command: "{{ item }}" 
    args: 
    chdir: "/src/package/" 
    with_items: 
    - "./configure" 
    - "/usr/bin/make" 
    - "/usr/bin/make install" 

希望这可以帮助其他

13

运行与ansible多个shell命令,你可以使用外壳模块,多行字符串(请注意垂直斜杠后壳:),如在此示例中所示:

- name: Build nginx 
    shell: | 
     cd nginx-1.11.13 
     sudo ./configure 
     sudo make 
     sudo make install