DevOps 05182017 Ansible Install HTTPD Variable Files Handlers

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

-Ansible is available in "open source" and commercial/Enterprise

We are using "ansible" open source version 1.8


For Enterprise it is called as "Ansible Tower"

-This is Push method and agent else and runs on "Python" language
-We use SSH connection (How to create SSH connection is given below) for connection other
nodes(Server's)
-We can run adhoc job @ CLI promt
-Ansible modules are called as "playbooks"

-Installation of Ansible
-First we need to get extra package repository "epel" (Extra Package Enterprise Linux)
[root@server1 ~]# yum install epel - release

-Now install ansible


[root@server1 ~]# yum install ansible

-Now we should be give permission to the "user1" where we are going to run Ansible playbook
[root@server1 ~]# visudo

## Allow root to run any commands anywhere


root ALL=(ALL) ALL
user1 ALL=(ALL) NOPASSWD:ALL

-Now update the server name along with ip in the below path
[root@server1 ~]# cd /etc
[root@server1 etc]# vi hosts

-Now update the "ansible" host inventory files in the path "/etc/ansible/"
[root@server1 ~]# cd /etc/ansible/
[root@server1 ansible]# ls
ansible.cfg hosts index.html roles webserver.yml

-Now rename the exiting "hosts" file to hosts.orig (If required we can use the original file later)
[root@server1 ansible]# cp hosts hosts.orig
[root@server1 ansible]# ls
ansible.cfg hosts hosts.orig index.html roles webserver.yml

-To verify the list of modules available in ansible use the below command
[root@server1 ~]# ansible-doc -l

-If you required particular module


[root@server1 ~]# ansible-doc -l | grep -i yum

-To view particular info about module use the below command (Example are bottom)
[root@server1 ~]# ansible-doc yum

-To verify the communication b/w the nodes (server)


[root@server1 ~]# su - user1
[user1@server1 ~]$ ansible all -m ping
server2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
server3 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host server3 port 22: No route to host\r\
n",
"unreachable": true
}
[user1@server1 ~]$

-Adhoc Jobs (CLI from server1) Examples

-Install "httpd" from command line (Without playbook) from user1/server1 to user1/server2
[user1@server1 ~]$ ansible server2 -m yum -a "name=httpd state=present"

or

[user1@server1 ~]$ ansible server2 -k -m yum -a "name=httpd state=present"


-m -> Name of the module
-k -> If you specify (-k), it will prompt for password
-a -> for input such as name/state in the above example (Syntex)

-Example to copy file from server1 to server2


[user1@server1 ~]$ ansible server2 -m copy -a "src=index.html dest=/var/www/html"

-How to start a service using CLI


[root@server1 ~]# ansible server2 -k -m service -a "name=httpd state=started"
-Now create password less SSH connection b/w user1/server1 and user1/server2
Now switch to user1/server1
[root@server1 ~]# su - user1
[user1@server1 ~]$ ssh-keygen

-Now copy the public key of user1/server1 to the user1/server2


[user1@server1 ~]$ cd .ssh

Copy "id_rsa.pub" to user1/server2


[user1@server1 .ssh]$ ssh-copy-id user1@server2

-Now verify whether you have access to user1/server2 without passowrd less authnication
[user1@server1 ~]$ ssh user1@server2

-
First ansible playbooks.

-Now to switch to user1/server1 by using the below command


[root@server1 ~]# su - user1

-Now we are going to install "httpd" in server2 using ansible playbook called "webserver.yml"
-The below example will illustrate how to setup web server "httpd"
-If the above output is not as desired please verify the below
[root@server1 ~]# service iptables stop
-How to use variables in Ansible
-Create variable.yml file as show below from previous webserver.yml
[user1@server1 ~]$ cp webserver.yml variable.yml

{{host}} - This will be part of run time as "extra-vars" as show below


[user1@server1 ~]$ ansible-playbook variable.yml --extra-vars "host=server2"

-Pkg1, pkg2 and pk3 Info


pkg1 - "httpd" as part of vars in the playbook

pkg2 - As part of file "abc.yml"

pkg3 - User input (CLI)


-To verify the syntax
[user1@server1 ~]$ ansible-playbook variable.yml --extra-vars "host=server2" --check
-Handlers in Ansible
-Tasks in Handlers will be executed at the end after all the tasks are executed successful only

-To verify the syntax


[user1@server1 ~]$ ansible-playbook handler.yml --check
-How to handle Error in Ansible
First copy the existing playbook to handler.yml
[user1@server1 ~]$ cp webserver.yml errors.yml

-In the below example we are going to name "httpd1" instead of "httpd"
ignore_errors: yes, this will ignore and execute the next task

ignore_errors: no, this will stop execution


-Example on creation of file, copy ls output/redirect to /tmp/abc, create dir and run script
-Create playbook "command.yml"

more.sh
-How to Example on "ls" command (unix)
-How to execute shell script in Ansible

You might also like