ansible debug模块是最常用的调试模块,用于输出变量、表达式或自定义信息,在编写和排查playbook时非常有用。
1.基本语法
– name: Debug message
debug:
msg: \”Hello World\”
2.样例
1.debug基本测试
– hosts: all
tasks:
– name: Debug message
debug:
msg: \”Hello World\”
输出:
[root@cent79-2 yaml-directory]# ansible-playbook debug.yaml
PLAY [all] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [192.168.10.100]
TASK [Debug message] ******************************************************************************************
ok: [192.168.10.100] => {
\”msg\”: \”Hello World\”
}
PLAY RECAP ****************************************************************************************************
192.168.10.100 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@cent79-2 yaml-directory]#
2.输出变量(var)
– hosts: all
tasks:
– name: Show hostname
debug:
var: ansible_hostname
输出:
[root@cent79-2 yaml-directory]# ansible-playbook debug.yaml
PLAY [all] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [192.168.10.100]
TASK [Show hostname] ******************************************************************************************
ok: [192.168.10.100] => {
\”ansible_hostname\”: \”patrolagent\”
}
PLAY RECAP ****************************************************************************************************
192.168.10.100 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@cent79-2 yaml-directory]#
3.查看注册变量
– hosts: all
tasks:
– name: Show hostname
shell: hostname
register: hostname_result
– debug:
var: hostname_result
输出:
[root@cent79-2 yaml-directory]# ansible-playbook debug.yaml
PLAY [all] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [192.168.10.100]
TASK [Show hostname] ******************************************************************************************
changed: [192.168.10.100]
TASK [debug] **************************************************************************************************
ok: [192.168.10.100] => {
\”hostname_result\”: {
\”changed\”: true,
\”cmd\”: \”hostname\”,
\”delta\”: \”0:00:00.002204\”,
\”end\”: \”2026-07-13 15:56:09.473972\”,
\”failed\”: false,
\”rc\”: 0,
\”start\”: \”2026-07-13 15:56:09.471768\”,
\”stderr\”: \”\”,
\”stderr_lines\”: [],
\”stdout\”: \”patrolagent\”,
\”stdout_lines\”: [
\”patrolagent\”
]
}
}
PLAY RECAP ****************************************************************************************************
192.168.10.100 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@cent79-2 yaml-directory]
如果只想输出命令结果
– hosts: all
tasks:
– name: Show hostname
shell: hostname
register: hostname_result
– debug:
var: hostname_result.stdout
输出:
[root@cent79-2 yaml-directory]# ansible-playbook debug.yaml
PLAY [all] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [192.168.10.100]
TASK [Show hostname] ******************************************************************************************
changed: [192.168.10.100]
TASK [debug] **************************************************************************************************
ok: [192.168.10.100] => {
\”hostname_result.stdout\”: \”patrolagent\”
}
PLAY RECAP ****************************************************************************************************
192.168.10.100 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@cent79-2 yaml-directory]#
4.输出自定义信息(msg)
– hosts: all
tasks:
– debug:
msg: \”Current host is {
{ ansible_hostname }}\”
输出:
[root@cent79-2 yaml-directory]# ansible-playbook debug.yaml
PLAY [all] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [192.168.10.100]
TASK [debug] **************************************************************************************************
ok: [192.168.10.100] => {
\”msg\”: \”Current host is patrolagent\”
}
PLAY RECAP ****************************************************************************************************
192.168.10.100 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@cent79-2 yaml-directory]#
输出多个变量
– hosts: all
tasks:
– debug:
msg:
– \”Hostname: {
{ ansible_hostname }}\”
– \”IP: {
{ ansible_default_ipv4.address }}\”
– \”OS: {
{ ansible_distribution }}\”
输出:
[root@cent79-2 yaml-directory]# ansible-playbook debug.yaml
PLAY [all] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [192.168.10.100]
TASK [debug] **************************************************************************************************
ok: [192.168.10.100] => {
\”msg\”: [
\”Hostname: patrolagent\”,
\”IP: 192.168.10.100\”,
\”OS: RedHat\”
]
}
PLAY RECAP ****************************************************************************************************
192.168.10.100 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@cent79-2 yaml-directory]#
5.输出表达式结果
– hosts: all
tasks:
– debug:
msg: \”{
{ 10 + 20 }}\”
输出:
[root@cent79-2 yaml-directory]# ansible-playbook debug.yaml
PLAY [all] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [192.168.10.100]
TASK [debug] **************************************************************************************************
ok: [192.168.10.100] => {
\”msg\”: \”30\”
}
PLAY RECAP ****************************************************************************************************
192.168.10.100 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@cent79-2 yaml-directory]#
判断条件
– hosts: all
tasks:
– debug:
msg: \”{
{ ansible_memtotal_mb > 4096 }}\”
输出:
[root@cent79-2 yaml-directory]# ansible-playbook debug.yaml
PLAY [all] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [192.168.10.100]
TASK [debug] **************************************************************************************************
ok: [192.168.10.100] => {
\”msg\”: false
}
PLAY RECAP ****************************************************************************************************
192.168.10.100 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@cent79-2 yaml-directory]#
6.配合when
– hosts: all
tasks:
– debug:
msg: \”Memory is greater than 4GB\”
when: ansible_memtotal_mb > 4096
输出:
[root@cent79-2 yaml-directory]# ansible-playbook debug.yaml
PLAY [all] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [192.168.10.100]
TASK [debug] **************************************************************************************************
skipping: [192.168.10.100]
PLAY RECAP ****************************************************************************************************
192.168.10.100 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[root@cent79-2 yaml-directory]#
7.配合register
– hosts: all
tasks:
– name: Check nginx
shell: systemctl is-active PatrolAgent
register: pa_status
– name: Pa output
debug:
var: pa_status.stdout
输出:
[root@cent79-2 yaml-directory]# ansible-playbook debug.yaml
PLAY [all] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [192.168.10.100]
TASK [Check nginx] ********************************************************************************************
changed: [192.168.10.100]
TASK [Pa output] **********************************************************************************************
ok: [192.168.10.100] => {
\”pa_status.stdout\”: \”active\”
}
PLAY RECAP ****************************************************************************************************
192.168.10.100 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@cent79-2 yaml-directory]#
8.查看所有Facts
– hosts: all
tasks:
– name: Facts output
debug:
var: ansible_facts
输出:
[root@cent79-2 yaml-directory]# ansible-playbook debug.yaml
PLAY [all] ****************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [192.168.10.100]
TASK [Facts output] *******************************************************************************************
ok: [192.168.10.100] => {
\”ansible_facts\”: {
\”all_ipv4_addresses\”: [
\”192.168.10.100\”,
\”192.168.10.111\”
],
\”all_ipv6_addresses\”: [
\”fe80::6a88:30a0:2d04:24ed\”
],
\”ansible_local\”: {},
\”apparmor\”: {
\”status\”: \”disabled\”
},
\”architecture\”: \”x86_64\”,
\”bios_date\”: \”11/12/2020\”,
\”bios_version\”: \”6.00\”,
\”cmdline\”: {
\”BOOT_IMAGE\”: \”/vmlinuz-3.10.0-693.el7.x86_64\”,
\”LANG\”: \”en_US.UTF-8\”,
\”quiet\”: true,
\”rd.lvm.lv\”: \”rhel_patrolagent/swap\”,
\”rhgb\”: true,
\”ro\”: true,
\”root\”: \”/dev/mapper/rhel_patrolagent-root\”
},
\”date_time\”: {
\”date\”: \”2026-07-13\”,
\”day\”: \”13\”,
\”epoch\”: \”1783932084\”,
\”hour\”: \”16\”,
\”iso8601\”: \”2026-07-13T08:41:24Z\”,
\”iso8601_basic\”: \”20260713T164124285920\”,
\”iso8601_basic_short\”: \”20260713T164124\”,
\”iso8601_micro\”: \”2026-07-13T08:41:24.285920Z\”,
\”minute\”: \”41\”,
\”month\”: \”07\”,
\”second\”: \”24\”,
\”time\”: \”16:41:24\”,
\”tz\”: \”CST\”,
\”tz_offset\”: \”+0800\”,
\”weekday\”: \”Monday\”,
\”weekday_number\”: \”1\”,
\”weeknumber\”: \”28\”,
\”year\”: \”2026\”
},
\”default_ipv4\”: {
\”address\”: \”192.168.10.100\”,
\”alias\”: \”ens33\”,
\”broadcast\”: \”192.168.10.255\”,
\”gateway\”: \”192.168.10.1\”,
\”interface\”: \”ens33\”,
\”macaddress\”: \”00:0c:29:7c:8d:7b\”,
\”mtu\”: 1500,
\”netmask\”: \”255.255.255.0\”,
\”network\”: \”192.168.10.0\”,
\”type\”: \”ether\”
},
\”default_ipv6\”: {},
\”device_links\”: {
\”ids\”: {
\”dm-0\”: [
\”dm-name-rhel_patrolagent-root\”,
\”dm-uuid-LVM-UlaebvSQUYHFSvRiRiQgmUk4M2n1X4fBSE24QCo1EkXin4VkULSH3U6xkiCLvACC\”
],
\”dm-1\”: [
\”dm-name-rhel_patrolagent-swap\”,
\”dm-uuid-LVM-UlaebvSQUYHFSvRiRiQgmUk4M2n1X4fBrcpQr58KLciodrOxFf1dy2iwzJ7XfOe5\”
],
\”dm-2\”: [
\”dm-name-rhel_patrolagent-var\”,
\”dm-uuid-LVM-UlaebvSQUYHFSvRiRiQgmUk4M2n1X4fBPZ1fz70wHTdVNEixh4KNjJEILFyr1HVJ\”
],
\”dm-3\”: [
\”dm-name-rhel_patrolagent-home\”,
\”dm-uuid-LVM-UlaebvSQUYHFSvRiRiQgmUk4M2n1X4fBxk2EgXPcz21MGa9t2DafrOOVgZzgDNPC\”
],
\”sda2\”: [
\”lvm-pv-uuid-VRgGxq-GkDc-m54A-5T1D-j4MQ-hwCj-YP32cV\”
],
\”sr0\”: [
\”ata-VMware_Virtual_SATA_CDRW_Drive_01000000000000000001\”
]
},
\”labels\”: {
\”sr0\”: [
\”RHEL-7.4\\\\x20Server.x86_64\”
]
},
\”masters\”: {
\”sda2\”: [
\”dm-0\”,
\”dm-1\”,
\”dm-2\”,
\”dm-3\”
]
},
\”uuids\”: {
\”dm-0\”: [
\”f50ba8b5-e369-4235-8c3b-1e7b6d0bda48\”
],
\”dm-1\”: [
\”30d1fee5-cb49-489c-b787-e33ee0a9b131\”
],
\”dm-2\”: [
\”c3b19f3a-71ee-450b-9d34-956675a278a1\”
],
\”dm-3\”: [
\”f9f9733b-9aa4-4d7d-84b8-40d4f53d412d\”
],
\”sda1\”: [
\”bc617ee4-b4e6-4f09-9a4b-2025d80e4684\”
],
\”sr0\”: [
\”2017-07-11-01-39-24-00\”
]
}
},
\”devices\”: {
\”dm-0\”: {
\”holders\”: []
网硕互联帮助中心




评论前必须登录!
注册