Ansible: Configuration and inventory files, Direct commands


The Ansible installation creates a global configuration file ( /etc/ansible/ansible.cfg) and a global inventory file ( /etc/ansible/hosts). However, we prefer to use configuration and inventory files at the level of each Ansible project. This allows you to use different configurations and inventories depending on the project.

The inventory file contains the list of machines to manage. Each machine will appear on one line and it is possible to create groups of machines to later launch Ansible scripts to groups of machines.
Example 1. Example inventory file hosts.cfgusing groups

# Inventory hosts.cfg file

[controller]
10.0.0.51

[network]
10.0.0.52

[compute]
10.0.0.53
10.0.0.54
10.0.0.55
10.0.0.56

[block]
10.0.0.51

[shared]
10.0.0.63

[object]
10.0.0.61
10.0.0.62
As an example we can create a working folder (pe cursostic). In that folder we will save all our files. We will start by saving the configuration file ( ansible.cfg) and the inventory file ( hosts.cfg). In the inventory file we will place the machines to manage
Example 2. Local configuration fileansible.cfg
[defaults]

inventory      = ./hosts.cfg
./hosts.cfg - Use the inventory file located in the same folder
Example 3. Inventory filehosts.cfg
# Inventory hosts.cfg file
20.0.1.11
20.0.1.4
Test run
$ ansible all -m ping

20.0.1.11 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
20.0.1.4 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Direct commands
Example 4. Know the disk usage of the machines in the inventory

$ ansible all -a "df -h" 

20.0.1.11 | CHANGED | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
udev            991M     0  991M   0% /dev
tmpfs           201M  3.1M  197M   2% /run
/dev/vda1        20G  2.0G   18G  10% /
tmpfs          1001M     0 1001M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs          1001M     0 1001M   0% /sys/fs/cgroup
tmpfs           201M     0  201M   0% /run/user/1000

20.0.1.4 | CHANGED | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
udev            991M     0  991M   0% /dev
tmpfs           201M  3.1M  197M   2% /run
/dev/vda1        20G  2.0G   18G  10% /
tmpfs          1001M     0 1001M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs          1001M     0 1001M   0% /sys/fs/cgroup
tmpfs           201M     0  201M   0% /run/user/1000
all refers to running on all computers in the inventory

Running commands like root
The argument --becomeallows executing commands like root.
Example 5. Perform operations as rooton all managed computers

ansible all -a "apt update" --become
--become run operations as rooton managed computers
Example 6. Reboot all computers in inventory
$ ansible all -a "reboot" --become
Example 7. Perform operations on a group of managed computers
ansible webserver -a "apt install apache2" --become
webserver indicates the group of servers in the inventory on which to perform operations. A list of groups separated by commas can be indicated.
Ansible allows the use of modules that extend the basic functionality provided by Ansible. The Ansible modules page offers access to the list of modules grouped by categories.
For example, the module copycopies a file from the local file system to the specified location on remote machines.
Example 8. Copy a file to managed machines
ansible all -m copy -a "src=sample.txt dest=/home/ubuntu/sample.txt"

No comments

Powered by Blogger.