Ansible Setup Book
Ansible Setup Book
Ansible Setup Book
com 1
How to Install and Configure ‘Ansible’ Automation Tool
for IT Management – Chapter 1
Ansible is an open source, powerful automation software for configuring, managing and
deploying software applications on the nodes without any downtime just by using SSH.
Today, most of the IT Automation tools runs as a agent in remote host, but ansible just
need a SSH connection and Python (2.4 or later) to be installed on the remote nodes to
perform it’s action.
My Environment Setup
Controlling Machine – Ansible
Remote Nodes
Node 1: 192.168.0.112
Node 2: 192.168.0.113
Node 3: 192.168.0.114
2. Once you confirm your system settings, it’s time to install ‘Ansible’ software on the
system.
Here we are going to use official Ansible PPA repository on the system, just run the below
commands to add the repository.
On RHEL/CentOS/Fedora
Unfortunately, there are no official Ansible repository for RedHat based clones, but we can
install Ansible by enabling epel repository under RHEL/CentOS 6, 7 and currently
supported fedora distributions.
Fedora users can directly install Ansible through default repository, but if you are using
RHEL/CentOS 6, 7, you have to enable EPEL repo.
After configuring epel repository, you can install Ansible using following command.
After installed successfully, you can verify the version by executing below command.
$ ansible –version
4. To perform any deployment or management from the localhost to remote host first we
need to create and copy the ssh keys to the remote host. In every remote host there will
be a user account tecmint (in your case may be different user).
First let we create a SSH key using below command and copy the key to remote hosts.
5. After creating SSH Key successfully, now copy the created key to all three remote
server’s.
$ ssh-copy-id [email protected]
$ ssh-copy-id [email protected]
$ ssh-copy-id [email protected]
$ ssh [email protected]
$ ssh [email protected]
$ ssh [email protected]
Inventory file, This file hold the host information’s like which host we need to get connect
from local to remote. Default inventory file will be under /etc/ansible/hosts.
7. Now let’s add these three hosts to inventory file. Open and edit file using your favourite
editor, Here I use vim.
[web-servers]
192.168.0.112
192.168.0.113
192.168.0.114
Note: The ‘web-servers‘ in the brackets indicates as group names, it is used in classifying
systems and deciding which systems you are going to controlling at what times and for
what reason.
9. Now, here we are using another module called ‘command‘, which is used to execute list
of commands (like, df, free, uptim, etc.) on all selected remote hosts at one go, for
example watch out few examples shown below.
a. To check the partitions on all remote hosts
$ ansible -m command -a "df -h" web-servers
Like this way, we can run many shell commands using ansible as what we have run the
above steps.
Conclusion
Ansible is a Powerful IT automation tool which is must every sysadmins for deploying
applications and managing server’s at one go. Among any other automation tool such as
puppet, Capistrano, salt, Ansible is quit very interesting and very easy to setup for
production environment. Capistrano oh no i feel headache please leave me alone :p this
what i used to say.
Ansible use only SSH as there agent. We don’t have to install and run any agent in the
remote servers. Hope this article will be interesting one for you too. In our next article, I will
show you how to setup the directory structure for Ansible deployment and creating
playbooks and working with it.
Till then keep on tracking us to get updated articles and don’t forget to tell us your opinions
on the Ansible and also tell us do you use any other automation tool which is more
powerful than Ansible….
Reference Links
http://www.ansible.com/get-started
http://docs.ansible.com/
After installing the software in the controller machine, creating the keys for passwordless
login and copying them to the nodes, it’s time to learn how to optimize the process of
managing such remote systems using Ansible.
Node1: 192.168.0.29
Node2: 192.168.0.30
In addition, please note that both nodes have been added in the webservers section of the
local /etc/ansible/hosts file:
For example, setting up and configuring WordPress on multiple hosts – which we will
cover in the next article of this series). This is where Playbooks come into scene.
Simply put, Playbooks are plain text files written in the YAML format, and contain a list
with items with one or more key/value pairs (also known as a “hash” or a “dictionary”).
Inside each Playbook you will find one or more group of hosts (each one of these groups is
also called a play) where the desired tasks are to be performed.
An example from the official docs will help us to illustrate:
1. hosts: this is a list of machines (as per /etc/ansible/hosts) where the following tasks
will be performed.
2. remote_user: remote account that will be used to perform the tasks.
3. vars: variables used to modify the behavior of the remote system(s).
4. tasks are executed in order, one at a time, against all machines that match hosts. Within
a play, all hosts are going to get the same task directives.
If you need to execute a different set of associated tasks for a specific host, create another
play in the current Playbook (in other words, the purpose of a play is to map a specific
selection of hosts to well-defined tasks).
---
- hosts: webservers
remote_user: root
vars:
variable1: value1
variable2: value2
remote_user: root
tasks:
- name: description for task1
task1: parameter1=value_for_parameter1 parameter2=value_for_parameter2
- name: description for task1
task2: parameter1=value_for_parameter1 parameter2=value_for_parameter2
handlers:
- name: description for handler 1
service: name=name_of_service state=service_status
- hosts: dbservers
remote_user: root
vars:
variable1: value1
variable2: value2
…
5. handlers are actions that are triggered at the end of the tasks section in each play, and
are mostly used to restart services or trigger reboots in the remote systems.
# mkdir /etc/ansible/playbooks
And a file named apache.yml inside of there with the following contents:
---
- hosts: webservers
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: pkg=httpd state=latest
- name: replace default index.html file
copy: src=/https/www.scribd.com/static_files/index.html dest=/var/www/html/ mode=0644
notify:
- restart apache
- name: ensure apache is running (and enable it at boot)
service: name=httpd state=started enabled=yes
handlers:
- name: restart apache
service: name=httpd state=restarted
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
</script>
</head>
<body>
<h1>Apache was started in this host via Ansible</h1><br>
<h2>Brought to you by Tecmint.com</h2>
</body>
</html>
That said, now it’s time to use this playbook to perform the tasks mentioned earlier. You
will note that Ansible will go through each task by host, one at a time, and will report on the
status of such tasks:
# ansible-playbook /etc/ansible/playbooks/apache.yml
Now let’s see what happens when we open a browser and point it to 192.168.0.29 and
192.168.0.30:
# ansible-playbook /etc/ansible/playbooks/apache.yml
This time, the task reports that the Apache web server was started and enabled on each
host:
Summary
In this article we have described how to run commands and execute complex tasks on
several remote hosts simultaneously using Ansible. The official documentation and the
GitHub repository provide a lot of examples and guides on how to use Ansible to achieve
almost any imaginable task.
As you start learning how to automate tasks on remote Linux hosts using Ansible, we
would like to hear your thoughts. Questions, comments, and suggestions are also always
welcome, so feel free to contact us using the form below any time.
In the current tutorial we will explain how to set up WordPress in the same remote
servers:
where we installed, enabled, and started Apache (you probably know by now why we
chose to work with a web server as an initial example in the last tutorial).
I highly encourage you to read Chapter 1 and Chapter 2 before proceeding further in order
to make sure you’re familiar with the concepts associated with Ansible.
In the above image we can see that ansible-galaxy created two directories with the same
name as our roles, and other subdirectories (defaults, files, handlers, meta, tasks,
templates, and vars) and a README.md file inside each of them.
In addition, a YAML file named main.yml was created inside all of the directories listed
earlier, with the exception of files and templates.
We will begin by editing the following configuration files as indicated:
1. /etc/ansible/playbooks/wp-dependencies/tasks/main.yml. Note that we are including
httpd in case you have not followed along with the previous tutorials of this series.
main.yml
2. /etc/ansible/playbooks/wp-dependencies/defaults/main.yml
main.yml
---
# defaults file for wp-dependencies
wp_mysql_db: MyWP
wp_mysql_user: wpUser
wp_mysql_password: wpP4ss
3. /etc/ansible/playbooks/wp-install-config/tasks/main.yml:
main.yml
---
# tasks file for wp-install-config
- name: Create directory to download WordPress
command: mkdir -p /opt/source/wordpress
…
/** The name of the database for WordPress */
define('DB_NAME', 'MyWP');
5. For new database server installations where the root password is empty, such as in this
case, unfortunately we need to setup the password for user root individually in every
machine through mysql_secure_installation.
As far as I know, there is no available workaround that will allow you to set up the root
password via Ansible in the same step where you create the administrative database
account for WordPress.
Make sure you use the same password in all hosts, then copy the credentials in
/root/.my.cnf (the actual location may differ in your case, but in all instances it needs to
match the value of the src parameter for the task Copy ~/.my.cnf to nodes in
/etc/ansible/playbooks/wp-dependencies/tasks/main.yml).
In that file (see above) we’ve assumed that the password for root is
YourMariaDBRootPassword.
roles:
- wp-dependencies
- wp-install-config
# ansible-playbook playbook.yml
Now let’s check if we can access the WordPress Admin page using the IP addresses of
node1 192.168.0.29 and node2 192.168.0.30:
https://youtu.be/wA6kPTZGo4c
As you can see, you can set up multiple WordPress installations with little to no effort using
Ansible. Then you can use the respective Admin user interface to configure each site
separately.
Final considerations
If you are using another distribution to deploy WordPress, the packages name may vary,
but it comes down to installing the Apache web server, the MariaDB database server, and
the Python MySQL module. If that is the case, use your distribution’s software
management system to search for the exact package name that you need to install.
Summary
In this series we have explained how to use Ansible to run commands and execute
complex tasks in several Linux machines simultaneously.
One of such examples is setting up WordPress, as we have discussed in this guide.
Whether you are a system administrator or a blogger, I hope you have found the concepts
and examples in this tutorial useful.