Source From Here
Question
I have written a post about how to debug playbooks by dumping all variables in file (on remote server) by using Ansible template file. Here is some faster and more convenient way to print multiple variables or all with debug purpose inside a playbook. Here are some handy commands for quick dumping of a given variable, multiple variables, or all variables.
Dump all variables/facts for a given host (without invoking a playbook)
1) Based on inventory file
2) Without inventory file
Here is how you can dump the facts, without even having an inventory file:
(Don’t forget the “,” (comma) in the end of the hostname/ip)
Printing multiple Ansible variables with debug purpose (inside a playbook)
First you need to define your debug task , which I called debug_info in my case. I have used some nice technique (I found out there) for printing multi-line message inside debug statement:
In order to get only the debug information (
without executing any other tasks inside the playbook), you could limit the task executing by providing “–tags ‘debug_info’ ” to ansible-playbook command. So after executing command "ansible-playbook debug-print-variables/test1.yaml --tags "debug_info" with output similar to:
Printing all Ansible variables with debug purpose (inside a playbook)
Now if we want to print all internal variables, we could use the following yaml:
Another good way is to use something like that:
Executing this task is going to dump all your variables:
Question
I have written a post about how to debug playbooks by dumping all variables in file (on remote server) by using Ansible template file. Here is some faster and more convenient way to print multiple variables or all with debug purpose inside a playbook. Here are some handy commands for quick dumping of a given variable, multiple variables, or all variables.
Dump all variables/facts for a given host (without invoking a playbook)
1) Based on inventory file
- # Dump facts for host "some_host" which is defined inside inventory_file.txt
- ansible -i inventory_file.txt some_host -m setup
Here is how you can dump the facts, without even having an inventory file:
(Don’t forget the “,” (comma) in the end of the hostname/ip)
- # Dump facts for ip 1.1.1.1
- ansible -i1.1.1.1, some_host -m setup
- # Dump facts for domain example.com
- ansible -iexample.com, some_host -m setup
First you need to define your debug task , which I called debug_info in my case. I have used some nice technique (I found out there) for printing multi-line message inside debug statement:
- - name: Print some debug information
- hosts: localhost
- vars:
- msg: |
- Ansible Distribution: {{ ansible_distribution }}
- Ansible Dist version: {{ ansible_distribution_version }}
- tags: debug_info
- tasks:
- - name: debug
- debug:
- msg: "{{ msg.split('\n') }}"
Printing all Ansible variables with debug purpose (inside a playbook)
Now if we want to print all internal variables, we could use the following yaml:
- - name: Print some debug information
- hosts: localhost
- vars:
- msg: |
- Module Variables ("vars"):
- --------------------------------
- {{ vars | to_nice_json }}
- Environment Variables ("environment"):
- --------------------------------
- {{ environment | to_nice_json }}
- GROUP NAMES Variables ("group_names"):
- --------------------------------
- {{ group_names | to_nice_json }}
- GROUPS Variables ("groups"):
- --------------------------------
- {{ groups | to_nice_json }}
- HOST Variables ("hostvars"):
- --------------------------------
- {{ hostvars | to_nice_json }}
- tasks:
- - name: debug1
- debug:
- msg: "{{ msg.split('\n') }}"
- tags: debug_info1
- - name: debug2
- debug:
- var: hostvars[inventory_hostname]
- tags: debug_info2
- ...
- tasks:
- - name: debug1
- debug:
- msg: "{{ msg.split('\n') }}"
- tags: debug_info1
- # Second way to print variables for debugging
- - name: debug2
- debug:
- var: hostvars[inventory_hostname]
- tags: debug_info2
沒有留言:
張貼留言