How to create Ansible inventory Documentation

Posted By :Baldev Pal |23rd June 2021

 

 

What is Ansible-inventory:

 

  1. Ansible works against multiple systems in our infrastructure at the same time. Selecting a system list in an ansible inventory defaults to being saved in the location /etc/ansible/hosts.
  2. You can specify a different inventory file using the -i <path> option on the command line.
  3. Manual inventory- Create a manual list.
  4. Dynamic inventory- Integrate your CMDB, Cloud inventory systems, LDAP, and more.
  5. Based on location, server role, Applications installed, Development, and production.
  6. A single host can be part of multiple groups.
  7. Include multiple groups as another group of children. Inventory is the most helpful config for ansible

 

Step1. Go to AWS account and Create 3 ec2 instances in the same AZ.

Step2. Take access to all machines via ssh.

Step3. Now go inside the Ansible node server and install the ansible package.

 

$ wget package_name

$ apt install package_name -y

$ apt update

                             Ansible Introduction for Beginners

 

Step4. Now we have to install all the packages simultaneously.

$ apt install ansible python openssl -y

 

Step5. Go to ansible.cfg file inside ansible server and uncomment two lines.

$ nano /etc/ansible/ansible.cfg

1.host_key_checking = False

2.remote_user = ubuntu

 

Step6. Now create the inventory.ini file inside the Ansible server and paste ansible_host, ansible_ssh_user, ansible_ssh_pass.

$ nano inventory.ini

 

Step7. Generate ssh key and copy the public key inside the Ansible server and paste the public key inside server hostA, hostB.

$ ssh-keygen

                            How to Install and Configure Ansible on Ubuntu 20.04  

 

 

$ cd .ssh

$ cat id_rsa.pub

                           How to install and configure Ansible on ubuntu 20.04 - DevOps Article

 

Step8. Now login hostA, hostB server, and past the public key inside known_hosts file.

$ ssh -i pem-filename ubuntu@ip

$ apt update

$ cd .ssh

$ nano known_hosts

Step9. Connect ansible server to hostA and hostB server.

$ ansible webservers -m ping

                                                   For personal use: Ansible-playbook batch source code deployment nginx -  Programmer Sought

 

What are Ad-hoc commands:

 

i. Ad-hoc command is commands which can be run individually to perform Quick function.

ii. These commands are not used for configuration management and deployment because these commands are of one-time usage.

iii. That can be used to ansible is an ad-hoc command that uses the /usr/bin/ansible command-line tool to automate a single task.  

For example:

$ ansible ansiblehosts -a  "df -Th"

                                                How to Install and Configure Ansible on Centos 7 | Fit-DevOps

 

 

What is an ansible module:

 

i. The Ansible is a ships number of module libraries we can be executed in a direction on remote hosts through a 'playbook'.

ii. The library modules can reside in any machine; no servers, daemons, or databases are required.

For example:

$ ansible all -b -m service -a "name=apache2 state=restarted"

 

 

What is an ansible-playbook:

 

i. Playbook is ansible are written in YAML format.

ii. It is a human-readable data serialization language. It is commonly used for configuration files.

iii. Playbook is like a file where you write codes consist of vars, tasks, handlers, files, templates, and roles.

iv. Each playbook is composed of one or more 'modules' in a list module is a collection of configuration files.

v. Playbook is divided into many sections like -

                            a. Target section:- Define the host against which playbooks task has to be executed.

                            b. Variable section:- define variables

                            c. Task section:- List of all modules that need to run, in order.

 

What is an ansible-variable:

 

i. Ansible user variables which are defined previously to enable more flexibility in playbook and roles. That is used to loop through a set of given values, access various information like the hostname in a system, and replace certain strings in a template with specific values.
ii. Put variable section above tasks so that we define it first & use it later.

 

What is handler-section:

 

i. A handler is the same as a task, but it will run when called by another task.
ii. The Handler is just like a regular task in an ansible playbook but is only run if it contains a notify directive and indicates that it changed something.

 

For example: Now create one playbook

$ nano nginx.yml


- name: the install nginx

  hosts: webservers

  sudo: yes

  vars:

     pkgname: nginx

     tasks:

       - name: install nginx

         action: apt name=’{{pkgname}}’ state=installed

       - name: 'copy file an directory on webserver1'

         copy:

            src: /home/baldevpal/terraform/templatemo_525_the_town/

            dest: /var/www/html/

            notify: restart nginx

  handlers:

     - name: restart nginx

       service: name=nginx state=restarted


 $ ansible-playbook -i inventory.ini nginx.yml

 

                                                      Using Ansible to configure NGINX on Ubuntu and host a static website -  GraspingTech

 

What is Ansible-vault:

 

i.The Ansible allows keeping for sensitive data such as passwords or keys in encrypted files, rather than plaintext in our playbooks.

$ ansible-vault encrypt target.yaml

$ ansible-vault decrypt target.yaml

 

What is Ansible-role:

 

i.We can use two techniques for reusing a set of tasks includes and roles.

ii. Roles are good for organizing tasks and encapsulating data needed to accomplish those tasks.

Ansible-roles: 

  1. Default
  2. Files
  3. Handlers
  4. Meta
  5. Templates
  6. Tasks
  7. Vars

iii. We can organize playbooks into a directory structure called roles.

iv. Adding more and more functionality to the playbook will make it difficult to

maintain in a single file.

For example:

$ ansible-galaxy init my-role

                                                 How to Create Ansible Roles and Use them in Playbook


About Author

Baldev Pal

Baldev is a DevOps Engineer. He has Knowledge of Linux, AWS, Docker, Jenkins, Kubernetes, Terraform, and Git.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us