顯示具有 Ansible 標籤的文章。 顯示所有文章
顯示具有 Ansible 標籤的文章。 顯示所有文章

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",


2020年2月13日 星期四

[Ansible 文章收集] How to check if a file/directory exists in Ansible

Source From Here
Preface
To get the details of a file or directory in a Linux system, we can use the Ansible stat module. It works similar to the Linux ‘stat’ command. Of course, the module provides much more detail than whether a file exists or not. We can know when the file was last modified, what all permissions the invoking user has about the file, whether it is a directory etc.

If you just need to check the status of the file to create a new file, then you should use the file module. It will create a file or directory only if the file does not exist. I will show you an example below. For more details on how to create directories in Ansible, you can refer this post.

The stat module needs the path of the file, directory or symlink to be checked at the minimum. The stat module returns all the details regarding the object specified in the path parameter. We need to store this in a register. Then using the return values and the ‘when’ condition statements, we can check various properties of the files.

Checking if the file exists or not exists
Let’s see a primary example of how to check if an object, file/directory, exists using the stat module.

In the following example, I am checking the status of ‘prompt.yaml’ file. I am storing the details of the stat module execution in a register named ‘file_details’. The module returns a lot of return values, and one of them is ‘exists’. It will return a boolean value, true or false, depending on the status of the file.
  1. - hosts: all  
  2.   tasks:  
  3.   - name: Ansible check file exists example.  
  4.     stat:  
  5.       path: /Users/mdtutorials2/Documents/Ansible/prompt.yaml  
  6.     register: file_details  
  7.   
  8.   - debug:  
  9.       msg: "The file or directory exists"  
  10.     when: file_details.stat.exists  
  11.     # check file not exist as below  
  12.     # when: not file_details.stat.exists   
The above example will give the output whether it is a file or directory or symlink. So how to check if a particular object exists and it is not a directory or symlink. We can use the ‘isreg‘ return value for checking if its a return value. It will be true if the path is a regular file.
  1. - debug:  
  2.     msg: "Ansible file exists example"  
  3.   when: file_details.stat.exists and file_details.stat.isreg  
Another way is to combine the ‘isdir‘ and ‘islnk‘ return values. The below task shows how it can be done.
  1. - debug:  
  2.      msg: "It is a file"  
  3.    when: file_details.stat.exists and file_details.stat.isdir == false and file_details.stat.islnk == false  
Checking if a directory exists
For checking if a particular object is a directory and also it exists you can combine the ‘exists’ and ‘isdir’ return values. The ‘isdir’ value will be true if the object is a directory and else false. For example, in the below case I have given the path for ‘test1’ object. If it is not a directory, the task will be skipped.
  1. - hosts: all  
  2.   tasks:  
  3.   - name: Ansible check directory exists example.  
  4.     stat:  
  5.       path: /Users/dnpmacpro/Documents/Ansible/test1  
  6.     register: files_to_delete  
  7.   
  8.   - debug:  
  9.       msg: "It is a directory"  
  10.     when: files_to_delete.stat.exists and files_to_delete.stat.isdir  
Supplement
FAQ - How can I run a ansible task only if a file or directory does NOT exist?

[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...