当前位置:网站首页>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, toml

If 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.com

The 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 :

image-20220404210014398

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

image-20220404210028425

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 :

image-20220404210106160


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 :

image-20220404210134791

be based on yaml Static manifest file :

image-20220404210151639

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 :

image-20220404210215540

be based on yaml Static manifest file :

image-20220404210238266


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 :

image-20220404210257039

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

image-20220404210320638

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 .

image-20220404210444971

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 :

image-20220404210507577


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 yes

A floating point value and a string :

temperature: 36.5       # Floating point valueversion: "2.0"           # String containing a dot

1.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 ping

Clear 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 】!

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091818084777.html