Ansible Notes
It is a push configuration management tool. Configuration lies/written on the master
server that gets pushed to client/node servers.
What is Ansible: It is an open-source configuration management tool that automates
o Orchestration(IT Automation): It is automated configuring, managing and
coordinating of computer systems, applications and services
o Configuration management: Maintain consistency of all systems
o Application deployment: Automate the deployment
yum install ansible -y
edit /etc/ansible/hosts file and define required server groups under which add the IP
addresses of your client/node machines
[ansible_client]
192.168.1.100 ansible_ssh_user=root ansible_ssh_pass=password
Playbook (Configuration file): It is a set of instruction written in yaml (YAML Ain’t
Markup Language)
---
- name: sample book
hosts: ansible_client
remote_user: root
become: true/yes become root or sudo if non-root user is used above
tasks:
- name: install httpd
yum:
name: httpd
state: latest
- name: run httpd
service:
name: httpd
state: started
- name: create content
copy:
content: "Congrats on installing ansible"
dest: /var/www/index.html
ansible-playbook sample.yml --syntax-check (check if playbook syntax is correct): If
syntax is fine, then output of the command will be just the playbook name
ansible-playbook sample.yml this will push the configuration to the client machines
defined in the ansible_client group
o The above command first gathers the current facts of the remote machine(current
state of the remote machine is gathered first)
Why Ansible: Ansible falls on the ops side of DevOps process. It means it is a tool that
primarily helps system admin/ops guys to configurate and maintain desired system
state.
Ansible helps to manager large number of servers to be managed/maintained to be
consistently at desired state. It helps to standardize the infrastructure.
Architecture:
Modules are ansible playbooks. Inventory contains the group/list of servers to be
managed by ansible. Ansible uses SSH to connect to servers that it will manage.
Working of Ansible: Installed only on local machine which makes ansible agentless.
Then it has playbook (Set of instructions to be executed) and inventory with list of
servers to be managed. Connects to servers via SSH and pushes playbook to those
servers and run the instructions in the pushed playbook on the servers.
Sample playbook to install webserver and database server (should starts with ---)
Ansible tower by Red Hat is a framework for ansible which provides a GUI to work with
Ansible. Instead of typing long commands, tasks can be performing using single click
How to use Ansible with Terraform
nginx.yaml file
---
- name: Install Nginx
hosts: all
remote_user: ubuntu
become: yes
roles:
- nginx
Now create roles/nginx/tasks folder in the current folder that contains nginx.yaml file
In the tasks folder create main.yaml
---
- name: Ensure Nginx is at the latest version
apt:
name: nginx
state: latest
- name: Make sure Nginx is running
systemd:
name: nginx
state: started
Now create ansible.cfg in the same folder as nginx.yaml file and add below to disable host
validation done by ansible
[defaults]
host_key_checking = false
Now create main.tf in the same folder as nginx.yaml file