Ansible Configuration And Inventory Files
Last Updated :
11 Mar, 2024
Ansible is a tool that is managed by RedHat and is primarily used for configuration and orchestration. With the help of the tool, we can manage and deploy software on various Linux servers. Ansible doesn't support Windows system configuration
What makes it exceptionally good is that it is agentless, which essentially means it does not require the tool to be installed on managed nodes. So the question would be, won't it pose a security risk if any Linux server can be configured from outside? Here comes the SSH, which is used to generate keys, which are essentially public/private keys. Ansible is also idempotent, which means no matter how many times you run the playbook against the specified managed nodes, the results will be essentially the same.
Why does it matter?
Suppose out of 15 tasks, 10 are implemented and it fails at 11th, which may arise from a syntax error in the script to a network interrupt, and so after rectifying that, we again implement the script, but now the state differs for the first 10 tasks and fails. Hence, Ansible makes notice of all these changes and adjusts accordingly
Terminologies
- Control Node: A system on which the tool is installed
- Managed Node: A system that is controlled by a control node using Ansible
- Playbook: A file written in YAML which is used to automate configuration
- Inventory: A file where the managed nodes are grouped according to need and are important so that Ansible can essentially differentiate between the various systems.
Overview
We are going to use VirtualBox and Ubuntu Server and create two VMs, one as the managed node and the other as the control node.
Steps
- Get ready, VMs: Make two VMs of Ubuntu Server
- Change the network to a bridged adapter inside the virtual box for each of the VMs.

- Note down the IP Address for each using VM using following command
ifconfig
Note that it would be under adapter name other than the network adapter name `lo` and woiuld be named as inet address - Generation of keys: It is required for agent-less architecture
ssh-keygen -C "keyname"
ssh-keygen -C "keyname" ; use -t tag for specifying encryption menthod - Now we have to copy the public key to client so that the host can get to know the data sent by client.
ssh-copy-id targetnode@itsIPAddress
With that initial setup is done
Ansible Configuration
This allows for setting up the basic configuration of what nodes are included [IP Address] , roles path, key path etc. It is written in YAML.
According to RedHat it is stored at 4 locations:
- $ANSIBLE_CONFIG if the environment variable is set.
- ansible.cfg if it’s in the current directory. ---> is the one we can modify
- ~/.ansible.cfg if it’s in the user’s home directory.
- /etc/ansible/ansible.cfg, the default config file.
Example
[defaults]
inventory = inventory
private_key_file = ~/.ssh/gfg
roles_path = roles
#remote_user = client
Here [defaults] section is where we set default configuration options for Ansible. These are used unless overridden by other configuration settings or command-line arguments.
- private_key_file points to the ssh-key created name gfg
- roles_path is for showing where roles created and their config is present.
Another example can be
[defaults]
inventory = /etc/ansible/hosts
remote_user = ansible
private_key_file = ~/.ssh/id_rsa
roles_path = /etc/ansible/roles
log_path = /var/log/ansible.log
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
become is the equivalent to act with permission level of other , this allows control node to run the playbook config on target node. For more tags according to your need refer this Ansible Configuration Settings
Inventory Files
It can be written in INI and YAML. Inventory Files are used because they contain details about servers, devices, and other resources managed by tools like Ansible, Terraform etc. They store IP Address and enable automation tools to execute tasks, deploy applications, and manage configurations across entire infrastructures, facilitating scalability, consistency, and reliability in IT operations and system administration workflows.
INI format
[webservers]
192.168.100.1
192.168.100.2
[dbservers]
192.168.100.3
192.168.100.4
192.168.100.5
we can use webservers and dbservers as tags to for modification to be done to a group of servers.
Ex: add mySQL DB to dbservers only
YAML format
webservers:
- 192.168.100.1
- 192.168.100.2
dbservers:
- 192.168.100.3
- 192.168.100.4
- 192.168.100.5
These inventory files are declared inside the ansible.cfg or is used given at runtime using commands.
Sample Code
Following code runs and NGINX server and we copy html file from files dir to www dir
https://github.com/yeskaydee?tab=repositories, Feel free to commit any changes and improvement.
Output
ansible-playbook taskl.yml -u client -i hosts --ask-become-passansible-playbook taskl.yml -u client -i hosts --ask-become-pass ;
here
-u stands for user , -i stands for inventory file (not required if specified in ansible.cfg)
--ask-become-pass prompts to enter password of target nodes, so that become privileges are given
refer FAQ's 5.
Similar Reads
Terraform Configuration File
Terraform is one of the important tools used for managing Infrastructure as code (IaC). It eases defining, provisioning, and automating of infrastructures from multiple clouds AWS, Azure, and Google Cloud. A configuration file serves as the heart of Terraform which defines the infrastructure that ha
8 min read
Which Configuration Management Software is Agentless?
The process of managing individual system components is referred to as configuration management. An IT system may comprise various IT assets and components. These assets may be a piece of single software or automation tools. Systematic tracking and monitoring of updates to a software configuration i
5 min read
How to Find the MYSQL Configuration File "my.cnf".
The primary configuration file for the MySQL database server is called my.cnf. It includes several options and parameters needed to efficiently operate your MySQL database. My.cnf file editing is frequently required by database administrators to personalize their database installation. In this artic
2 min read
How to View Configuration Files Without Comments in Linux?
We came across an extremely long configuration file with hundreds of thousands of comments, and all we wanted to do was filter out the important content or settings. As a result, we'll look at various methods for viewing configuration files in Linux without comments.Linux configuration files are sof
4 min read
Ansible and Jinja2: Creating Dynamic Templates
Automation is a very integral part of modern IT operations, where teams can deploy, manage, and scale applications in quite an easy manner. Ansible, with its agentless architecture and very easy configuration language, is one powerful automation tool to do these tasks. One of the most versatile feat
7 min read
Ansible Interview Questions and Answers
As DevOps continues to shape the tech landscape, tools like Ansible have become must-haves for anyone looking to excel in software automation. Ansible stands out for its simplicity and power, making it a favorite among companies like Google, Microsoft, and Netflix for streamlining configuration mana
15+ min read
Kubernetes Configuration File - Step by Step Tutorial
Kubernetes Configuration File is the main tool for creating and configuring components in Kubernetes. Components in a Kubernetes cluster can be created in two ways - either by giving commands to the CLI or by creating a configuration file. The advantage of having a Configuration file is that it's si
10 min read
Variables And Templates In Ansible
Letâs say you are owning a small technological company and you need to get the machines set up in a way that itâs correct and as it should be. This might have you installing core software packages, copying over and tweaking system config files or getting involved in system settings just to name a fe
10 min read
Implementing Configuration Versioning with Spring Cloud Config
Configuration versioning is the critical aspect of maintaining and managing the configurations in the microservices architecture. It can allow the teams to track the changes, revert to previous versions and manage the configurations more efficiently, Spring Cloud Config can provide a powerful way to
5 min read
Introduction to Ansible and its Architecture components
In this article, we will discuss the overview of ansible and will mainly focus on its architecture part. Let's discuss it one by one. Overview :Ansible is an IT automation engine that can automate various IT needs. And it has features like application deployment that means you can deploy your applic
3 min read