2020年3月13日 星期五

[Ansible 常見問題] Ansible Debug Print All Variables

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
  1. # Dump facts for host "some_host" which is defined inside inventory_file.txt  
  2. ansible -i inventory_file.txt some_host -m setup  
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)
  1. # Dump facts for ip 1.1.1.1  
  2. ansible -i1.1.1.1,  some_host -m setup   
  3.   
  4. # Dump facts for domain example.com  
  5. ansible -iexample.com,  some_host -m setup   
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:
  1. - name: Print some debug information  
  2.   hosts: localhost  
  3.   vars:  
  4.     msg: |  
  5.         Ansible Distribution: {{ ansible_distribution }}  
  6.         Ansible Dist version: {{ ansible_distribution_version }}  
  7.   tags: debug_info  
  8.   tasks:  
  9.   - name: debug  
  10.     debug:  
  11.         msg: "{{ msg.split('\n') }}"  
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:
  1. - name: Print some debug information  
  2.   hosts: localhost  
  3.   vars:  
  4.     msg: |  
  5.         Module Variables ("vars"):  
  6.         --------------------------------  
  7.         {{ vars | to_nice_json }}  
  8.   
  9.         Environment Variables ("environment"):  
  10.         --------------------------------  
  11.         {{ environment | to_nice_json }}  
  12.   
  13.         GROUP NAMES Variables ("group_names"):  
  14.         --------------------------------  
  15.         {{ group_names | to_nice_json }}  
  16.   
  17.         GROUPS Variables ("groups"):  
  18.         --------------------------------  
  19.         {{ groups | to_nice_json }}  
  20.   
  21.         HOST Variables ("hostvars"):  
  22.         --------------------------------  
  23.         {{ hostvars | to_nice_json }}  
  24.   
  25.   tasks:  
  26.   - name: debug1  
  27.     debug:  
  28.         msg: "{{ msg.split('\n') }}"  
  29.     tags: debug_info1  
  30.   - name: debug2  
  31.     debug:  
  32.         var: hostvars[inventory_hostname]  
  33.     tags: debug_info2  
Another good way is to use something like that:
  1. ...  
  2.   tasks:  
  3.   - name: debug1  
  4.     debug:  
  5.         msg: "{{ msg.split('\n') }}"  
  6.     tags: debug_info1  
  7.   # Second way to print variables for debugging  
  8.   - name: debug2  
  9.     debug:  
  10.         var: hostvars[inventory_hostname]  
  11.     tags: debug_info2  
Executing this task is going to dump all your variables:
# ansible-playbook debug-print-variables/test2.yaml --extra-vars '{"MY_VAR":"TEST"}' --tags "debug_info2" | grep MY_VAR
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
"MY_VAR": "TEST",


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...