Ansible Overview tells you about what Ansible is and the problems it solves. In this article, we see the Architecture and basic concepts of Ansible
Control Node & Managed Nodes
The Ansible tool is installed in the Control Node. It has the related files and Playbooks . The Managed nodes are the servers where the Playbooks/Commands have to be run. The Control Node connects to the Managed nodes through SSH (Secure SHell). The Managed nodes does not need to have Ansible installed in them as Ansible is agentless. The only requirement for Managed nodes is that it should have Python installed in them (for modules).
Ansible Configuration File
This is the main file which helps in configuring various paths and values for Ansible. The below is the Default configuration file. As you can see,
inventory = /etc/ansible/hosts --specifies the default path for the hosts file. This variable inventory can be changed as per requirement
Similarly various other settings are set , for example ,the colours are set based on the output. The error message will be displayed in Red , warnings in Purple and so on. We can specify to log path, SSH timeout value etc.
# config file for ansible -- http://ansible.com/
# ==============================================# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first[defaults]# some basic default values...#inventory = /etc/ansible/hosts
#library = /usr/share/my_modules/
#remote_tmp = $HOME/.ansible/tmp
#local_tmp = $HOME/.ansible/tmp
#forks = 5
#poll_interval = 15
#sudo_user = root
#ask_sudo_pass = True
#ask_pass = True
#transport = smart
#remote_port = 22
#module_lang = C
#module_set_locale = False.
.
.
.[colors]
#highlight = white
#verbose = blue
#warn = bright purple
#error = red
#debug = dark gray
#deprecate = purple
#skip = cyan
#unreachable = red
#ok = green
#changed = yellow
#diff_add = green
#diff_remove = red
#diff_lines = cyan
Inventory/Host file
The Inventory or the Host files contains the IPs of the managed nodes. The default Inventory file is taken from /etc/ansible/hosts. But if we want to have different playbooks with different host files, it only makes sense to have a custom hosts file inside our current working directory. The location of the customised hosts file is specified using the ‘-i ‘argument ( ‘i’ stands for inventory).
#Command to run Playbook using customized hosts file
#Deploy.yml is the name of the Playbook
#hosts is the filename for the hostfile/inventory. It can be named differentlyansible-playbook deploy.yml -i hosts
The following is an example of a hostfile
#hosts file
159.65.210.167
120.32.134.43
There are multiple ways of passing the hosts .
[i] Using ‘all’
ansible-playbook all -i hosts deploy.yml #takes all IPs specified in the hosts file
[ii] Groups
The IP can be grouped and called as required by the Playbook. This can be done using a small edit onto the hostfile.
#hosts file [AWS]
159.65.210.167
165.23.345.12[DigitalOcean]
120.32.134.43#Segregating IPs to group. We can now call the IP based on Group names.#Calling the AWS server group with Playbook deploy.ymlansible-playbook -i hosts deploy.yml --limit AWS
[iii] Specifying hosts inside Playbook
This is the most common method to specify the host group name inside the Playbook .
#deploy.yml
---
hosts: AWS
become: yes
gather_facts: no
tasks:
.
.
.
.
Running the ansible command:
ansible-playbook -i hosts deploy.yml #Takes the required group from hosts file
Modules
Modules are short code snippets . They can be used in Ansible Playbooks.
The “-m” argument takes the module name and its corresponding arguments
Out of the many modules that ansible provides, here are a few examples of the most commonly used modules :
- ping
- debug
- file
- command
- copy
- apt /yum
- service
- user
Tasks & Playbook
Tasks are actions that you need to perform onto the managed nodes . For single time actions , we have Ad-Hoc commands . When we have multiple tasks to execute, we write the multiple tasks together and call it a Playbook.
Adhoc commands are executed in the Terminal. These tasks are usually for a single time purpose, like changing file permissions onto managed nodes, copying a file from remote(managed server) to local. But if these tasks are to be repeated often, these tasks are written inside a playbook in a single YAML file . Running this file once executes all the tasks in a serial manner.
To keep it simple, ad-hoc commands are for one time task and are for Playbooks for Multiple and Repetitive Tasks.
Cheers!
Linu