Ansible-Automation Everywhere
Ansible is an IT automation configuration tool used to handle the configuration. It is not a deployment tool, it is basically configuration management tool used as a deployment tool in most of the organization. Ansible is written in Python.
It is working on ssh command to configure the environment.
Architecture\Working of Ansible
Ansible works by connecting to your nodes and pushing out small programs, called “Ansible modules” to them. Ansible then executes these modules (over SSH by default), and removes them when finished. Your library of modules can reside on any machine, and there are no servers, daemons, or databases required.
There are 2 types of machine
Control machine − Machine from where we can manage other machines.
Remote machine − Machines which are handled/controlled by control machine
The management node in the below picture is the controlling node (managing node) which controls the entire execution of the playbook. It’s the node from which you are running the installation. The inventory file provides the list of hosts where the Ansible modules needs to be run and the management node does a SSH connection and executes the small modules on the hosts machine and installs the product/software.
Installation of Ansible(Run below in Controller machine)
sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible & sudo apt-get update
sudo apt-get install ansible
Note :-
Ansible controller setup can be done only on Linux machine.
Inventory File
Inventory file contains the list of target server.
Below are the steps to create inventory file
mkdir ansible-test
cd ansible-test
cat > inventory.txt
variable_name ansible_hosts =<IP> ansible_ssh_pass=<pwd>
You can classify these hostname into group in inventory file with below syntax-
[db_server]
variable_name ansible_hosts =<IP> ansible_ssh_pass=<pwd>
For windows node,add the below in inventory file
<variable_name> ansible_hosts=<IP> ansible_connection=winrm ansible_user=<user> ansible_password=<password>
Playbook
A single YML file which have set of instructions
Task-a set of action to performed on the hosts.
Syntax-
-
name: ping test
hosts: all
tasks:
- name: ping to test the connection
ping:
MODULES
Module can control system resources which is used to perform a specific task on the target Node.
There are different Modules in Ansible
1.Command-execute a command on remote node
Syntax-
command: cat resolv.config chdir=/etc
command: mkdir /folder creates=/folder
2. Service –used o manage the service
3.Files
4.Database etc
Variables, Loop and Conditions
Variables –a simple name which stores the data
ex- ansible_host, ansible_connection
Loops- A task perform multiple times
Syntax-
tasks: name= {{item.name}}
loop:
- name: joe
- name:sam
Conditions- task perform when the given conditions satisfied.
name: nginx
state: present
when: ansible_os_family==“Ubuntu”
Roles
Roles used to make your task reusable for all the project.
syntax-
- name : Install
hosts:db_server
roles:
-my_sql
User can create custom roles and uploaded to the ansible galaxy.
Ansible-galaxy init mysql
Few command you can try-
Ansible_galaxy search mysql
ansible-galaxy install geerlingy.mysql
Ansible-galaxy list
Ansible-config dump | grep ROLE
Ansible-galaxy install geerlinguy.mysql –p /roles
Ad-hoc Command
Ansible abc -m copy -a “src = /etc/yum.conf dest = /tmp/yum.conf”
Ansible abc -m yum -a “name = demo-tomcat-1 state = present”
Ansible abc -m yum -a “name = demo-tomcat-1 state = latest”
Ansible abc -m yum -a “name = demo-tomcat-1 state = latest”
Handlers
Handlers are our way of calling a Task after some other Task completes.
Handlers are like a task used whenever notify.
hosts: all
tasks:
- name: Install Nginx
apt: pkg=nginx
state=installed
update_cache=true
notify: -
Start Nginx handlers:
- name: Start Nginx service:
name=nginx
state=started