当前位置:网站首页>Automatic operation and maintenance 4 - variables and encryption in ansible

Automatic operation and maintenance 4 - variables and encryption in ansible

2022-06-21 13:38:00 Zhaohui_ Zhang

Catalog

One 、 Variable naming

Two 、 Variable level

3、 ... and 、 Variable setting and usage

1、 Using variables

2、 stay playbook Directly define variables in

3、 Define variables in the file

4、 Set host variables and manifest variables

5、 Directory setting variables

6. Override variable with command

7. Use an array to set variables

8. Register variables

9. Fact variables ——ansible_facts

10. Magic variable

Four 、JINJA2 Templates

j2 Template writing rules

5、 ... and 、Ansible Encryption control


One 、 Variable naming

It can only contain numbers , Underline , Letter
Can only start with an underscore or a letter  

Two 、 Variable level

overall situation Set from the command line or configuration file
host By list , The task of collecting or registering facts

paly

stay play And the... Set in the related structure

Variable priority setting : Narrow areas take precedence over wide areas (play > host > overall situation )

3、 ... and 、 Variable setting and usage

1、 Using variables

 

2、 stay playbook Directly define variables in

  1 ---
  2 - name: test yml                # Project name 
  3   hosts: westos                 # Remote host list 
  4   vars:                         # Defining variables 
  5     NAME: zzh
  6   tasks:
  7     - name: create user         # Define subprojects 
  8       user:
  9         name: "{
   {NAME}}"        # Using variables 

usage : 

3、 Define variables in the file

  1 ---
  2 - name: test yml
  3   hosts: westos
  4   vars_files: user.yml            # File to call variables 
  5   tasks:
  6     - name: create user
  7       user:
  8         name: "{
   {NAME}}"

usage : So let's create one <user.yml> file , Write the variable name in the file . Call the variables in the file as follows in the script .

 

4、 Set host variables and manifest variables

  1 ---
  2 - name: test yml
  3   hosts: test                    # The variable to be specified is in the Group 
  4   tasks:
  5     - name: create user
  6       user:
  7         name: "{
   {NAME}}"

Add variables to the list , The variable is only valid for specific groups . How to write it :[ Group name :vars]

usage :

5、 Directory setting variables

group_vars Manifest variables , In the directory The file name is consistent with the host list name
host_vars Host variables , In the directory The file name is consistent with the host name

   Method 1 :

Method 2 :

6. Override variable with command

Be careful : This method has the highest priority

ansible-playbook test.yml -e "NAME=westos_zzh"

7. Use an array to set variables

fit Multiple variables The definition of

  1 ---
  2 - name: test yml
  3   hosts: test
  4   vars_files: ./user.yml                    # Specify variable storage file 
  5   tasks:
  6     - name: show vars                       # subprojects 1
  7       debug:                                
  8         var: USERLIST['lee']['age']         # Show USERLIST Under the lee Of age The variables in the 
  9 
 10     - debug:                                # subprojects 2
 11         msg: "{
   {USERLIST['westos']}}"       # Show USERLIST Under the westos Partial variables 

  Create a file to store variables in the same directory .

Show multiple variables  

8. Register variables

register Put the module's Output Capture and write to the specified character string in .

  1 ---
  2 - name: test register
  3   hosts: test
  4   tasks:
  5     - name: hostname command
  6       shell:
  7         hostname
  8       register: info                        # Store the output of the subproject as info In the list 
  9 
 10     - name: show messages
 11       debug:
 12         msg: "{
   {info['stdout']}}"            # from info Select output... From the list “stdout” Variable 
 13 
 14     - name: show rc                          # from info Select output... From the list “rc” Variable 
 15       debug:
 16         var: info['rc']                              

9. Fact variables ——ansible_facts

The following commands can be used to collect the factual information of the controlled host :

ansible 172.25.254.151 -m setup 

The fact variable is ansible Variables automatically detected in the controlled host ;
There is also host related information in the fact variable ;
When it is necessary to use host related information, it is not necessary to collect data , Just call it directly ;
Because the variable information is system information Do not set at will For information collection only , So it is called a fact variable .

  1 ---
  2 - name: test register
  3   hosts: test
  4   tasks:
  5     - debug:
  6         var: ansible_facts['enp1s0']['ipv4']['address']    # Filter the data under the fact variable 

Save the value of the variable :

  1 ---
  2 - name: test register
  3   hosts: test
  4   tasks:
  5     - lineinfile:
  6         path: /mnt/address
  7         line: "{
   { ansible_facts['enp1s0']['ipv4']['address']}}"
  8         create: yes

  preservation enp1s0 Network card ipv4 Address to the controlled host </mnt/address> In file

  Be careful :  gather_facts: no  ## stay playbook Turn off fact variable collection in

This parameter will speed up execution , However, fact variables will not be collected, resulting in inaccurate results or even errors .

10. Magic variable

hostvars:ansible Internal information of the software
group_names: The currently managed host is in the same group
groups: List all groups and hosts in the list
inventory_hostname: Contains the name of the currently managed host configured in the manifest

The magic variable is ansible Internal variables of the host

ansible localhost -m debug -a 'var=hostvars'        # Show ansible Internal information of the software 

ansible test -m debug -a 'var=groups'                # List all groups and hosts in the list 
ansible test -m debug -a 'var=inventory_hostname'    # Contains the name of the currently managed host configured in the manifest 
ansible test -m debug -a 'var=group_names'           # The currently managed host is in the same group 

 

Four 、JINJA2 Templates

Jinja2 yes Python The next widely used template engine . His design idea comes from Django Template engine for , It also extends its grammar and a series of powerful functions . One of the most significant is the addition of sandbox execution function and optional automatic escape function

j2 Template writing rules

test :

~~~~

test :

~~~~~

experiment :

test :

5、 ... and 、Ansible Encryption control

practice :

In all clients </mnt> Next build one “hosts” file , The content of the document and </etc/hosts> The contents of the file in are the same .

Summary of this chapter

~~~~~~j2 The content after the template has not been completed due to time ~~~~~~ Ongoing update ~~~~~~

原网站

版权声明
本文为[Zhaohui_ Zhang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221437485606.html