Author : Saikrishna
Date : July 2025
-
Ansible is powerful tool and widely used as Configuration management tool.
-
Written in Python Language, so python is the pre-requisite for installing Ansible.
-
Agentless Architecture which means ansible is not needed to be installed in every target node.
-
Passwordless Authentication which Connects with Linux through "SSH" and Windows through "winrm", which is also pre-requisite.
-
Simple YAML language unlike the other tools like puppet and chef which are complex languages.
-
Controlled Node and Managed Nodes Architecture.
-
Uses Push mechanism on to the target servers.
-
Most widely Used in Network Automation.
-
Eliminate repitition which automates.
-
Manage and maintain system configurations.
-
Perform zero-downtime rolling updates.
-
Idempotence.
Commands
----------
brew/yum/apt install ansible (Installing Ansible in MacOS, RHEL and Ubuntu)
pip install ansiblev (Installing Ansible using python package manager)
ansible --version.
ansible -i inventory all -m "shell" -a "df -h & touch file & nproc".
ansible-playbook -i inventory <playbook.yml>
ansible-playbook -i inventory <playbook.yml> --check
ansible-playbook -i inventory <playbook.yml> -vvv
ansible-galaxy role init
Passwordless Authentication
ssh-copy-id -f "-o IdentityFile " ubuntu@ (ssh-copy-id -f "-o IdentityFile key-pair.pem" [email protected])
Copy the public key of controlled node to the target nodes in the ~/.ssh/authorised_keys file.
Can also setup Password Authentication in the target nodes with setting the values "yes"
Inventory File
It has IP addresses of managed nodes and we can group the managed nodes into multiple.
Ansible-Vault
-
Create a password for vault
openssl rand -base64 2048 > vault.pass
-
Add your AWS credentials using the below vault command
ansible-vault create/encrypt/derypt/edit group_vars/all/pass.yml --vault-password-file vault.pass
Ansible-Role Hierarchy Structure
Task 1: Create three(3) EC2 instances on AWS using Ansible loops
2 Instances with Ubuntu Distribution 1 Instance with Redhat Distribution Hint: Use <connection: local> on Ansible Control node.
Task 2: Set up passwordless authentication between Ansible control node and newly created instances.
Task 3: Automate the shutdown of Ubuntu Instances only using Ansible Conditionals
Hint: Use when condition on ansible gather_facts