当前位置:网站首页>Goldfish rhca memoirs: do447 management list -- compile yaml list file
Goldfish rhca memoirs: do447 management list -- compile yaml list file
2022-06-09 18:26:00 【Hua Weiyun】
Personal profile : Hello everyone , I am a Brother goldfish ,CSDN New star creator in operation and maintenance field , Hua Wei Yun · Cloud sharing experts , Alicloud community · Expert bloggers
Personal qualifications :CCNA、HCNP、CSNA( Network Analyst ), Soft test primary 、 Intermediate network engineer 、RHCSA、RHCE、RHCA、RHCI、ITIL
Maxim : Hard work doesn't necessarily succeed , But if you want to succeed, you must work hard
1.1 Listing plug-ins
from Ansible 2.4 Start ,Ansible Directory files supporting different formats are implemented in the form of plug-ins . Plugins are enhancements Ansible Code snippets of functions . Implement manifest support through plug-ins ,Ansible You can provide a new plug-in to support new formats and methods for generating inventory data . Conventional ini Style static manifest file and dynamic manifest script are implemented through a plug-in .
Most manifest plug-ins are disabled by default . You can be in your ansible.cfg Enable specific plug-ins in the configuration file , stay inventory Part of the enable_plugins In the instruction :
[inventory]enable_plugins = host_list, script, auto, yaml, ini, tomlIf you don't specify enable_plugins Instructions , The list in the above example is the default . When Ansible Analyze the source of the list . It will try to follow enable_plugins The order in which the instructions appear uses each plug-in .
The script plug-in provides support for standard dynamic list scripts .ini The plug-in provides support for the standard ini Support for static manifest file format . Other plug-ins get directory information from files in other formats or other sources .
Details can be found at https://docs.ansible.com/ansible/latest/plugins/inventory.html obtain .
Some follow Ansible The manifest plug-in released together provides a standardized replacement for dynamic manifest scripts . for example ,openstack Can get Red Hat openstack Virtual machine information in platform environment ,aws ec2 It can be downloaded from Amazon Web Services ec2 Get cloud instance information .
important :
When should dynamic list scripts be used , When should I use the manifest plugin ?
One of the benefits of using the plug-in with manifest is , If you have a plug-in available in your use case , It is with the core Ansible The code is maintained together . You can write your own manifest plug-ins , But it must be used like most plug-ins Python Realization .
Please visit https://docs.ansible.com/ansible/latest/dev_quide/developing_inventory.html.
You can also continue to use your own dynamic list script , The plug-in system will continue to support these scripts . These scripts only need to be executable files , So you can write them in any programming language you like .
1.2 Yaml Static manifest file
You can use yaml inventory Plug in based on yaml Instead of based on ini Write a static manifest file with the syntax of . This plug-in is enabled by default . Why create YAML inventory plug-in unit , Because it's easy for users to read , Easy for software analysis , And allows you to YAML For script 、 Variable files and lists .
for example :
[lb_servers]servera.lab.example.com[web_servers]serverb.lab.example.comserverc.lab.example.com[backend_server_pool]server[b:f].lab.example.comThe following is the same YAML Format manifest file :
lb_servers: hosts: servera.lab.example.com:web_servers: hosts: serverb.lab.example.com: serverc.lab.example.com:backend_server_pool: hosts: server[b:f].lab.example.com:YAML Listing uses blocks to organize related configuration items . Each block is marked with a group name and a colon (:) start , Everything indented under the group name belongs to this group .
If you indent below the group name , So keywords hosts The hostname block will be started . All server names indented under the host belong to this group . These servers themselves form the group to which they belong , So they have to use colons (:) end .
You can use keywords in chunks children. This keyword will start the list of groups that are members of this group . These members can have their own hosts and children block .
YAML Grammar is better than INI One advantage of grammar is , It organizes the server list and nested group list in the same location of the static manifest file .
important :
all Groups exist implicitly at the top level , And include the rest of the listing as its child elements . You can be in your YAML It is clearly listed in the list file , But not necessarily :

Some are based on ini The static list of includes hosts that are not members of any group .

Ansible Automatically place any of these hosts in ungrouped In a special group . Based on yaml In the static list of , You can explicitly assign hosts to ungrouped To complete :

1.3 Set manifest variables
Can be based on yaml Directly set the manifest variable in the manifest file of , It's like based on inil As in the manifest file .
Be careful :
in many instances , The best practice is to avoid storing variables in static manifest files .
Many experienced Ansible Developers prefer to use static manifest files to store information about which hosts are managed and which groups they belong to . Variables and their values are stored in the of the manifest host_vars or group_vars In file .
However , In some cases , You may wish to ansible_port or ansible_connection Such variables are saved in the same file as the list itself . To keep the information in one place . If you set variables in too many different places , It's hard to remember where a particular variable is set .
In chunks , have access to vars The key word in YAML Set group variables directly in the manifest file . for example , Based on ini In the static manifest file , Can be smtp_relay Set the variable to the value smtp.lab.example.com Used to monitor all hosts in the group , As shown below :

be based on yaml Static manifest file :

You can set the host variable to YAML Items indented under the host in the manifest file . for example , Based on ini In the static manifest file , The host can be localhost Of ansible_connection The variable is set to local, As shown below :

be based on yaml Static manifest file :

1.4 from INI The switch to YAML
have access to ansible-inventory Command help will be based on ini The list of is converted to YAML Format . However , This is not the intended use of the tool . This tool is to show Ansible See the list of the whole configuration , And the results may be different from those reported in the original inventory file .ansible-inventory Format of command parsing and test manifest file , But it doesn't try to verify that the hostname in the manifest actually exists .
Be careful :
The name in the manifest file is not necessarily a valid host name , But by the playbook Use to refer to a specific managed host . The name of the managed host can have a ansible_host Host variable set , This variable points to ansible The real name or... That should be used when connecting to the managed host IP Address , And the name in the manifest file can be a simplified alias for script purposes .
for example , Suppose there is an initial ini Static list in format , Name it origin_inventory, As shown below :

In this case , You can use the following ansible-inventory To generate YAML Output .

Watch this output carefully , Something strange may be noticed .web Server's alternate_server Group variables are reported as host variables for two members of the group . Besides , Because the group backend server pool is the first of these two hosts . The value of this variable is reported there , Not in the group web The definition of those hosts in the server reports .
The converted list will produce the same output . please remember , Even if the group variable is based on web Membership settings in the server . Even if play The host is selected according to the membership in different groups , It is still set for the hosts in the Group .
important :
If the host sets one of these variables based on membership in the group , And the variable is not overwritten , So even if the host chooses it for the script based on membership in different groups , It will also be effective .
The problem with this behavior is , This is not how the original list works , This can cause confusion . To fix this difference , You must edit the generated YAML. under these circumstances , You will edit the destination list . Delete these two definitions , And then in web Create one under the server vars In this section , Copy variables from the server alternate_server The definition of . You must also change the original by backend_server_pool Server defined by the scope in , To reuse the scope , Not listed separately .

When converting very large manifest files , Use ansible-inventory Commands can save a lot of time , But it has to be used with caution . If the original static manifest file does not declare manifest variables directly , It will work better . But from host_vars and group_vars.
Be careful :
ansible-inventory Of YAML Some groups or host lines in the output are marked with {} end . Indicates that the group does not have any members or group variables , Or the host has no host variables . If you use YAML The format is written manually , You don't need to include these braces .
1.5 YAML Troubleshooting skills
If you are using YAML Trouble encountered in static manifest file of format , Then you should remember something about YAML Grammar things .
Colons and spaces
In a string without quotation marks , A colon followed by a space causes an error ,YAML It will be interpreted as starting a new element in the dictionary .
Here are some examples and how to solve these problems :

Variable of value
Ansible Use {{ variable }} Replace variable , however YAML China and Israel { Anything at the beginning is interpreted as the beginning of the dictionary . therefore , You must enclose the variable placeholder in double quotation marks :
foo: "{{ variable }} rest of the value".In general , When using any reserved characters [] {} > | * & ! % # ` @ , You should use double quotes around the value "".
Understand the difference between strings and Boolean or floating-point numbers
Boolean and floating-point numbers used as variable values should not be enclosed in quotation marks . Values enclosed in quotation marks are treated as strings .
for example , A Boolean value and a string :
active: yes # Boolean valuedefault_answer: "yes" # string containing yesA floating point value and a string :
temperature: 36.5 # Floating point valueversion: "2.0" # String containing a dot1.6 Textbook exercises
[[email protected] ~]$ lab inventory-yaml start
Modify as required yaml Host list
[[email protected] ~]$ cd DO447/labs/inventory-yaml/[[email protected] inventory-yaml]$ lltotal 4-rw-rw-r--. 1 student student 300 May 17 2019 inventory[[email protected] inventory-yaml]$ cp inventory inventory.yml[[email protected] inventory-yaml]$ cat inventory.yml active_web_servers: hosts: server[b:c].lab.example.com:inactive_web_servers: hosts: server[d:f].lab.example.com:region_eu: hosts: serverc.lab.example.com: serverf.lab.example.com:web_servers: children: active_web_servers: inactive_web_servers:all_servers: hosts: servera.lab.example.com: children: web_servers:test run
[[email protected] inventory-yaml]$ ansible -i inventory all_servers --list-hosts hosts (6): servera.lab.example.com serverb.lab.example.com serverc.lab.example.com serverd.lab.example.com servere.lab.example.com serverf.lab.example.com[[email protected] inventory-yaml]$ ansible -i inventory.yml all_servers -m pingClear the experiment
[[email protected] ~]$ lab inventory-yaml finish
summary
RHCA Certification requires experience 5 Study and examination of the door , It still takes a lot of time to study and prepare for the exam , Come on , Can GA 🤪.

That's all 【 Brother goldfish 】 Yes Chapter two Management list – To write YAML Inventory file Brief introduction and explanation of . I hope it can be helpful to the little friends who see this article .
Red hat Certification Column series :
RHCSA special column : entertain RHCSA authentication
RHCE special column : entertain RHCE authentication
This article is included in RHCA special column :RHCA memoir
If this article 【 article 】 It helps you , I hope I can give 【 Brother goldfish 】 Point a praise , It's not easy to create , Compared with the official statement , I prefer to use 【 Easy to understand 】 To explain every point of knowledge with your writing .
If there is a pair of 【 Operation and maintenance technology 】 Interested in , You are welcome to pay attention to ️️️ 【 Brother goldfish 】️️️, I will bring you great 【 Harvest and surprise 】!

边栏推荐
- 入駐快訊|歡迎XCHARGE智充科技正式入駐 InfoQ 寫作平臺!
- ElasticSerach
- CNN - nn.Conv1d使用
- Welcome to the InfoQ writing platform!
- Overview of GCN graph convolution neural network
- 深入理解JVM和GC
- 微信小程序根据经纬度获取省市区信息
- Squeeze and exception networks learning notes
- Interpretation of new shares | ranked second in the event content marketing industry, and wanted to push SaaS products on the cloud to create a competitive barrier
- CAM350检查gerber与钻孔文件
猜你喜欢

基于FPGA的SD卡读写设计及仿真verilog

Redis报错Field redisTemplate in xxx required a bean of type ‘org.x.RedisTemplate‘that can not be foud

WSL 挂载U盘

NLP-文本表示-词袋模型和TF-IDF

Cortex-a9 Samsung itop-4412 development board introduction embedded

Interpretation of new shares | ranked second in the event content marketing industry, and wanted to push SaaS products on the cloud to create a competitive barrier

Transformer模型新SOTA--fully attentional networks (FANs) 学习笔记

Explain MySQL index

Redis source code learning-01_ Debugging redis source code in clion

MySQL启动失败的原因是什么及如何解决
随机推荐
日常2琐事^_^
CAM350检查gerber与钻孔文件
Fastjson deserialization Remote Code Execution Vulnerability
redis源码学习-03_动态字符串SDS
线程中断
Redis source code learning-01_ Debugging redis source code in clion
kubectl 最新常用命令 --V1.24版本
空闲内存的管理
mysql版本驱动问题
Zhichong launched net zero series energy storage and charging all-in-one machine to create a net zero future with BYD
Cortex-a9 Samsung itop-4412 development board introduction embedded
如何让我们的自建站点拥有自定义域名(1)
Golang基础(5)
Golang Foundation (5)
贤弟单腾,因崔思婷,机器人类打字~~~~~~
Computer knowledge and optimization
图解|高性能服务器设计之缓存系统一致性
155_ Model_ Safety stock of power Bi & power pivot purchase, sales and inventory
Vulkan规范笔记(一) 第一章至第六章
AD PCB画图注意点