Ansible: Initial Setup
In my previous post, I went quickly through ansible installation and initial setup. I did not really setup anything. I just showed you where the find things that are brought by ansible by default.
In this post I will go deeper in the setup process. But I am still not going to try to impress you here. Let keep that for future posts.
Ansible Control Node
Ansible config file is locate at /etc/ansible/ansible.cfg by default. We are going to use this file later to customize our installation of Ansible.
If you have just a fiew nodes, you can SSH into each one of them to make sure you can correctly connect. That also means that if you have just a few nodes, Ansible might not the right tool.
Use ssh-copy-id key.pub node-user@192.168.10.10 to add the controller ssh key to authorized hosts that can connect to the nodes.
Ansible Inventory
The inventory contains the nodes you want ansible to manage. The default inventory file is located at /etc/ansible/hosts. The nodes are put into groups for ease of management. The group names must be unique and they are case sensitive. The inventory file contains the IP addresses or FQDN of the managed hosts.
If we want to use the default inventory file we can just run:
# to ping all nodes in the web group
ansible -m ping web
But if we are working on a dedicated inventory file, like my_nodes.ini, we should tell ansible that we are providing and inventory file by adding -i [INVENTORY FILE]. For example, ansible web -i my_nodes -m ping
The inventory in the ini format looks like:
[web]
192.168.12.13
192.168.12.14
[db]
192.168.13.13
192.168.13.15
But the inventory file can also be written in the YAML format:
my_nodes:
hosts:
node_01:
ansible_host: 192.168.10.12
node_02:
ansible_host: 192.168.10.13
[web] is a group name. It is unique accross the inventory file. We can have multiple groups in a inventory file.
To run ansible command on multiple groups we do separate the groups name with colons. For example:
ansible web:db -m ping -i my_nodes.ini --ask-pass
This command will nodes in the web and db groups. --ask-pass allows prompting for password if somehow the SSH daemon in the managed nodes is asking for the user password.
How to Manage Nodes with Ansible
Until now we only learned how to ping our nodes using ansible ping module. ansible web -m ping is the language to tell ansible to use the ping module to ping the web group.
Key Points to Remember
-
Ansible is used to automate repetitive tasks we perform on network devices
-
Ansible inventory contains grouped list of nodes we want to manage
-
The inventory can be written in the
iniorYAMLformat -
Ansible comes with prebuilt modules like
pingto faciliate the nodes management.
In my next posts, I will be going deeper on each importaint part of Ansible such as inventory and playbook.
So, read me soon.