How to write an Ansible playbook

Learn how to create a playbook to install a LAMP stack on a Ubuntu Server.

ansiblehero.jpg
Image: Jack Wallen

Ansible is a great tool for managing a large number of servers. With it, you can create incredibly flexible, automated tasks that run from a centralized server to act on remote hosts. With these tasks, called playbooks, you can undertake admin duties that would otherwise require you to log onto each remote machine manually and run those commands individually.

I've already walked you through the process of installing Ansible on Ubuntu Server 18.04 (See: How to install Ansible on Ubuntu Server 18.04). Now it's time to learn how to write a basic Ansible playbook. Once you understand how Playbooks are written, you can create very complex scripts that can do any number of admin tasks on your remote data center servers.

SEE: Disaster recovery and business continuity plan (Tech Pro Research)

What you need

Obviously, you need Ansible installed and running, with remote hosts configured. You will also need a remote Linux server in your data center to test on and a user with sudo privileges. I'll demonstrate the creation of a Playbook that will act on a remote Linux server.

The playbook

The Ansible playbook is a set of instructions that will be executed on a single host or a group of hosts. It is written as a .yaml file (with proper YAML formatting). Let's say you want to install a LAMP stack and your target server is 192.168.1.100. We'll call this playbook lampstack.yaml. The first section (that will install the LAMP stack) of this Playbook will look like this:

---
#Install LAMP Stack On Ubuntu Server
- hosts: ANSIBLE_HOST
  become: yes

What you see in the above are the following:

  • Three dashes are required for YAML formatting.
  • The line beginning with # is simply a comment, telling us what the playbook file is.
  • The hosts line is our hosts declaration. This will be read from the /etc/ansible/hosts file, so ANSIBLE_HOST will need to be a host listed in that file.
  • become: yes This is set to yes to activate privilege escalation.

The next section is the tasks section and looks like this:

    apt:
        pkg:
          - apache2
          - mysql-server
          - php
          - php-mysql
         state: present
         update_cache: yes

The above section declares:

  • apt: We're calling apt to install packages
  • pkg: What follows are the packages to be installed.
  • -apache2, -mysql-server, -php, -php-mysql declares which packages are to be installed.
  • state: Indicates we're install the latest packages.
  • update_cache: Instructs apt to update its cache.

The next section starts both the Apache and MySQL services and looks like this:

- name: start apache service
   become: yes
   service:
       name: apache2
       state: started
       enabled: yes

- name: start mysql service
   become: yes
   service:
       name: mysql
       state: started
       enabled: yes

Finally, we're going to create the target directory (in case the installation doesn't) and then copy the index.html into it. This section looks like this:

- name: create target directory
   file: path=/var/www/html state=directory mode=0755

- name: deploy index.html
   become: yes
   copy:
       src: /tmp/index.html
       dest: /var/www/html/index.html

Put those three pieces together in the single file (named lampstack.yaml - Figure A) and save it.

Figure A

Figure A: Our complete playbook file.

Next, create the index.html file, with the following content:

<html>
  <body>
    <h1>Hello, TechRepublic!</h1>
  </body>
</html>

Save that file as /tmp/index.html.

Deploying the playbook

Now that our playbook is ready, we can now deploy it with the following command:

ansible-playbook lampstack.yml -c paramiko --user=USERNAME --extra-vars "ansible_sudo_pass="PASSWORD"

where USERNAME is a remote user who has sudo privileges and PASSWORD is that remote users' sudo password.

If the playbook is written correctly, it should take care of all included tasks. You should then be able to point a browser to http://SERVER_IP (where SERVER_IP is the IP address of the target host, defined in /etc/ansible/hosts) and see Hello, TechRepublic! displayed (Figure B).

Figure B

Figure B: Our index.html file is displayed.

And that's all there is to creating your first Ansible playbook. There's a lot more to learn, so I suggest heading over to the official Ansible Playbook documentation and reading how to create even more complex playbooks.

Also see