Joh*_*Doe 61 command module ansible
我试过这个:
- command: ./configure chdir=/src/package/
- command: /usr/bin/make chdir=/src/package/
- command: /usr/bin/make install chdir=/src/package/
Run Code Online (Sandbox Code Playgroud)
哪个有效,但我想还有更多......整洁.
所以我尝试了这个:
来自: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/
Run Code Online (Sandbox Code Playgroud)
我也尝试了这个: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
Run Code Online (Sandbox Code Playgroud)
这不起作用,说有一个引用问题.
任何人?
Ped*_*ano 83
如果YAML中的值以大括号({)开头,则YAML解析器假定它是字典.因此,对于这样的情况,值中存在(Jinja2)变量,需要采用以下两种策略之一来避免混淆YAML解析器:
引用整个命令:
- command: "{{ item }} chdir=/src/package/"
with_items:
- ./configure
- /usr/bin/make
- /usr/bin/make install
Run Code Online (Sandbox Code Playgroud)
或更改参数的顺序:
- command: chdir=/src/package/ {{ item }}
with_items:
- ./configure
- /usr/bin/make
- /usr/bin/make install
Run Code Online (Sandbox Code Playgroud)
感谢@RamondelaFuente的替代建议.
小智 54
要使用ansible运行多个shell命令,可以使用带有多行字符串的shell模块(请注意shell :)之后的垂直斜杠,如下例所示:
- name: Build nginx
shell: |
cd nginx-1.11.13
sudo ./configure
sudo make
sudo make install
Run Code Online (Sandbox Code Playgroud)
小智 13
壳牌对我有用。
简单的说,Shell 就和你运行 shell 脚本一样。
笔记:
以下示例显示 shell 中出现错误,但执行结束时成功。
- name: test shell with an error
become: no
shell: |
rm -f /test1 # This should be an error.
echo "test2"
echo "test1"
echo "test3" # success
Run Code Online (Sandbox Code Playgroud)
此示例显示停止 shell 并出现 exit 1 错误。
- name: test shell with exit 1
become: no
shell: |
rm -f /test1 # This should be an error.
echo "test2"
exit 1 # this stops ansible due to returning an error
echo "test1"
echo "test3" # success
Run Code Online (Sandbox Code Playgroud)
参考: https: //docs.ansible.com/ansible/latest/modules/shell_module.html
小智 7
这里有这样的工人。\o/
- name: "Exec items"
shell: "{{ item }}"
with_items:
- echo "hello"
- echo "hello2"
Run Code Online (Sandbox Code Playgroud)
你也可以这样做:
- command: "{{ item }}"
args:
chdir: "/src/package/"
with_items:
- "./configure"
- "/usr/bin/make"
- "/usr/bin/make install"
Run Code Online (Sandbox Code Playgroud)
希望可以帮助其他
我遇到了同样的问题.在我的例子中,我的部分变量在字典中,即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 }}"
Run Code Online (Sandbox Code Playgroud)
roles/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
Run Code Online (Sandbox Code Playgroud)
上面的每个{{item}}导致类似于以下的错误(对于上面提到的3个命令).如您所见,tools_dir的值未填充(tools_dir是一个变量,它在公共角色的默认值/ 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
Run Code Online (Sandbox Code Playgroud)
解决方案很简单.我没有在Ansible中使用"COMMAND"模块,而是使用了"Shell"模块并在roles/git/defaults/main.yml中创建了一个变量
所以,现在roles/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-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"
Run Code Online (Sandbox Code Playgroud)
和任务角色/ 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
Run Code Online (Sandbox Code Playgroud)
这次,当模块为"SHELL"时,值被成功替换,并且ansible输出回显正确的值.这不需要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",
Run Code Online (Sandbox Code Playgroud)