Introduction to Ansible - Part 4

Introduction to Ansible - Part 4

In the past 3 posts I did a quick Introduction to Ansilble, a quick overview of some of the important config files that you should be familiar with and how to run Ansible commands you can find the links to Part 1, 2  and 3 here:

https://bobbyiliev.com/blog/introduction-to-ansible-part-1

https://bobbyiliev.com/blog/introduction-to-ansible-part-2

https://bobbyiliev.com/blog/introduction-to-ansible-part-3

Overview

In this part we will focus on what Ansible playbooks are, we create and run a few playbooks as well so make sure to have your 3 servers up and running and be able to follow along.

If you don't have your 3 servers yet make sure to check the part 2 of the series! You can get a free $50 credit that you could use to deploy your virtual machines and test the guide yourself on a few Digital Ocean servers:

Digital Ocean $50 Free Credit

Ansible Playbooks

As you might remember from the previous post the Ansible Ad-hoc commands are just like bash commands that you run via your terminal, on the other side the Ansible playbooks are like bash scripts, they take many steps and can be executed on many hosts.

The playbooks are written in yaml format. If you are not familiar with yaml I would suggest that you go though a few courses to get your self up to speed as yaml is basically used everywhere nowadays. One thing to keep in mind is that the spacing in yaml is really important, even if you have one extra space the playbook would not work.

To run a playbook we would use the ansible-playbook command followed by the playbook yaml file. Example:

ansible-playbook install.yml

The playbooks consist of a number of so called plays. What a single play is just a task that has to be executed on certain host.

Here's a very simple Ansible playbook:

# Example playbook
---
- hosts: webservers
  become: yes
tasks:
- name: Install Apache2 Latest
  apt:
    name: apache2
    state: latest
- name: create test.html
  file:
    name: /var/www/html/test.html
    state: touch
- name: Add some content to the test.html file
  lineinfile:
    line: "Bobby Iliev Ansible"
    path: /var/www/html/test.html
- name: Start Apache2
  service:
    name: apache2
    state: started
- hosts: dbservers
become: yes
tasks:
- name: Latest MySQL
  apt:
    name: mysql-server
      state: latest
  - name: Start MySQL
    service:
      name: mysql
      state: started

Side note, as a good practice I would recommend using version control for your playbooks. I would use git for example. 

Quick rundown of the playbook

# This is just a comment

---  specifies the beginning of our playbook

- hosts  specifies the hosts that we would like to run the commands against

become: yes  this means that we would run all commands as root

tasks: specifies a list of the commands that we would be executing

-name - specifies the name of the task

You can copy and paste this to a install.yml file and run the playbook with this command:

ansible-playbook install.yml

You should get a similar output:

Ansible Playbooks Bobby

After that verify that your apache2 was installed and started and also that the test.html file was updated by visiting your IP address via your browser or run

curl http://your_server_ip/test.html

Output:

Ansible Playbooks 2

Note: you should write your playbooks in a way that it is safe to run the playbook multiple times against one and the same target without any negative impact.

Ansible variables

You can use letters, numbers and underscores for your variable naming conversion. You can pass your variables as an argument using the -e flag or you could specify the variables directly in your playbook.

In your playbook you would have something like this after the start of the playbook before our tasks:

---
- hosts: webservers
vars:
my_service: apache2
desired_state: started

tasks:
- name: Apache started
  service:
    name:  "{{ my_service }}"
    state: "{{ desired_state }}"

Or we could skip the vars part and specify it in our command directly:

ansible-playbook start.yml -e "my_service=apache2 desired_state=started"

Output:

Ansible playbooks variables

For more information you can visit the official Ansible Playbooks documentation:

https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html

That's pretty much it for the Ansible Playbooks. We can now move to the ansible handlers and some useful tips:

https://bobbyiliev.com/blog/introduction-to-ansible-part-5

Feel free to reach out to me in case that you have any questions!

Coffee For Me