How to install Ansible on Ubuntu 18.04/16.04/14.04
First step: Update the Ubuntu packages
Now install Ansible by simple command
Verify the Ansible and Python Installed or not
Create an inventory file where added all the host.
Like – inventory
Before proceed. We will generate and ssh public to which will be added to our all host in authorize_keys. To generate the ssh public key on Ansible machine. We will run the following command.
Here as many vv we will use that shows that level the errors.
sudo apt-get update ; apt-get upgrade
sudo apt-get install ansible
ansible --version
python --version
Like – inventory
[webserver]
52.66.239.76
52.66.235.128
13.233.67.129
# 13.233.122.239 ansible_user=ec2-user
# 13.127.93.201 ansible_user=ec2-user
ssh-keygen
This command with generate a id_rsa.pub and id_rsa key under .ssh folder. We will copy the contents of id_rsa.pub and paste in each hosts mentioned under inventory file and append the contents under host user .ssh/authorize_keys.
Make sure host_key_ckecking = flase is to be uncommented. (This line is in file under /etc/ansible/ansible.cfg)
Make sure host_key_ckecking = flase is to be uncommented. (This line is in file under /etc/ansible/ansible.cfg)
Ansible Simple Playbook
---
- hosts: webserver
tasks:
- name: Install Apache package in Ubuntu machine
apt: name=apache2 state=present
- name: Start apache server and enable on boot mode
service: name=apache2 state=started enabled=yes
To check syntax error in Ansible Playbook file
ansible-playbook webserver.yml --syntax-check
To Check the no. of hosts will be used in particular playbook
ansible-playbook webserver.yml --list-host
To dry run (means check before run playbook) the playbook, this is for testing. If everything is working fine or not
ansible-playbook webserver.yml --diff --check
To run playbook on a particular host among all the host mentioned in hosts file
ansible-playbook webserver.yml -l '13.233.67.129'
How to run Ansible Playbook
ansible-playbook -i
Ansible Playbook with Notify and Handler (copy the apache conf file to host machine)
---
- hosts: webserver
tasks:
- name: Install Apache package in Ubuntu machine
apt: name=apache2 state=present
- name: copy file to remote server
template: src=apache2.conf dest=/etc/apache2/apache2.conf
notify:
- restart apache
handlers:
- name: restart apache
service: name=apache2 state=restarted enabled=yes
Call variable in Ansible
Here we have changed the variable in apache.conf file Timeout 300 to Timeout {{Timeout}} and when we run the playbook it will change the variable in run level
Here we have changed the variable in apache.conf file Timeout 300 to Timeout {{Timeout}} and when we run the playbook it will change the variable in run level
ansible-playbook -i -e 'Timeout=600'
In template, We can call the variables but in file we can’t call variables.
---
- hosts: webserver
tasks:
- name: Install Apache package in Ubuntu machine
apt: name=apache2 state=present
- name: copy file to remote server
file: src=apache2.conf dest=/etc/apache2/apache2.conf
notify:
- restart apache
handlers:
- name: restart apache
service: name=apache2 state=restarted enabled=yes
To check the exactly where are we getting a the error. This is we used for debugging
ansible-playbook webserver.yml -vv