Command Line Modules to Ansible Playbooks

Vince Sesto
Mar 6, 2018 · 4 min read

In our previous post we discussed running Ansible Modules from the command line: https://medium.com/splunkuserdeveloperadministrator/ansible-modules-from-the-command-line-9b8da771cb9e

Getting to know Ansible modules this way gives you a great lead in to working with playbooks, which is basically a script of consecutive tasks taking place to get our environment into a specific state.

So why don’t we take the Ansible commands we ran previously and set them up as a playbook to deploy and run Apache on our host. If you haven’t read the previous post, we used the following commands to:

Install apache:

ansible --become -K mylaptop -i hosts -m apt -a "name=apache2 state=present"

Configure the application:

ansible mylaptop --become -K -i hosts -m file -a "path=/tmp/another_test owner=root group=root state=directory"

Then ensure the service was running:

ansible --become -K mylaptop -i hosts -m service -a "name=apache2 state=started"

So to construct a playbook, it is made a lot simpler as we already know the modules we are going to use. Just before we create our playbook, we need to make sure we have a hosts file set up with the specific system we are deploying to. For now we will use the following to deploy to the PC we are currently working on.

Download the code from GitHub at the following location:
https://github.com/vincesesto/blogansibleplaybooks

1. Start by creating the following file as hosts, and use your favorite text editor to add in the following details.

 1  [mylaptop]
2 localhost
3
4 [linux]
5 192.168.56.101

2. Create a new file called apache-server.yml. A you can see with the “yml” extension our playbook is in YAML format. If you haven’t used this before, make sure you are careful with your whitespace and indentation. If something is incorrect, it could leave the playbook with errors and unable to deploy the Apache service.

3. Use your favorite text editor to add in the first three lines of text. The first line lets us know we are using a YAML format file. Line 2 provides a statement telling us we are going to deploy our code onto our host. We normally specified this in our command line, but line two now takes care of that. Line three tells us that we are going to run the rest of the tasks as sudo.

  1 ---
2 - hosts: webserver
3 become: sudo

4. We can then add our first task. All tasks as listed under the tasks statement. For each task that we put in place, we start with a descriptive name which is then displayed in our output when we run the playbook. Underneath the name, we then make a statement with our module name, in this case apt(Or yum if you are using Redhat/CentOS) and then the arguments that need to be provided to the module.

  4   tasks:
5 - name: ensure apache is installed and up to date
6 apt: name=apache2 state=latest

5. As an example, we added in the configuration to use the file module. We will do this again. Once again we see the module name followed by the source and destination of the config file. Line 9 then shows a notify statement. What this does, is it lets Ansible know that if there have been any changes to Apache configuration, we then need to notify the handlers to restart apache, but more on that in a second.

  7   - name: write the apache config file
8 file: src=apache2.conf dest=/etc/apache2/apache2.conf
9 notify:
10 - restart apache

6. The last task we need to put in place is to ensure the service is running and enabled on boot.

 11   - name: apache is running (and enable it at boot)
12 service: name=apache2 state=started enabled=yes

7. Line 13 then adds in the handlers for our playbook. As we saw earlier, we need to perform a restart if there have been any changes to the configuration, and this is set up the same as we would a normal task, but this will not be called unless it is needed.

 13   handlers:
14 - name: restart apache
15 service: name=apache2 state=restarted

8. We can now run the playbook. The command line is a lot simpler as well now as all the details are in the playbook.

ansible-playbook -i hosts apache-server.ymlPLAY [mylaptop] *********************************TASK [Gathering Facts] ***************************
ok: [localhost]
TASK [ensure apache is installed *****************
ok: [localhost]
TASK [write the apache config file] ***********************
ok: [localhost]
TASK [apache is running (and enable it at boot)] **********
ok: [localhost]
PLAY RECAP ************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0

A very simple, playbook but I think it tells us a lot on how we can transfer our knowledge from the command line into an Ansible playbook.

Found this post useful? Kindly tap the clap button below! :)

About The Author

Vince has worked with Splunk for over 5 years, developing apps and reporting applications around Splunk, and now works hard to advocate its success. He has worked as a system engineer in big data companies and development departments, where he has regularly supported, built, and developed with Splunk. He has now published his first book via Packt Publishing — Learning Splunk Web Framework.

Splunk User Developer Administrator

The magazine has been set up as a share of knowledge and…

Splunk User Developer Administrator

The magazine has been set up as a share of knowledge and allow people to come together and provide useful insights into their use of Splunk in their day to day work, in an unbiased and friendly setting.

Vince Sesto

Written by

Vincent Sesto is a DevOps Engineer, Endurance Athlete, Coach and Author. One of his passion’s in life is endurance sports as both an athlete, coach and author.

Splunk User Developer Administrator

The magazine has been set up as a share of knowledge and allow people to come together and provide useful insights into their use of Splunk in their day to day work, in an unbiased and friendly setting.