Ansible Inventory

Ansible Inventory: Ansible functions over numerous frameworks in your foundation simultaneously. It does so by choosing segments of frameworks recorded in Ansible's stock, which defaults to being spared in the area and etc/ansible/hosts. You can indicate an alternate stock document utilizing the - i <path> choice on the order line.

Not exclusively is this stock configurable, yet you can similarly use various stock records simultaneously and pull stock from dynamic or cloud sources or various arrangements (YAML, INI, and so on), as depicted in working with Dynamic Inventory. Presented in variant 2.4, Ansible has stock modules to make this adaptable and adjustable.

Hosts and Groups

The stock document can be in one of the numerous organizations, contingent upon the stock modules you have. For such models, the arrangement for /etc/ansible/hosts is an INI-like (Defaults of Ansible) and resembles this:

 mail.example.com
 [webservers]
 foo.example.com
 bar.example.com
 [dbservers]
 one.example.com
 two.example.com
 three.example.com 

The headings in sections are bunch names, which is used in characterizing frameworks and choosing what frameworks you control at what times and for what reason.

The YAML type would resemble:

 all:
     hosts:
 mail.example.com:
     children:
                 webservers:
     hosts:
                         foo.example.com:
                         bar.example.com:
                 dbservers:
     hosts:
           one.example.com:
           two.example.com:
           three.example.com: 

It's alright to place frameworks in two or more groups; for example, a server could represent a webserver and a dbserver. Note that factors will originate from the entirety of the gatherings. Variable priority is point by point in a later section.

After the hostname with a colon, you can put the port number that sudden spike in demand for non-standard SSH ports. Ports recorded in your SSH config document won't be utilized with the paramiko association however will be used with the OpenSSH association.

It is proposed that you configure them if things are not executing on the default port to create stuff unequivocal:

bigelephant.illustration.com:5039

Assume that you have recently static IPs and need to set up certain false names live in your host record or interfere through passages. You can do this as follows:

In INI:

jumper ansible_port=5555 ansible_host=192.0.201.36

In YAML:

 ...
      hosts:
 jumper:
      ansible_port: 5555
      ansible_host: 192.0.201.36 

In the model given above, attempting to Ansible against the host pseudonym "jumper" (which may not sound like a genuine hostname) will contact 192.0.2.50 on port 6666. Note that this is utilizing a component of the stock document to characterize some unique variables. As a rule, it isn't an ideal approach to characterize variables that portray your framework strategy. However, we'll share proposals on covering this stuff afterward.

Host Variables

As portrayed above, it is anything but difficult to relegate factors that will be utilized in playbook afterwards:

 [atlanta]
 host1 http_port=80 maxRequestsPerChild=808
 host2 http_port=303 maxRequestsPerChild=909 

Group Variables

A whole group can consume variables without a moment's delay:

The INI way:

 [atlanta]
 host1
 host2
 [atlanta:vars]
 ntp_server=ntp.atlanta.illustration.com
 proxy=proxy.atlanta.illustration.com 

The YAML rendition:

 atlanta:
        hosts:
             host1:
             host2:
        vars:
 ntp_server: ntp.atlanta.example.com
 proxy: proxy.atlanta.example.com 

Know that this is just an advantageous method to apply variables to numerous hosts immediately. However, you can target that by gathering; factors are constantly smoothed to the host level before a play is executed.

Gatherings of Groups and Group Variables

It is conceivable to gather of groups utilizing the  :children addition in INI or the children: section in YAML. Variables can be applied by using: vars or vars:

 [atlanta]
 host1
 host2
 [raleigh]
 host2
 host3
 [southeast:children]
 atlanta
 raleigh
 [southeast:vars]
 some_server=foo.southeast.example.com
 halon_system_timeout=40
 self_destruct_countdown=80
 escape_pods=2
 [india:children]
 southeast
 upper east
 southwest
 northwest
 ------------------------------------------------------------------------------------------------------------------------------------------
 all:
   children:
      india:
        children:
          southeast:
             children:
                atlanta:
    hosts:
        host1:
        host2:
                raleigh:
     hosts:
        host2:
        host3:
             vars:
 some_server: foo.southeast.example.com
 halon_system_timeout: 40
 self_destruct_countdown: 80
 escape_pods: 2
           northeast:
           northwest:
           southwest: 

If you need to store records or hash information or keep host and gather explicit factors separate from the stock document, see the following segment. Kid bunches have several properties to note:

  • Any host that is an individual from a youngster bunch is naturally an individual from the parent gathering.
  • A kid gathering's factors will have higher priority (supersede) than a parent gathering's factors.
  • Gatherings can have various guardians and kids, however, not roundabout connections.
  • Hosts can likewise be in various gatherings, yet one host blends the information from the numerous gatherings.

Default groups

Two default groups are 'all' and 'ungrouped'. 'all' contain each host. Ungrouped contains all that don't have other gatherings besides all. Although all and ungrouped are consistently present, they can be understood and not show up in bunch postings like group_names.

How Variables Are Merged

As a matter of course, factors are consolidated/smoothed to the particular host before a play is run. It keeps Ansible zeroed in on the Host and Task, so bunches don't generally make due outside of stock and host coordinating. Of course, variables are overwritten by Ansible, including the ones characterized for a gathering as well as host (if you want to change this, see the hash_merge setting). The request/priority is (from most reduced to most noteworthy):

  • all group (since it is the 'parent' of every single other group)
  • parent group
  • child group
  • host

When gatherings of a similar parent/children level are combined, it is done one after another in order, and the last gathering stacked overwrites the past gatherings. For instance, an a_group will be converged with b_group, and b_group vars that match will overwrite the ones in a_group.

New in form 2.4.

Beginning in Ansible form 2.4, clients can utilize the gathering variable ansible_group_priority to change the union request for gatherings of a similar level (after the parent/children request is settled). As a higher magnitude of the number, the later it will be consolidated, giving it a higher need. If the variable’s value is not, it defaults to 1. For instance:

 a_group:
 testvar: a
 ansible_group_priority: 10
 b_group?
 testvar: b             

If the two groups are required with a similar need in the given model, the outcome would regularly have been testvar == b; however, since we are giving the a_group a greater need, the outcome will be testvar == a.