Aru*_*gal 47 action newline roles ansible ansible-playbook
在Ansible(1.9.4)或2.0.0中
我运行了以下操作:
- debug: msg="line1 \n {{ var2 }} \n line3 with var3 = {{ var3 }}"
Run Code Online (Sandbox Code Playgroud)
$ cat roles/setup_jenkins_slave/tasks/main.yml
- debug: msg="Installing swarm slave = {{ slave_name }} at {{ slaves_dir }}/{{ slave_name }}"
tags:
- koba
- debug: msg="1 == Slave properties = fsroot[ {{ slave_fsroot }} ], master[ {{ slave_master }} ], connectingToMasterAs[ {{ slave_user }} ], description[ {{ slave_desc }} ], No.Of.Executors[ {{ slave_execs }} ], LABELs[ {{ slave_labels }} ], mode[ {{ slave_mode }} ]"
tags:
- koba
- debug: msg="print(2 == Slave properties = \n\nfsroot[ {{ slave_fsroot }} ],\n master[ {{ slave_master }} ],\n connectingToMasterAs[ {{ slave_user }} ],\n description[ {{ slave_desc }} ],\n No.Of.Executors[ {{ slave_execs }} ],\n LABELs[ {{ slave_labels }} ],\n mode[ {{ slave_mode }} ])"
tags:
- koba
Run Code Online (Sandbox Code Playgroud)
但这不是用新行打印变量(对于第三个调试操作)?
dia*_*neo 59
调试模块支持数组,所以你可以这样做:
debug:
msg:
- "First line"
- "Second line"
Run Code Online (Sandbox Code Playgroud)
输出:
ok: [node1] => {
"msg": [
"First line",
"Second line"
]
}
Run Code Online (Sandbox Code Playgroud)
或者您可以使用此答案中的方法:
mai*_*kel 47
我发现使用debug打印多行文本最方便的方法是:
- name: Print several lines of text
vars:
msg: |
This is the first line.
This is the second line with a variable like {{ inventory_hostname }}.
And here could be more...
debug:
msg: "{{ msg.split('\n') }}"
Run Code Online (Sandbox Code Playgroud)
它将消息拆分为一个数组,调试将每一行打印为字符串.输出是:
ok: [example.com] => {
"msg": [
"This is the first line.",
"This is the second line with a variable like example.com",
"And here could be more...",
""
]
}
Run Code Online (Sandbox Code Playgroud)
感谢jhutar.
Eje*_*jez 16
我发现显示带有格式的消息(例如:换行、制表符...)的最方便和简单的方法是使用pause模块而不是debug模块:
- pause:
seconds: 1
prompt: |
======================
line_1
line_2
======================
Run Code Online (Sandbox Code Playgroud)
您还可以在提示中包含一个包含格式(新行、制表符...)的变量,它将按预期显示:
- name: test
hosts: all
vars:
line3: "\n line_3"
tasks:
- pause:
seconds: 1
prompt: |
/////////////////
line_1
line_2 {{ line3 }}
/////////////////
Run Code Online (Sandbox Code Playgroud)
当您想显示命令的输出,而不是运行额外的任务来运行命令并注册输出时,您可以直接使用提示中的管道查找并一次性完成工作:
- pause:
seconds: 1
prompt: |
=========================
line_1
{{ lookup('pipe', 'echo "line_2 with \t tab \n line_3 "') }}
line_4
=========================
Run Code Online (Sandbox Code Playgroud)
如果您有多个主机,请注意该pause任务将仅针对主机列表中的第一个主机运行一次。
这意味着如果您要显示的变量仅存在于部分主机中,而第一个主机不包含该变量,那么您将收到错误消息。
为避免出现此类问题,请使用{{ hostvars['my_host']['my_var'] }}
代替{{ my_var }}
结合pause使用when条件可能会跳过任务!为什么?因为该任务只会针对可能不符合规定when条件的第一个主机运行一次。
为避免这种情况,请不要使用限制主机数量的条件!因为您也不需要它,因为您知道该任务无论如何只会运行一次。还可以使用hostvars上述说明来确保您获得所需的变量,无论选择的主机是什么。
例子:
不正确:
- name: test
hosts: host1,host2
vars:
display_my_var: true
tasks:
- when: inventory_hostname == 'host2'
set_fact:
my_var: "hi there"
- when:
- display_my_var|bool
- inventory_hostname == 'host2'
pause:
seconds: 1
prompt: |
{{ my_var }}
Run Code Online (Sandbox Code Playgroud)
这个例子会跳过暂停任务,因为它会只选择第一个主机host1然后开始评估条件,当它发现host1不符合第二个条件时,它会跳过任务。
正确的:
- name: test
hosts: host1,host2
vars:
display_my_var: true
tasks:
- when: inventory_hostname == 'host2'
set_fact:
my_var: "hi there"
- when: display_my_var|bool
pause:
seconds: 1
prompt: |
{{ hostvars['host2']['my_var'] }}
Run Code Online (Sandbox Code Playgroud)
显示内容取决于主机的消息的另一个示例:
- set_fact:
my_var: "hi from {{ inventory_hostname }}"
- pause:
seconds: 1
prompt: |
{% for host in ansible_play_hosts %}
{{ hostvars[host]['my_var'] }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
apt与[:-1]---
- name: 'apt: update & upgrade'
apt:
update_cache: yes
cache_valid_time: 3600
upgrade: safe
register: apt
- debug: msg={{ apt.stdout.split('\n')[:-1] }}
Run Code Online (Sandbox Code Playgroud)
上debug:一行由于导致了不错的换行.split('\n'),并且由于导致了最后一个空字符串被抑制[:-1]。当然,所有这些都是Python字符串操作。
"msg": [
"Reading package lists...",
"Building dependency tree...",
"Reading state information...",
"Reading extended state information...",
"Initializing package states...",
"Building tag database...",
"No packages will be installed, upgraded, or removed.",
"0 packages upgraded, 0 newly installed, 0 to remove and 0 not upgraded.",
"Need to get 0 B of archives. After unpacking 0 B will be used.",
"Reading package lists...",
"Building dependency tree...",
"Reading state information...",
"Reading extended state information...",
"Initializing package states...",
"Building tag database..."
]
Run Code Online (Sandbox Code Playgroud)
您可以使用stdout_lines寄存器变量:
- name: Do something
shell: "ps aux"
register: result
- debug: var=result.stdout_lines
Run Code Online (Sandbox Code Playgroud)
我对 @Bruce P 关于通过 sed 进行管道输出的回答进行了一些挖掘,这就是我想到的:
ansible-playbook [blablabla] | sed 's/\\n/\n/g'
Run Code Online (Sandbox Code Playgroud)
如果有人感兴趣的话。
作为一种解决方法,我使用了with_items,它对我有用。
- debug: msg="Installing swarm slave = {{ slave_name }} at {{ slaves_dir }}/{{ slave_name }}"
- debug: msg="Slave properties = {{ item.prop }} [ {{ item.value }} ]"
with_items:
- { prop: 'fsroot', value: "{{ slave_fsroot }}" }
- { prop: 'master', value: "{{ slave_master }}" }
- { prop: 'connectingToMasterAs', value: "{{ slave_user }}" }
- { prop: 'description', value: "{{ slave_desc }}" }
- { prop: 'No.Of.Executors', value: "{{ slave_execs }}" }
- { prop: 'LABELs', value: "{{ slave_labels }}" }
- { prop: 'mode', value: "{{ slave_mode }}" }
tags:
- koba
Run Code Online (Sandbox Code Playgroud)