Inventory

Can be .ini or .yml. Basically has information about the Managed node and how ansible can communicate with it.

Store the configuration of each host that the Play targets to.

Example:

inventory.yml

all:
    vars:
        ansible_user: root
        ansible_password: test1

myhosts:
    hosts:
        host1:
            ansible_host: 172.20.0.2
        host2:
            ansible_host: 172.20.0.3
        host3:
            ansible_host: 172.20.0.4

inventory.ini

[all:vars]
ansible_user=root
ansible_ssh_pass=test1
ansible_connect=ssh

[myhosts]
172.20.0.2
172.20.0.3
172.20.0.4

These are equivalent. You can test the connection by running:

ansible myhosts -m ping -i inventory.yml

To specifying pinging with different user, you can

ansible myhosts -m ping -i inventory.yml -u anotheruser

Groups

In the previous example, a group is for example myhosts

Default group

Ansible create the two default group:

  • all: all the group
  • ungrouped for hosts that're not grouped

Host

In the previous example, a host is for example host1, host2 and host3

[!note]
The IPs in here are from Managed node

The vars in here are variables. Take a look at Ansible Variables

[!important]
The way ansible works here is try to SSH into these machine, so that we need to make sure that our Managed node is added to the known_host of our Control node

Metagroups

Metagroup is a big group which has mulitple other groups. A metagroup can follow this format:

metagroupname:
    children:
        ...

For example:

all:
    vars:
        ansible_user: root
        ansible_password: test1

webserver:
    hosts:
        springboot:
            ansible_host: 172.20.0.2
        react:
            ansible_host: 172.20.0.3
        python:
            ansible_host: 172.20.0.4


database:
    hosts:
        mysql:
            ansible_host: 172.20.0.5
        mongodb:
            ansible_host: 172.20.0.6



app:
    children:
        database:
        webserver:

In here app is the metagroup which has database and webserver.

To ping webserver only we can do

ansible webserver -m ping -i inventory.yml

To ping database only we can do

ansible database -m ping -i inventory.yml

To ping everything we can do

ansible app -m ping -i inventory.yml

Vars

To declare a global vals we can do (use the vars in Ansible Variables):

all:
    vars:
        ansible_user: root
        ansible_password: test1

Otherwise, if it's service dependent var, we can do:

webserver:
    hosts:
        springboot:
            ansible_host: 172.20.0.2
            ansible_password: test2 # here 
        ...

It can also be applied to the whole group:

...
database:
    hosts:
        mysql:
            ansible_host: 172.20.0.5
        mongodb:
            ansible_host: 172.20.0.6
    vars:
        ansible_password: test2 # here
...