Ansible2: Host list "Go"

Source: Internet
Author: User
Tags vars ssh port

Ansible by reading the default host manifest configuration/etc/ansible/hosts, you can simultaneously connect to multiple remote hosts to perform tasks, and the default path can specify the path by modifying the Hostfile parameter of ansible.cfg.

One, hosts and Groups (host and group)

The simplest definition format for/etc/ansible/hosts is as follows:

1. Simple hosts and groups
Mail.yanruogu.com
[Webservers]
Web1.yanruogu.com
Web2.yanruogu.com
[Dbservers]
Db1.yanruogu.com
Db2.yanruogu.com

A, the name in brackets for the group name, you can according to their own needs to the large host into a group with identification, such as the above divided into two groups webservers and Dbservers Group;

b, the host (hosts) section can use the domain name, hostname, IP address, of course, the use of the first two, the host can also be resolved to the corresponding IP address, generally such configuration more use of IP address;

2. Ports and aliases

If some hosts have SSH running on a custom port, Ansible will not use the ports listed in your SSH profile when using Paramiko for SSH connections, but if you modify the ansible using OpenSSH for SSH connections:

192.168.1.1:3091

If you want to set some aliases for some static IP, you can do this:

Web1 Ansible_ssh_port = 3333 Ansible_ssh_host = 192.168.1.2

The Web1 alias above refers to the host with IP 192.168.1.2,ssh connection port 3333.

3. Specify the host range
[Webservers]
Www[01:50].yanruogu.com
[Databases]
Db-[a:f].yanruogu.com

The above specifies a total of 50 hosts from the Web1 to the Web50,webservers group, and the databases group has db-a to db-f a total of 6 hosts.

4. Using Host variables

The following are some of the variables that are commonly used in the hosts section:

Ansible_ssh_host #用于指定被管理的主机的真实IP
Ansible_ssh_port #用于指定连接到被管理主机的ssh端口号, default is 22
Ansible_ssh_user #ssh连接时默认使用的用户名
Ansible_ssh_pass #ssh连接时的密码
Ansible_sudo_pass #使用sudo连接用户时的密码
Ansible_sudo_exec #如果sudo命令不在默认路径, you need to specify the sudo command path
Ansible_ssh_private_key_file #秘钥文件路径, secret key file you can use this option if you do not want to use Ssh-agent management
Ansible_shell_type #目标系统的shell的类型, default sh
Ansible_connection #SSH type of connection: local, SSH, Paramiko, Paramiko by default before Ansible 1.2, then smart selection, preference for controlpersist-based SSH (Prerequisites for support)
Ansible_python_interpreter #用来指定python解释器的路径, the default is/usr/bin/python can also specify the path of Ruby, Perl
Ansible_*_interpreter #其他解释器路径, usage is similar to Ansible_python_interpreter, where "*" can be in other languages such as Ruby or Perl

Examples are as follows:

[Test]
192.168.1.1 ansible_ssh_user=root ansible_ssh_pass= ' [email protected] '
192.168.1.2 ansible_ssh_user=breeze ansible_ssh_pass= ' 123456 '
192.168.1.3 Ansible_ssh_user=bernie ansible_ssh_port=3055 ansible_ssh_pass= ' 456789 '

The above example specifies three hosts, three hosts with the password is [email protected], 123456, 45789, the specified SSH connection user name is root, Breeze, bernie,ssh Port respectively is 22, 22, 3055, This way, when the ansible command executes, it is no longer necessary to instruct the user and password.

5. Variables within the group

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

[Test]
Host1
Host2
[Test:vars]
ntp_server=192.168.1.10
proxy=192.168.1.20

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

6. Group inclusion and intra-group variables
[Wuhan]
Web1
Web2
[Suizhou]
Web4
Web3
[Hubei:children]
Wuhan
Suizhou
[Hubei:vars]
ntp_server=192.168.1.10
zabbix_server=192.168.1.10
[China:children]
Hubei
Hunan

In the above example, the Wuhan group has the Web1 and WEB2; the Suizhou group has WEB3, WEB4 host, and a Hubei group, including Wuhan and Suizhou; at the same time, 2 VARs variables are specified for all hosts in the group. Set up a group of Chinese group, including Hubei, Hunan.

Note: VARs variables in the ansible ad-hoc part of the basic use, mainly used in Ansible-playbook.

Second, Patterns (host and group regular matching part)

It is not entirely accurate to interpret patterns directly as regular, and the normal understanding is that patterns means which hosts are managed in Ansible and which hosts to communicate with. Before we go into this question, let's look at the usage of ansible:

Ansible <pattern_goes_here> M <module_name>-a <arguments>

Direct previous example:

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

Here is the Webservers group or host restart httpd service, where webservers is the pattern part. The reason for this is that pattern (pattern) can be understood as a regular, mainly for the use of the following commonly used in terms of.

1, indicates that all hosts can use all or * 2, wildcards and logic or

Wildcard characters can also be used to specify a set of host or host names with a rule feature, which represents or---logic or

Web1.yanruogu.com
Web1.yanruogu.com:web2.yanruogu.com
192.168.1.1
192.168.1.*

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

*.yanruogu.com
*.com Webservers1[0] #表示匹配 the 1th host of the WEBSERVERS1 group Webservers1[0:25] #表示匹配 webservers1 group 1th to 25th host (official website document is ":" indicates scope, test discovery should use "-", note not to be confused with matching multiple host groups)

The above usage is equally applicable across multiple groups, such as:

Webservers
Webservers:dbservers #表示两个组中所有的主机
3. Logical non-logical AND

Non-expression, such as the target host must be in group webservers but not in the Phoenix Group

Webserver:!phoenix

An expression of the intersection, such as the target host must be in group Webservers and in group staging

Webservers:&staging

A more complex example:

Webserver:dbservers:&staging:!phoenix

The target host that the above complex expression finally represents must be met: in the webservers or dbservers group, it must also exist in the staging group, but not in the Phoenix Group.

4. Mixed Advanced usage
*.yanruogu.com:*.org

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

~ (web|db). *\.yanruogu\.com

Specific possible uses for the two ansible-playbook:

A, in the Ansible-palybook command, you can also use variables to form such expressions, but you must use the "-e" option to specify the expression (which we do not normally use):

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

b, in Ansible and Ansible-playbook, you can also explicitly specify to exclude certain hosts or groups by a parameter of "--limit":

Ansible-playbook site.yml--limit Datacenter2
C, starting from Ansible1.2, if you want to exclude the host in a file can use "@":
Ansible-playbook site.yml--limit @retry_hosts. txt

Turn from

This article is from the "Nobody" blog, please be sure to keep this source http://breezey.blog.51cto.com/2400275/1757643

Ansible2: Host list "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.