What is ansible?
Ansible is an open-source automation tool used for configuring systems, deploying applications, and automating tasks. It works without needing special software installed on target machines, using SSH for communication. Ansible’s simple, human-readable YAML files (Playbooks) define the desired state of your systems, making it easy to manage infrastructure consistently and efficiently.
Creating setup
Passwordless Authentication required for creating setup for multiple setup
ssh-copy-id -f "-o IdentityFile <PATH TO PEM FILE>" root@<INSTANCE-PUBLIC-IP>
Set Inventory
An Ansible inventory file lists the hosts (nodes) that Ansible will manage. It groups hosts and defines variables for them, enabling efficient configuration and deployment across multiple machines.
- Create a inventory.ini file and give your node details with user name
root@<ip-address>
root@10.23.209.12
Run the playbook including
ansible-playbook -i inventory.ini deploy.yml
Group Ip address
[RHEL]
root@10.23.209.12
[ubuntu]
root@10.23.209.12
Run the group using command: ansible-playbook -i inventory.ini deploy.yml RHEL
What is ad-hoc command ?
An ad-hoc command in Ansible is a single, quick command used to perform tasks on remote hosts without writing a playbook. It’s ideal for simple, one-time tasks.
ansible -i inventory.ini -m shell -a "df -h" -l RHEL
What is an Ansible Playbook?
An Ansible playbook is a configuration management and automation tool that defines a series of tasks for Ansible to execute on managed machines (hosts). A playbook is written in YAML format and is designed to be human-readable. It allows users to define what state they want the system to be in, and Ansible automatically ensures the tasks are performed to reach that desired state.
A playbook typically contains one or more plays, and each play defines:
- Which hosts to target.
- The tasks to run on those hosts.
- The variables or configurations to use.
Where you used ansible in your project, give me a scenario ?
In our dev, stage or QA environment we have multiple server, suppose developer wants to clone the code for that we need to install git, so in that situation i can login to a master server and write a playbook and run it, the automatically in all the server git will be download
What is ansible module ?
Ansible modules are discrete units of code designed to perform specific tasks in an Ansible playbook. They are essentially building blocks used to automate a wide variety of tasks, from managing services and files to interacting with cloud providers. Here’s an overview of some common Ansible modules, organized by category:
Common Ansible Modules:
- File Management:
copy
: Copies files to remote locations.fetch
: Retrieves files from remote machines to the local machine.file
: Manages file properties, such as permissions and ownership.template
: Copies a Jinja2 template after rendering it with variables.
- Package Management:
apt
: Manages packages on Debian/Ubuntu-based systems.yum
: Manages packages on RHEL/CentOS-based systems.
- Service Management:
service
: Manages services.systemd
: Manages systemd services.
Lets say I have both Ubuntu and centos machines as nodes I want install application tree using same playbook, how would you approach this scenario?
To install the tree
application on both Ubuntu and CentOS nodes using the same Ansible playbook, you can use the ansible_os_family
variable to determine the operating system and apply the appropriate package manager.
- name: Install tree on Ubuntu and CentOS
hosts: all
become: true
tasks:
- name: Install tree
package:
name: tree
state: present
when: ansible_os_family == "Debian" or ansible_os_family == "RedHat"
How to handle prompts with ansible playbook?
Using the expect
Module
- name: Handle a prompt during execution
expect:
command: passwd user1
responses:
"New password:": "mypassword123"
"Retype new password:": "mypassword123"
become: true