Ansible Study notes 1-inventory file

Source: Internet
Author: User
Tags ssh port

Inventory file


Ansible can operate concurrently with multiple hosts belonging to one group, the relationship between the combined hosts is configured through the inventory file, the default file path is/etc/ansible/hosts


In addition to the default files, you can use multiple inventory files at the same time, or you can pull inventory configuration information from a dynamic source or from the cloud

------------------------------------------------------------------------

Hosts and Groups

The format of the/etc/ansible/hosts file is similar to the INI configuration file for Windows


Mail.example.com

[Webservers]

Foo.example.com

Bar.example.com

[Dbservers]

One.example.com

Two.example.com

Three.example.com


The square brackets [] are group names, which are used to classify the system to allow for individual management of different systems


A system can belong to different groups, such as a server that can belong to both the Webserver group and the DBServer group.

Variables that belong to two groups can be used by this host


If the SSH port of the host is not a standard 22 port, you can append the port number to the host name, separated by a colon.

The port numbers listed in the SSH configuration file are not used in the Paramiko connection and are used in the OpenSSH connection


When the port number is not the default setting, you can explicitly indicate

192.168.80.20:20120


Suppose you have some static IP address, want to set some aliases, but not set in the system's host file, or you are connected through a tunnel, then you can set the following

Jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50

In this example, the "jumper" alias will connect 192.168.1.50:5555. Remember, this is a variable that is set by the properties feature of the inventory file. Generally, this is not a set variable


A similar set of hostname that can be abbreviated as

[Webservers]

Www[01:50].example.com

In the shorthand mode of a number, 01:50 can also be 1:50, meaning the same, and you can define the shorthand mode for the letter orientation.

[Databases]

Db-[a:f].example.com


For each host, you can also choose a connection type and a connection user name:

[Targets]

localhostansible_connection=local

other1.example.comansible_connection=sshAnsible_ssh_user=mpdehaan

other2.example.comansible_connection=sshAnsible_ssh_user=mdehaan


So the setup of the inventory file discussed above is a shorthand method

----------------------------------------------------------------------

Host variables

As mentioned earlier, assigning variables to the host is easy to do, and these variables can be used in playbooks after they are defined

[Atlanta]

Host1http_port=80maxrequestsperchild=808

Host2http_port=303maxrequestsperchild=909


Variables for groups

You can also define variables that belong to the entire group

[Atlanta]

Host1

Host2


[Atlanta:vars]

Ntp_server=ntp.atlanta.example.com

Proxy=proxy.atlanta.example.com


Take a group as a child of another group

You can use a group as a child of another group and assign variables to the entire group, which can be used for/usr/bin/ansible-playbook, but not for/usr/bin/ansible-playbook, but not for/usr/bin/ Ansible use

[Atlanta]

Host1

Host2


[Raleigh]

Host2

Host3


[Southeast:children]

Atlanta

Raleigh


[Southeast:vars]

Some_server=foo.southeast.example.com

Halon_system_timeout=30

Self_destruct_countdown=60

escape_pods=2


[Usa:children]

Southeast

Northeast

Southwest

Northwest


-----------------------------------------------------------------

Sub-Files define host and group variables

Saving all the variables in the inventory master file is not the best way to save it in a standalone file that is associated with the inventory file, unlike the inventory file, which is formatted as Yaml


Suppose the path to the inventory file is:

/etc/ansible/hosts


Suppose a host name is ' Foosball ', the host belongs to two groups at the same time, one is ' Raleigh ' and the other is ' webservers '.

Then the variables in the following configuration file can be used by the ' foosball ' host, followed by the ' Raleigh ' group variable, ' webservers ' group variable, ' foosball ' host variable

/etc/ansible/group_vars/raleigh

/etc/ansible/group_vars/webservers

/etc/ansible/host_vars/foosball


For example, suppose you have some hosts that belong to different data centers and then divide them in turn

Each data center uses a number of different servers, such as NTP server, database server, etc., then the group variable of ' Raleigh ' is defined in the file

'/etc/ansible/group_vars/raleigh ', may be similar to this


---

ntp_server:acme.example.org

database_server:storage.example.org


The files that define variables are not necessarily present because this is an optional feature


There are further uses, you can create a directory for a host or a group, the directory name is the host name or group name, the directory of the

You can create multiple files, and the variables in the file are read to the variables of the master or group

The following ' Raleigh ' group corresponds to the/etc/ansible/group_vars/raleigh/directory, which has two files db_settings and cluster_settings, each of which is set

Different variables


/etc/ansible/group_vars/raleigh/db_settings

/etc/ansible/group_vars/raleigh/cluster_settings


All hosts under the ' Raleigh ' group can use variables of the ' Raleigh ' group. When variables become too long, it is easier for us to define variables to manage and organize


Tip:ansible 1.2 and later, the group_vars/and host_vars/directories can be placed in the inventory directory or playbook directory. If the two directories are present, the configuration under the Playbook directory overrides the configuration of the inventory directory.


-----------------------------------------------------------------------------------

Description of the Inventory parameter

As mentioned earlier, by setting the following parameters, you can control how ansible interacts with the remote host, some of which we have already talked about:


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

An example of a host file:


Some_host ansible_ssh_port=2222 Ansible_ssh_user=manager

Aws_host Ansible_ssh_private_key_file=/home/example/.ssh/aws.pem

Freebsd_host Ansible_python_interpreter=/usr/local/bin/python

Ruby_module_host ansible_ruby_interpreter=/usr/bin/ruby.1.9.3


This article from "Eight Miles" blog, declined reprint!

Ansible Study notes 1-inventory file

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.