现在我在ansible中使用shell脚本,如果它在多行上,那么它的可读性会更高
- name: iterate user groups
shell: groupmod -o -g {{ item['guid'] }} {{ item['username'] }} ....more stuff to do
with_items: "{{ users }}"
只是不确定如何在 Ansible shell 模块中允许多行脚本
Ansible 在其 playbook 中使用 YAML 语法。 YAML 有许多块运算符:
-
The >
是一个折叠块操作符。也就是说,它通过空格将多行连接在一起。语法如下:
key: >
This text
has multiple
lines
将分配值This text has multiple lines\n
to key
.
-
The |
字符是文字块运算符。这可能就是您想要的多行 shell 脚本。语法如下:
key: |
This text
has multiple
lines
将分配值This text\nhas multiple\nlines\n
to key
.
您可以将其用于多行 shell 脚本,如下所示:
- name: iterate user groups
shell: |
groupmod -o -g {{ item['guid'] }} {{ item['username'] }}
do_some_stuff_here
and_some_other_stuff
with_items: "{{ users }}"
有一个警告:Ansible 对参数进行了一些简单的操作shell
命令,因此虽然上面的命令通常会按预期工作,但以下命令不会:
- shell: |
cat <<EOF
This is a test.
EOF
Ansible 实际上会用前导空格呈现该文本,这意味着 shell 永远不会找到该字符串EOF
在一行的开头。您可以使用以下方法来避免 Ansible 的无用启发式方法:cmd
参数如下:
- shell:
cmd: |
cat <<EOF
This is a test.
EOF
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)