当前位置:网站首页>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
3、 ... and 、 Variable setting and usage
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
9. Fact variables ——ansible_facts
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 ~~~~~~
边栏推荐
- Setting of Seaborn drawing style
- Is it safe to open a securities account by downloading the app of qiniu business school? Is there a risk?
- 3000 frame animation illustrating why MySQL needs binlog, redo log and undo log
- Explanation of common mesh generation methods in workbench
- 3. function improvement
- 【深入理解TcaplusDB技术】Tmonitor系统升级
- seaborn数据总体分布的可视化策略
- 使用seaborn绘制热图
- Kubernetes快速實戰與核心原理剖析
- 3D slicer saves segmentation results
猜你喜欢

Nanjing University static program analyses -- Introduction learning notes

Unbounded territory won the title of innovative brand of digital culture industry in 2022

Swift return button

【深入理解TcaplusDB技术】TcaplusDB业务数据备份

小程序直播互动功能运行在App里?
Collection reference type in JS

Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes for Dense Object Detection

分布式事务,原理简单,写起来全是坑

【深入理解TcaplusDB技术】TcaplusDB导入数据

Cvpr2022 | the action sequence verification task was first proposed by X xiaohongshu of Shanghai University of science and technology, which can be applied to multiple scenarios such as scoring of spo
随机推荐
Artifacial Intelligent Project
如何阅读AI顶会论文?
MySQL - view properties
7. pointer
对app和微信小程序进行接口测试
6. functions
8. structure
[in depth understanding of tcapulusdb technology] tcapulusdb construction data
MySQL constraints (descriptions of various conditions when creating tables)
Reading notes on how to connect the Internet ADSL
Are you still using generator to generate crud code of XXX management system? Let's see what I wrote
MySQL - built in functions
Detailed explanation and examples of common parameters of curl
Babbitt yuancosmos daily must read: wechat may ban a official account for the first time on the grounds of "involving secondary transactions in digital collections", and the new regulations of the pla
PHP uses grafika to synthesize pictures and generate poster images
3. function improvement
Master the basic usage of SQLite3
5000 word analysis: the way of container security attack and defense in actual combat scenarios
Work content of service governance
【深入理解TcaplusDB技术】TcaplusDB构造数据