Ansible detailed explanation (18) - Ansible use tips

Today, I will continue to introduce you to the relevant knowledge of Linux operation and maintenance. The main content of this article is the use of Ansible tips.

1. Playbook variable priority

In Ansible Detailed Explanation (8) - Ansible playbook variables , we introduced the definition and declaration methods of five playbook variables. There are five methods including using the gather facts
module to obtain variables, passing in variables through the -e parameter when calling playbook, defining variables in the hosts file, defining variables in the playbook, and independent variable files.
So, when using these 5 methods to pass in a variable with the same name at the same time, which variable will playbook use? This is a problem of variable priority in an Ansible Playbook.
We can simply experiment with a script as follows:

---
- hosts: exp
  vars_files:
   - ./vars.yml
  vars:
   ansible_all_ipv4_addresses: vars_through_playbook
  tasks:
  - name: Test the priority of the ansible variable
    shell: echo {
    
    {
    
     ansible_all_ipv4_addresses }} > /root/234.txt

In this script, we try to define the variables of ansible_all_ipv4_address in these four ways, and assign different values ​​to these variables, and then check which variable's value is written to the file.
After experiments, it is concluded that the priority of variables in ansible playbook is as follows:
assign value through -e command > assign value through independent variable file > assign value in playbook > obtain through gather facts module > assign value through hostfile file.

Second, the command alias problem

In Linux systems, the alias command is supported to define some aliases. Common aliases in Linux systems are as follows:
insert image description here
However, Ansible does not support the use of aliases when controlling downstream devices. Whether it is in the form of direct command control or writing commands into the playbook. Whether the downstream device has a corresponding alias or the local ansible device has a corresponding alias. Taking the common ll command as an example, the result of executing the ll command on the command line is as follows:
insert image description here
Therefore, when we use Ansible, we should pay special attention to avoid using the alias of the command.

3. Name multitasking in ansible

For ansible, when using the shell module, semicolon intervals can be used to execute multiple commands on the client side.
If you use ansible playbook, you can write multiple commands under one name task, as follows:
insert image description here

Fourth, ansible command execution directory problem

When ansible executes a task, if the root user is used to log in, the default directory is /root, and if another user is used to log in, the logged-in directory is the user's home directory. However, in the actual use of ansibke, when specifying files, absolute paths are generally used for the sake of safety.
Originality is not easy, please indicate the source for reprinting: https://blog.csdn.net/weixin_40228200

Guess you like

Origin blog.csdn.net/weixin_40228200/article/details/123505050