This will be Part 1 out of 2 of the Ansible primer series.
We love Ansible at OSODevOps. The learning curve is very gradual, and you can get value from it after just spending a few minutes working with it.
Ansible allows your whole infrastructure to be defined as code so that it can be version controlled, easily replicated and tested.
- Ansible and its module are written in Python, but you don’t need to know Python to use it
- Ansible is Agentless
- Ansible connects to remote servers and nodes using SSH
Some words you’ll come across in the Ansible world.
The entry point for configuration management in Ansible. A playbook is a YAML file that instructs Ansible on what to do. Below is an example of what a playbook looks like:
--- - name: ansible playground hosts: all connection: local tasks: - debug: msg="Hello World!"
Tasks define an operation to be performed on the destination host. From installing a package to staring a service and much more.
Is an INI file that lists and groups our hosts, which Ansible uses to know the destination hosts to apply the configuration to. Hosts entries usually include the hostname and few variables you can pass to Playbooks.
Simply put, Roles are a collection of Tasks. For example, getting Apache up and running usually involves various tasks. To improve efficiency and reduce code duplication, we can group this task into an Apache Role that we can then reuse in multiple Playbooks.
Ansible can be set up in multiple ways, but by far the easiest is by using the Python package manager PIP.
- Install PIP
# Debian/Ubuntu: apt-get install python-pip # RedHat/CentOS/Fedora: yum install python-pip # MacOSX (more info): sudo easy_install pip
- Install Ansible
pip install ansible
- Confirm installation
By default, Ansible on Linux will look for the file
/etc/ansible/hosts. A simple way to startup is to create this file with the below contents;
To define a different location for your inventory file or where are Roles are located, we update the ansible.cfg file.
Ansible will look this file in the current directory, or in your home directory (named .ansible.cfg) and finally in /etc/ansible/ansible.cfg.
[defaults] inventory = hosts remote_user = ubuntu host_key_checking = False
We’re now set up and ready to run our first Playbook. Using the example Playbook above, save it as hello.yml
We have set our hosts to all which means Ansible will target all hosts in our inventory file which at the moment is just 127.0.0.1. We also specified connection is local to tell Ansible to run without SSH.
This should be the structure of our ansible101 folder;
+-- ansible101 +-- hosts.ini +-- ansible.cfg +-- hello.yml
Time to run our playbook;
Anatomy of a Playbook run
- PLAY [ ansible playground ] : The start of the Playbook run
- TASK [ setup ] : Ansible is collecting information from the host and setting up to run the Task
- TASK [ debug ] : Ansible is running the Task
- PLAY RECAP : For each host, the number of tasks resulting in ok, changed, unreachable and failed
Hope you enjoyed the first installment of our Ansible primer series.
Next up in Part 2, now that we’ve set up, configured and run a playbook locally, we can move forward with targeting remote hosts and some more advanced configuration, tips, and tricks.
At OSO Devops, our experts can maintain your DevOps platform and be responsible for day-to-day operational issues, allowing you to develop and ship your product without the need for internal DevOps hires.