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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read