"Ansible authoritative Guide" note (2)--inventory configuration

Source: Internet
Author: User
Tags vars ssh port

Iv. Configuration of Inventory
Ansible defines hosts and groups through inventory, allowing time to be read through-I, default/etc/ansible/hosts. Multiple inventory can exist, enabling dynamic generation.
1. Define hosts and Groups
# vim/etc/ansible/hosts
192.168.12.22 #可以直接为IP地址
Nfs.magedu.com #可以是域名
ntp.magedu.com:2200 #可以: Connect to SSH Port

[webserver] #[] is a group name, the following are the group members
web[1:10].magedu.com #[1:10] = 1~10 all numbers
db-[a:f].magedu.com #[a:f] = a~f all Letters

2. Define host variables
Variables defined can be used in playbook, and variables set in the playbook with the same name will take precedence over the variables.
other1.example.com ansible_connection=ssh Ansible_ssh_user=mpdehaan #选择连接类型和连接用户
Other2.example.com http_port=8800 #定义http_port端口号8800

3. Define Group variables
[Test]
Web1.example.com
Web2.example.com

[Test:vars] #组变量, the variables defined below are common to all hosts within the test group
Ntp_server=ntp.example.com
Proxy=proxy.example.com

4. Use one group as a child of another group
[Apache]
Web1.example.com
[Nginx]
Web2.example.com
[Webserver]
Other1.example.com
[Webserver:children]
Apache
Nginx
#上例中webserver包括web1. example.com, web2.example.com, other1.example.com

5. Other inventory parameters
Ansible_ssh_host
The name of the remote host that will be connected. This variable can be used to set the alias of the host you want to set.

Ansible_ssh_port
SSH port number. This variable is set if it is not the default port number.

Ansible_ssh_user
The default SSH user name

Ansible_ssh_pass
SSH password (This method is not secure, we strongly recommend using--ask-pass or SSH keys)

Ansible_sudo_pass
sudo password (this method is not secure, we strongly recommend using--ask-sudo-pass)

Ansible_sudo_exe (New in version 1.8)
sudo command path (for version 1.8 and above)

Ansible_connection
The type of connection to the host. For example: local, SSH, or Paramiko. Ansible 1.2 By default using paramiko.1.2 after the default use of ' smart ', ' smart ' method will be based on whether to support Controlpersist, to determine whether the ' SSH ' mode is feasible.

Ansible_ssh_private_key_file
The private key file that is used by SSH. For cases where there are multiple keys and you do not want to use an SSH proxy.

Ansible_shell_type
The shell type of the target system. By default, command execution uses the ' SH ' syntax, which can be set to ' csh ' or ' fish '.

Ansible_python_interpreter
The python path of the destination host. Applicable: There are multiple python in the system, or the command path is not "/usr/bin/python", such as \*bsd, or/usr/bin/python
This is not a 2.X version of Python. We do not use the "/usr/bin/env" mechanism because this requires the remote user's path to be set correctly and requires that the "Python" executable name is not a name other than Python (it may actually be named Python26).

As with Ansible_python_interpreter, you can set paths like Ruby or perl ....

6, the variable read four positions
Inventory configuration
Areas defined in VARs in playbook
Files in the VARs directory in roles
Roles files in the Group_vars and Hosts_vars directories of the sibling directory
#设置变量时尽量沿用同一种方式.

7, Ansible Regular
(1) Full volume match all with * function, but * need to be caused.
Ansible all-m Ping
Ansible "*"-M ping

(2) logical OR (or) matching
Simultaneous execution of multiple hosts or groups
Ansible "Web1:web2"-M ping

(3) logical non (!) The
All hosts in the Web1 group, but not in the WEB2 group
Web1:!web2

(4) Logic and (&) matching
Hosts that exist in Web1 and WEB2
Web1:&web2

(5) Fuzzy matching
Check the 192.168.1.0/24 network segment for all host survival status.
Ansible 192.168.1.*-M Ping
All groups beginning with test
Ansible "test*"-M ping

(6) Domain cut, with Python string domain cut
Cases:
[Webservers]
Web1.example.com
Web2.example.com
Web3.example.com

Webservers[0] #==web1.example.com
WEBSERVERS[-1] #==web3.example.com
Webservers[0:2] #第一位到第三位 ==web1.example.com, web2.example.com, web3.example.com
Webservers[1:] #第二位到最后 ==web2.example.com, web3.example.com

(7) Regular match, "~" starts to indicate regular match
Ansible "~ (web|data|test) \.example\. (com|org) "-M ping

"Ansible authoritative Guide" note (2)--inventory configuration

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.