Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Inventory and Patterns of Ansible

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Inventory and Patterns of Ansible". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Inventory and Patterns of Ansible".

The Inventory file of Ansible can be understood as a list of all minion in salt-key in saltstack and the concept of user-defined nodegroup. By default, this file is / etc/ansible/hosts, and Dynamic Inventory will be discussed later. This section focuses on the static host cluster section. The Patterns (pattern) part can be understood as a regular expression, and through Patterns we can match some hosts in the Inventory packet.

1. Hosts and Groups (hosts and groups)

The simplest definition format for / etc/ansible/hosts is like the following:

1. Simple hosts and groups

Mail.361way.com[webservers] web1.361way.comweb2.361way.com [dbservers] db1.361way.comdb2.361way.com

The names in parentheses in * * an and * * represent group names. You can divide large hosts into identified groups according to your own needs. For example, I have two groups, webservers and dbservers, above.

* * b and * * hosts parts can be represented by domain name, host name and IP address. Of course, when using the first two, the host also needs to be able to reverse resolve to the corresponding IP address. Generally, IP address is often used in this configuration.

2. Port and alias

If the SSH of some hosts is running on custom ports, ansible will not use the ports listed in your SSH configuration file when using Paramiko for ssh connection, but if you modify ansible to use openssh for ssh connection, it will use:

192.168.0.10:5309

If you want to set some aliases for some static IP, it is similar to the parameter configuration of id in the minion configuration file in SaltStack. You can do this:

Jumper ansible_ssh_port = 5555 ansible_ssh_host = 192.168.1.50

The jumper alias above refers to the host whose IP is 192.168.1.50 and whose connection port is 5555.

3. Specify the host range

[webservers] www [01:50] .361way.com [databases] db- [a:f]. 91it.org

The above specifies a total of 50 hosts from web1 to web50,webservers group, and six hosts from db-a to db-f in databases group.

4. Use host variables

Here are the variables that are often used in the Hosts section

Ansible_ssh_host # Hostname to connect ansible_ssh_port # Port number defaults to 22ansible_ssh_user # user name used by default for ssh connection ansible_ssh_pass # password for ssh connection ansible_sudo_pass # use sudo to connect the user's password ansible_ssh_private_key_file # key file if you don't want to use ssh-agent management, you can use this option ansible*shell*type # shell type Default type of shansible_connection # SSH connection: local Ssh, paramiko defaults to paramiko before ansible 1.2 Later, the intelligent choice is to give priority to the use of ControlPersist-based ssh (supported premise) ansible_python _ interpreter # to specify the path of the python interpreter, as well as the path of ruby and perl

Examples are as follows:

[test] 10.212.52.252 ansible_ssh_user=root ansible_ssh_pass='361way.com'10.212.52.14 ansible_ssh_user=test1 ansible_ssh_pass='91it.org'10.212.52.16 ansible_ssh_user=test2 ansible_ssh_port=7788 ansible_ssh_pass='123456'

In the above example, three hosts are specified. The passwords of the three hosts are 361way.com, 91it.org, 123456, and the user names of the specified ssh connections are root, test1, and test2,ssh ports of 22, 22, and 7788, respectively. In this way, there is no need to instruct the user and password when the ansible command is executed. The execution result is as follows:

[root@361way.com ~] # ansible test-a 'uptime'10.212.52.252 | success | rc=0 > > 01:34am up 23 days 10:57, 2 users, load average: 0.442,0.39,0.4110.212.52.16 | success | rc=0 > > 01:41am up 331days 8:33, 2 users, load average: 0.00,0.01,0.0510.212.52.14 | success | rc=0 > > 01:40am up 331 days 7:55, 2 users, load average: 0.09,0.03,0.05

5. Intra-group variables

Variables can also be applied to all members of the group through the group name:

[test] host1host2[test: vars] ntp_server=ntp.361way.comproxy=proxy.361way.com

The above test group contains two hosts. By specifying the vars change to the test group, the corresponding host1 and host2 are equivalent to the corresponding specified ntp_server and proxy variable parameter values.

6. Group inclusion and intra-group variables

[hangzhou] host1host2[jiaxing] host2host3[zhejiang: children] hangzhoujiaxing[zhejiang: vars] some_server=foo.southeast.example.comhalon_system_timeout=30self_destruct_countdown=60escape_pods= 2[china: children] zhejianghenanshandonghebei

As in the example above, I specify Hangzhou group, I have host1, hosts2; Jiaxing group, I have host3 and host4 hosts; I specify another group Zhejiang group, which includes both Hangzhou and Jiaxing; and I specify four vars variables for all hosts in this group. Later, I set up a group of Chinese groups, including Zhejiang, Henan, Shandong and Hebei.

Note: since the vars variable is basically not used in the ansible ad-hoc section, it is mainly used in ansible-playbook and will be mentioned in later sections.

The above part is basically in full accordance with the official Inventory document part of the translation and minor changes. If you feel good in English, you can check the official page directly.

2. Patterns (regular matching part between host and group)

It is not entirely accurate to understand Patterns directly as regular. Normally, it means which hosts to manage in ansible and which hosts to communicate with. Before we explore this issue, let's take a look at the use of ansible:

Ansible-m-a

Go straight to the previous example:

Ansible webservers-m service-a "name=httpd state=restarted"

Here is to restart the httpd service for the webservers group or host, where webservers is the Pattern part. The reason why I said Pattern (pattern) above can be understood as regular, mainly for the following frequently used usage.

1. Indicates that all hosts can use all or *

2. Wildcards and logical OR

Wildcards can also be used to specify a set of host or host names with regular characteristics, with colons indicating or--- logic or

One.361way.comone.361way:two.361way.com192.168.1.50192.168.1.*

Of course, the * wildcard here can also be used in front, such as:

* .361way.com*.com

The above usage also applies to multiple groups, such as:

Webserverswebservers:dbservers / / represents all hosts in both groups

3. Logical non-and logical and

Of course, you can make negative expressions, for example, the target host must be in the group webservers but not in the phoenix group.

Webserver:!phoenix

You can also make an intersection expression, for example, the target host must be in both group webservers and group staging

Webservers:&staging

A more complex example:

WebserverRose dbserversRose stagingRose Phoenix

At the end of the complex expression above, the target host must satisfy: in the webservers or dbservers group, it must still exist in the staging group, but not in the phoenix group. These can be thought of as Compound matchers in SaltStack.

4. Mixed advanced usage

* .361way.com:*.org

You can also use "~" at the beginning to indicate that this is a regular expression:

~ (web | db). *\ .91it\ .org

At this point, you should be able to understand why I mentioned earlier that Patterns can be understood as regular. The last part gives two specific possible uses in ansible-playbook: a, in the ansible-palybook command, you can also use variables to form such an expression, but you must use the option of "- e" to specify the expression (which we don't usually use):

Ansible-palybook-e webservers! {{excluded}}: & {{required}}

B. In ansible and ansible-playbook, you can also explicitly specify the exclusion of certain hosts or groups with a parameter "- limit":

Ansible-playbook site.yml-limit datacenter2 Thank you for your reading, the above is the content of "how to use Inventory and Patterns of Ansible". After the study of this article, I believe you have a deeper understanding of how to use Inventory and Patterns of Ansible, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report