Ansible | debug モジュール

Ansible の debug モジュールでデバッグ表示を試してみた。

オプション

引数 デフォルト 備考
msg Hello world! 出力用のカスタマイズされたメッセージ。省略時、汎用的なメッセージが出力される。
var デバッグする変数名。msg引数とお互いに排他的である。

msg オプション

設定

playbook.yml

1
2
3
4
5
- hosts: test-hosts
become: true
tasks:
- debug:
msg: "System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}"

実行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ ansible-playbook -i hosts playbook.yml -K
SUDO password:

PLAY [test-hosts] **************************************************************

TASK [setup] *******************************************************************
ok: [192.168.1.201]

TASK [debug] *******************************************************************
ok: [192.168.1.201] => {
"msg": "System 192.168.1.201 has uuid 4C4C4544-0032-5A10-8046-C6C04F594258"
}

PLAY RECAP *********************************************************************
192.168.1.201 : ok=2 changed=0 unreachable=0 failed=0

var オプション

設定

playboo.yml

1
2
3
4
5
6
7
8
9
10
- hosts: test-hosts
become: true
tasks:
- shell: /usr/bin/uptime
register: result

- debug: var=result

- name: Display all variables/facts known for a host
debug: var=hostvars[inventory_hostname]

実行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
$ vi playbook.yml

$ ansible-playbook -i hosts playbook.yml -K
SUDO password:

PLAY [test-hosts] **************************************************************

TASK [setup] *******************************************************************
ok: [192.168.1.201]

TASK [command] *****************************************************************
changed: [192.168.1.201]

TASK [debug] *******************************************************************
ok: [192.168.1.201] => {
"result": {
"changed": true,
"cmd": "/usr/bin/uptime",
"delta": "0:00:00.004671",
"end": "2020-06-11 18:52:23.186351",
"rc": 0,
"start": "2020-06-11 18:52:23.181680",
"stderr": "",
"stdout": " 18:52:23 up 126 days, 5:40, 2 users, load average: 0.48, 0.51, 0.68",
"stdout_lines": [
" 18:52:23 up 126 days, 5:40, 2 users, load average: 0.48, 0.51, 0.68"
],
"warnings": []
}
}

TASK [Display all variables/facts known for a host] ****************************
ok: [192.168.1.201] => {
"hostvars[inventory_hostname]": {
"ansible_all_ipv4_addresses": [
"192.168.1.201",
"172.17.0.1",
"172.23.0.1",
"172.18.0.1"
],
"ansible_all_ipv6_addresses": [
...
"result": {
"changed": true,
"cmd": "/usr/bin/uptime",
"delta": "0:00:00.004671",
"end": "2020-06-11 18:52:23.186351",
"rc": 0,
"start": "2020-06-11 18:52:23.181680",
"stderr": "",
"stdout": " 18:52:23 up 126 days, 5:40, 2 users, load average: 0.48, 0.51, 0.68",
"stdout_lines": [
" 18:52:23 up 126 days, 5:40, 2 users, load average: 0.48, 0.51, 0.68"
],
"warnings": []
}
}
}

PLAY RECAP *********************************************************************
192.168.1.201 : ok=4 changed=1 unreachable=0 failed=0