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 

这不行,说有报价问题。

任何人?

如果YAML中的值以大括号( { )开始,则YAMLparsing器假定它是一个字典 。 所以,对于这样的情况下,值中有一个(Jinja2)variables,需要采取以下两个策略之一来避免混淆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替代build议。

要使用多行命令运行多个shell命令,可以使用多行string的shell模块(请注意shell之后的垂直斜线) ,如下例所示:

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

我面临同样的问题。 在我的情况下,我的variables的一部分是在字典中,即variables(循环)with_dict,我不得不在每个item.key上运行3个命令。 这个解决scheme更加相关,你必须使用with_dict字典来运行多个命令( 不需要with_items

在一个任务中使用with_dict和with_items没有帮助,因为它没有解决variables。

我的任务是这样的:

 - 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 }}" 

angular色/ git / defaults / 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 

对于每个{{item}}(上面提到的3个命令),上面的结果都会产生类似下面的错误。 正如你所看到的,tools_dir的值没有被填充(tools_dir是一个在普通angular色的defaults / main.yml中定义的variables,item.value.git_tar_dir的值也没有被填充/parsing)。

 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 

解决scheme很简单。 在Ansible中我没有使用“COMMAND”模块,而是使用了“Shell”模块,并在angular色/ git / defaults / main.yml中创build了一个variables

所以,现在angular色/ git / defaults / 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 #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-xxx 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" 

和任务angular色/ git / tasks / 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: loop。

 "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", 

你也可以这样做:

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

希望可以帮助别人