Update 2019: We’ve recently developed a Learning Path, Introduction to Ansible, which will help you to get you started using Ansible to automate common IT tasks, you will learn about Configuration Management and you’ll be able to practice your knowledge on Ansible through a series of hands-on labs. Guy Hummel, our expert cloud trainer, has recently written an introductory post on What is Ansible?.
In a previous blogpost, we have seen the 5 best tools for AWS deployment. One of the tools which we covered was Ansible. In this blog post, we will see how to install this software and will learn the basics of it, to help you to get started with Ansible.
Ansible is one of the youngest and fastest growing configuration management, deployment and orchestration engine. Released in 2012, it is one of the most popular GitHub projects already.
Some of the biggest pros of using Ansible are its agent-less architecture, the use of SSH protocol for communication and the use of YAML syntax for its configuration files. It only requires Python packages installed on client nodes. Agent-less architecture removes the burden of upgrading packages at each new release, while the SSH protocol makes the communication between server and clients very secure. Further, YAML is very easy to read and understand, making the use of Ansible a lot simpler.
Ansible is available in two versions: Ansible Tower (paid one – free up to 10 nodes) and Ansible Open-Source (free).
staging.example.com [webservers] prod-web01.example.com prod-web02.example.com [databaseservers] prod-db01.example.com prod-db02.example.com
Apart from information about nodes and group of nodes, the inventory file also holds information about host specific variables (e.g.: ssh ports, DB parameters), group variables (e.g.: defining some system level parameters or default interpreter) and a group of variables.
You can also pull information about your dynamic inventory using an external inventory system. Plugins are available to fetch inventory from your cloud provider (AWS, GCE, Rackspace, Openstack, etc), LDAP or Cobbler.
Example: The service module is the easiest way to restart your webservers (apache)
ansible webservers –m service –a “name=httpd state=started”
In this case, if a service is already running, it won’t restart the service. This is what modules idempotence means.
A playbook is broken into multiple parts:
A sample playbook is here below:
--- - 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: write the apache config file template: src=/srv/httpd.j2 dest=/etc/httpd.conf notify: - restart apache - name: ensure apache is running service: name=httpd state=started handlers: - name: restart apache service: name=httpd state=restarted
We will dig deeper into Ansible playbooks in the next posts of this series.
As discussed above, the beauty of Ansible is that it is agent-less and relies on the SSH protocol to communicate with hosts. However, there are multiple ways you can connect to hosts or execute Ansible playbooks:
One of the other features making Ansible very easy to use is the variety of installation procedures.
# yum install ansible
1. If you are on RHEL or Centos, install the EPEL repository as well
2. Enable the RHEL Optional Repository [Only applicable for RHEL]
3. Install ansible
#yum install ansible
#pip install ansible
# git clone git://github.com/ansible/ansible.git –recursive #cd ./ansible #source ./hacking/env-setup
To build the inventory, you need to put your managed nodes information in your inventory file. For demonstration purpose, we have launched two fresh EC2 Amazon Linux instances and put down their private DNS in inventory file (/etc/ansible/hosts).
As discussed above, Ansible can be used in Ad-Hoc mode or playbook mode. For this blog post, we will demonstrate using ansible in ad-hoc mode to ping, install apache and start apache service on webservers mentioned in the inventory section. To connect to hosts, you should use ssh-agent.
To ping webservers instances group:
# ansible webserver –m ping –u ec2-user
To install apache on the webservers instances group :
# ansible webservers –m yum –u ec2-user --sudo –a “name=httpd state=present”
Now start the Apache service:
# ansible webservers –m service –u ec2-user –sudo –a “name=httpd state=started”
That’s it. Apache is now installed and running on the web servers instances group defined in the inventory file.
In our next blog post, we will have a close look at Ansible playbooks.
It's Flash Sale time! Get 50% off your first year with Cloud Academy: all access to AWS, Azure, and Cloud…
In this blog post, we're going to answer some questions you might have about the new AWS Certified Data Engineer…
This is my 3rd and final post of this series ‘Navigating the Vocabulary of Gen AI’. If you would like…