Ansible – Expanding Playbook Functionality

As the title of this article suggests, now that I have covered how to get a basic Playbook up and running I am going to start building on this to add some more advanced configuration.

Please check out my other articles on getting up and running with Ansible.

All specific Ansible modules and their available options can be found here:

Installing Multiple Packages

The first Playbook I created just installed a single package via yum package manager. I want to add a bit more functionality to this so that I install several packages within the same task. In this case I am going to modify the Playbook to include web tools. The package names have now been moved to a sub-section of the “name” attribute rather than being on the same line.

Start Service Upon Install

The Apache daemon does not start automatically upon successfully installing so I am gong to add some configuration to the Playbook to start Apache so it can be used. To do this I am going to add a “notify” attribute and a “handler” section to the Playbook which I can set to start the service once the Apache install is completed.

When the package install task has completed it will notify the handler “Restart Apache” which will restart the Apache services. The notify and handler names can be anything you want however they must match together.

Add Firewall Configuration

The CentOS template I am using has a default out of the box firewall configuration which means that even though my web services might be running on my test server I won’t be able to get any traffic through. To modify this I can add an additional section to the Playbook to handle the firewall configuration using the “firewalld” Ansible module.

In this case I have moved my notify setting from the Apache install task to the “Open HTTP” task so that the services are restarted after the firewall changes have been performed. Now when I execute the Playbook there are more tasks showing as executed.

When I point my web browser to the test servers IP address I now get a valid response back from the Apache test page.

Customize Web Content

Apache comes out of the box with a default landing page however in most cases this would need customizing or replacing with your own content. There are several ways we can accomplish this. The first is to create an empty file and add content to it via the “copy” Ansible module.

Another option we could use is to host a fully configured web file on the Ansible server and copy it to the correct location using the same “copy”module however this time specifying a “src” attribute.

These are just a few things that can be done with Playbooks from the many modules available.

3 thoughts on “Ansible – Expanding Playbook Functionality

  1. Pingback: Ansible – Getting Started | vnuggets

  2. Pingback: Ansible – Getting Started with Playbooks | vnuggets

  3. Pingback: Ansible – Using the “Vault” | vnuggets

Comments are closed.