ansible/utilities/debug.py

59 lines
1.8 KiB
Python
Raw Normal View History

Module to assist users in understanding Ansible After helping someone on IRC he was interested to have this debug module in upstream. This module simply 'prints' a message, and can be ordered to fail if needed. It helps to troubleshoot or understand inventory/facts issues and/or experiment with statements and conditions using only_if. Here is a small example playbook: ```yaml - hosts: all tasks: - local_action: debug msg="System $inventory_hostname has uuid ${ansible_product_uuid}" - local_action: debug msg="System $inventory_hostname lacks a gateway" fail=yes only_if: "is_unset('$ansible_default_ipv4.gateway')" - local_action: debug msg="System $inventory_hostname has gateway ${ansible_default_ipv4.gateway}" only_if: "is_set('$ansible_default_ipv4.gateway')" ``` outputting: ``` [root@moria ansible]# ansible-playbook -v -l localhost:x220 test6.yml PLAY [all] ********************* GATHERING FACTS ********************* ok: [localhost] ok: [x220] TASK: [debug msg="System $inventory_hostname has uuid $ansible_product_uuid"] ********************* ok: [localhost] => {"msg": "System localhost has uuid d125a48c-364f-4e65-b225-fed42ed61fac"} ok: [x220] => {"msg": "System x220 has uuid d125a48c-364f-4e65-b225-fed42ed61fac"} TASK: [debug msg="System $inventory_hostname lacks a gateway" fail=yes] ********************* failed: [localhost] => {"failed": true, "msg": "System localhost lacks a gateway", "rc": 1} ok: [x220] => {"msg": "System x220 has gateway 192.168.1.1"} PLAY RECAP ********************* localhost : ok=2 changed=0 unreachable=0 failed=1 x220 : ok=3 changed=0 unreachable=0 failed=0 ``` I had some other plans for the module, like displaying host inventory and complete inventory to help understand inventory and facts modules, but that would require an action-plugin for transfering inventory information etc... And I am not sure this is wanted/best done in a module.
2012-10-03 14:11:29 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2012 Dag Wieers <dag@wieers.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
DOCUMENTATION = '''
---
module: debug
short_description: Print statements during execution
description:
- This module prints statements during execution and can be useful
for debugging variables or expressions without necessarily halting
the playbook. Useful for debugging together with the 'when:' directive.
Module to assist users in understanding Ansible After helping someone on IRC he was interested to have this debug module in upstream. This module simply 'prints' a message, and can be ordered to fail if needed. It helps to troubleshoot or understand inventory/facts issues and/or experiment with statements and conditions using only_if. Here is a small example playbook: ```yaml - hosts: all tasks: - local_action: debug msg="System $inventory_hostname has uuid ${ansible_product_uuid}" - local_action: debug msg="System $inventory_hostname lacks a gateway" fail=yes only_if: "is_unset('$ansible_default_ipv4.gateway')" - local_action: debug msg="System $inventory_hostname has gateway ${ansible_default_ipv4.gateway}" only_if: "is_set('$ansible_default_ipv4.gateway')" ``` outputting: ``` [root@moria ansible]# ansible-playbook -v -l localhost:x220 test6.yml PLAY [all] ********************* GATHERING FACTS ********************* ok: [localhost] ok: [x220] TASK: [debug msg="System $inventory_hostname has uuid $ansible_product_uuid"] ********************* ok: [localhost] => {"msg": "System localhost has uuid d125a48c-364f-4e65-b225-fed42ed61fac"} ok: [x220] => {"msg": "System x220 has uuid d125a48c-364f-4e65-b225-fed42ed61fac"} TASK: [debug msg="System $inventory_hostname lacks a gateway" fail=yes] ********************* failed: [localhost] => {"failed": true, "msg": "System localhost lacks a gateway", "rc": 1} ok: [x220] => {"msg": "System x220 has gateway 192.168.1.1"} PLAY RECAP ********************* localhost : ok=2 changed=0 unreachable=0 failed=1 x220 : ok=3 changed=0 unreachable=0 failed=0 ``` I had some other plans for the module, like displaying host inventory and complete inventory to help understand inventory and facts modules, but that would require an action-plugin for transfering inventory information etc... And I am not sure this is wanted/best done in a module.
2012-10-03 14:11:29 +02:00
version_added: "0.8"
options:
msg:
description:
2012-11-21 18:49:30 +01:00
- The customized message that is printed. If omitted, prints a generic
Module to assist users in understanding Ansible After helping someone on IRC he was interested to have this debug module in upstream. This module simply 'prints' a message, and can be ordered to fail if needed. It helps to troubleshoot or understand inventory/facts issues and/or experiment with statements and conditions using only_if. Here is a small example playbook: ```yaml - hosts: all tasks: - local_action: debug msg="System $inventory_hostname has uuid ${ansible_product_uuid}" - local_action: debug msg="System $inventory_hostname lacks a gateway" fail=yes only_if: "is_unset('$ansible_default_ipv4.gateway')" - local_action: debug msg="System $inventory_hostname has gateway ${ansible_default_ipv4.gateway}" only_if: "is_set('$ansible_default_ipv4.gateway')" ``` outputting: ``` [root@moria ansible]# ansible-playbook -v -l localhost:x220 test6.yml PLAY [all] ********************* GATHERING FACTS ********************* ok: [localhost] ok: [x220] TASK: [debug msg="System $inventory_hostname has uuid $ansible_product_uuid"] ********************* ok: [localhost] => {"msg": "System localhost has uuid d125a48c-364f-4e65-b225-fed42ed61fac"} ok: [x220] => {"msg": "System x220 has uuid d125a48c-364f-4e65-b225-fed42ed61fac"} TASK: [debug msg="System $inventory_hostname lacks a gateway" fail=yes] ********************* failed: [localhost] => {"failed": true, "msg": "System localhost lacks a gateway", "rc": 1} ok: [x220] => {"msg": "System x220 has gateway 192.168.1.1"} PLAY RECAP ********************* localhost : ok=2 changed=0 unreachable=0 failed=1 x220 : ok=3 changed=0 unreachable=0 failed=0 ``` I had some other plans for the module, like displaying host inventory and complete inventory to help understand inventory and facts modules, but that would require an action-plugin for transfering inventory information etc... And I am not sure this is wanted/best done in a module.
2012-10-03 14:11:29 +02:00
message.
required: false
default: "Hello world!"
var:
description:
- A variable name to debug. Mutually exclusive with the 'msg' option.
author: Dag Wieers, Michael DeHaan
Module to assist users in understanding Ansible After helping someone on IRC he was interested to have this debug module in upstream. This module simply 'prints' a message, and can be ordered to fail if needed. It helps to troubleshoot or understand inventory/facts issues and/or experiment with statements and conditions using only_if. Here is a small example playbook: ```yaml - hosts: all tasks: - local_action: debug msg="System $inventory_hostname has uuid ${ansible_product_uuid}" - local_action: debug msg="System $inventory_hostname lacks a gateway" fail=yes only_if: "is_unset('$ansible_default_ipv4.gateway')" - local_action: debug msg="System $inventory_hostname has gateway ${ansible_default_ipv4.gateway}" only_if: "is_set('$ansible_default_ipv4.gateway')" ``` outputting: ``` [root@moria ansible]# ansible-playbook -v -l localhost:x220 test6.yml PLAY [all] ********************* GATHERING FACTS ********************* ok: [localhost] ok: [x220] TASK: [debug msg="System $inventory_hostname has uuid $ansible_product_uuid"] ********************* ok: [localhost] => {"msg": "System localhost has uuid d125a48c-364f-4e65-b225-fed42ed61fac"} ok: [x220] => {"msg": "System x220 has uuid d125a48c-364f-4e65-b225-fed42ed61fac"} TASK: [debug msg="System $inventory_hostname lacks a gateway" fail=yes] ********************* failed: [localhost] => {"failed": true, "msg": "System localhost lacks a gateway", "rc": 1} ok: [x220] => {"msg": "System x220 has gateway 192.168.1.1"} PLAY RECAP ********************* localhost : ok=2 changed=0 unreachable=0 failed=1 x220 : ok=3 changed=0 unreachable=0 failed=0 ``` I had some other plans for the module, like displaying host inventory and complete inventory to help understand inventory and facts modules, but that would require an action-plugin for transfering inventory information etc... And I am not sure this is wanted/best done in a module.
2012-10-03 14:11:29 +02:00
'''
EXAMPLES = '''
# Example that prints the loopback address and gateway for each host
2013-07-19 15:40:00 +02:00
- debug: msg="System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}"
2013-07-19 15:40:00 +02:00
- debug: msg="System {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }}"
when: ansible_default_ipv4.gateway is defined
- shell: /usr/bin/uptime
register: result
- debug: var=result
- name: Display all variables/facts known for a host
debug: var=hostvars[inventory_hostname]
'''