Table of Contents
Ansible
Ansible is an open source, command-line IT automation software application written in Python. It can configure systems, deploy software, and orchestrate advanced workflows to support application deployment, system updates, and more. Ansible's main strengths are simplicity and ease of use.
Install Ansible
apt install ansible
Next, we need to create a file `/etc/ansible/hosts`, and add our hosts. In essence, here we define hosts and groups of hosts that Ansible will try to manage.
sudo nano /etc/ansible/hosts [control] master01 ansible_connection=local [workers] worker01 ansible_connection=ssh worker02 ansible_connection=ssh worker03 ansible_connection=ssh
[cube:children] control workers
Above, you can see I have added 3 groups: control, workers and cube. Name of the group is the one in between [ ]. This was split so that if I want to execute some actions only on control server, I use the “control” group. Group “cube” has children. This basically means that it’s a group of groups, and when I’m using cube I’m targeting every single node from the listed groups.
Variable: `ansible_connection`: we are telling Ansible how to connect to that host. The primary method is ssh, but I specified “local” for control01, because this is the node that we are running Ansible from. This way, it won’t try to ssh to itself.
Lastly, we are going to make it so that user root will be able to log in to other nodes from control01 without the password using an ssh key. This step is optional, but after this you won’t need to type the password every time you run Ansible.
# Make sure you are user root cd mkdir -p ~/.ssh chmod 700 ~/.ssh # Do not fill anything in next command just enter ssh-keygen -t rsa # Copy keys to each node, for example: ssh-copy-id -i ~/.ssh/id_rsa.pub root@worker01 ssh-copy-id -i ~/.ssh/id_rsa.pub root@worker02 ssh-copy-id -i ~/.ssh/id_rsa.pub root@worker03
After this, we are ready for some mass settings with Ansible.
First Ansible commands
This is the last thing before we head on to the next article. We are going to check if Ansible is working fine and can connect to all nodes:
# Run following as root user # We are going to execute ping via ansible, the "cube" is group we specified in /etc/ansible/hosts # And if you remember this will execute the command on all nodes. # -m mean we are going to use module, in our case module: ping
ubuntu@ubuntu:~$ ansible cube -m ping
# Result should be:
ubuntu@ubuntu:~$ ansible cube -m ping
master01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
worker01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
worker02 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
worker03 | SUCCESS => {
