Chapter 2. Playbooks: A Beginning

Most of your time in Ansible will be spent writing playbooks. A playbook is the term that Ansible uses for a configuration management script. Let’s look at an example: installing the Nginx web server and configuring it for secure communication.

If you’re following along in this chapter, you should end up with the files listed here:

  • playbooks/ansible.cfg

  • playbooks/hosts

  • playbooks/Vagrantfile

  • playbooks/web-notls.yml

  • playbooks/web-tls.yml

  • playbooks/files/nginx.key

  • playbooks/files/nginx.crt

  • playbooks/files/nginx.conf

  • playbooks/templates/index.html.j2

  • playbooks/templates/nginx.conf.j2

Some Preliminaries

Before we can run this playbook against our Vagrant machine, we need to expose ports 80 and 443, so we can access them. As shown in Figure 2-1, we are going to configure Vagrant so that requests to ports 8080 and 8443 on our local machine are forwarded to ports 80 and 443 on the Vagrant machine. This will allow us to access the web server running inside Vagrant at http://localhost:8080 and https://localhost:8443.

Vagrant port forwarding
Figure 2-1. Exposing ports on Vagrant machine

Modify your Vagrantfile so it looks like this:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "forwarded_port", guest: 443

Get Ansible: Up and Running, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.