Ansible - Print message - debug: msg=“line1
{{ var2 }}
line3 with var3 = {{ var3 }}”
在Ansible(1.9.4)或2.0.0中
我执行了以下操作:
1 2 3 | - debug: msg="line1 {{ var2 }} line3 with var3 = {{ var3 }}" |
$cat角色/设置u jenkins u slave/tasks/main.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | - 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 = 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 |
但这不是用新行打印变量(用于第三次调试操作)?
调试模块支持数组,因此可以这样做:
1 2 3 4 | debug: msg: -"First line" -"Second line" |
输出:
1 2 3 4 5 6 | ok: [node1] => { "msg": [ "First line", "Second line" ] } |
或者您可以使用此答案中的方法:
在yaml中,如何在多行上断开一个字符串?
我发现使用调试打印多行文本最方便的方法是:
1 2 3 4 5 6 7 8 9 | - 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(' ') }}" |
它将消息拆分成一个数组,Debug将每一行作为字符串打印。输出是:
1 2 3 4 5 6 7 8 | 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...", "" ] } |
多亏了朱塔。
用
1 2 3 4 5 6 7 8 9 | --- - name: 'apt: update & upgrade' apt: update_cache: yes cache_valid_time: 3600 upgrade: safe register: apt - debug: msg={{ apt.stdout.split(' ')[:-1] }} |
上面的
')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | "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..." ] |
我在@bruce p answer上挖掘了一些有关通过sed进行管道输出的信息,这就是我想到的:
1 2 3 | ansible-playbook [blablabla] | sed 's/\ / /g' |
如果有人感兴趣。
这在这里讨论。简而言之,您要么需要通过SED传输输出以将
作为一个解决方法,我使用了一些物品,它对我有点作用。
1 2 3 4 5 6 7 8 9 10 11 12 13 | - 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 |