If you have been around the open source world within the last few years, you would have heard about Red Hat Ansible Automation Platform. If not, think of Ansible as the key to automating the world.

What is Ansible?

Automation! Ansible is automation.

It is an open-source software provisioning, configuration management, and application-deployment tool that is highly customizable through playbooks to meet the needs of the environment

Ansible Core

Ansible Core is the command-line tool that is installed from either community repositories or the official Red Hat repositories for Ansible.

Installation instructions can be found here.

Ansible Tower

Ansible Tower is the Graphical User Interface tool used to execute Ansible tasks. Tower requires a license that is based on the number of systems tower will manage.

Installation instructions can be found here.

How does Ansible work?

Ansible connects over ssh or WinRM (windows) to a remote system to execute a task. Python 2.4+ is a requirement on the remote systems where Ansible will execute its tasks.

Ansible Ad-hoc command

Ansible Core allows ad-hoc commands to be run, or for playbooks to be executed. We will cover playbooks in-depth in another section.

Here is an example of an ad-hoc command that pings ‘localhost’.

ansible localhost -m ping

Here is another example of running an ad-hoc shell command on a remote system:

ansible raleigh -m shell -a 'echo $TERM'

Additional examples and explanations can be found here.

Ansible Playbooks

Ansible playbooks are highly customizable scripts that are used to execute a series of tasks and commands. Below is a basic example of a playbook:

---

- name: My first playbook

  hosts: localhost

  tasks:

    - name: install nginx 

         yum:

              name: nginx

              state: present

 

Defining a playbook

Let’s break down the components of a playbook to understand it better.

name: A description field for the task.

hosts: An indication of where the task needs to be executed. 

tasks: A section used to order commands and states that are part of the specific task.

yum: An Ansible module used to manage yum content.

state: Ansible is state-based, which in this playbook, ensures that the Nginx package is present. Different modules support different state parameters.

Running a playbook

Using the above playbook as an example, we will save it as ‘nginx.yaml’.

To run the playbook from Ansible Core, you would execute the following command.

ansible-playbook nginx.yaml

 

Idempotency

A well-written playbook ensures that even if the playbook is run multiple times, the state will not change from what you wrote it for. For this reason, it is essential to ensure that all tasks in your playbook do not change the system in any way if the change you required has already been applied.

In summary

After reading this introduction to Ansible, you have a good understanding of the basics to get started. You can execute ad-hoc Ansible commands and run very basic playbooks. Understanding the importance of idempotence will serve you well when designing future automation. 

Ansible is far more than just a single command; it has enterprise-grade applications like “Ansible Tower” which opens the possibilities to manage hundreds, if not thousands of systems. 

The open source community is vast, with many examples out there to use. Use online resources like ansible-galaxy to find Ansible code that’s already been written to accomplish tasks. There is a good chance that the code you are wanting to write has already been written. No need to reinvent the wheel. If you do happen to find reusable code, please remember to give back any fixes or enhancements you make to the community.  

Connect with Red Hat Services
Learn more about Red Hat Consulting
Learn more about Red Hat Training
Join the Red Hat Learning Community
Learn more about Red Hat Certification
Subscribe to the Training Newsletter
Follow Red Hat Services on Twitter
Follow Red Hat Open Innovation Labs on Twitter
Like Red Hat Services on Facebook
Watch Red Hat Training videos on YouTube
Understand the value of Red Hat Certified Professionals


About the author

Ken is a senior consultant with the UK&I services team, delivering anything Red Hat. With over ten years of experience, no problem is too big or too small. Happy to help and most importantly learn something new every day.

Read full bio