07.06.2023

Using Ansible Playbooks

The instructions describe the application and work with Ansible Playbook, as well as a brief overview of their structure.

What are Ansible Playbooks?

The playbook in Ansible defines a series of certain actions to perform and are addressed to a specific set of servers. Unlike some other tools for making settings, the Playbook does not describe the state of the machine, and Ansible independently determines all the changes that need to be made. However, playbooks must be designed as idempotents, which means that they can be launched more than once without negative consequences.

Often, playbooks are used to perform the initial setup of servers - adding users and directories, managing software packages and files.

Playbook is a YAML file , which usually has the following structure:

---
- hosts: [target hosts]
  remote_user: [yourname]
  tasks:
    - [task 1]
    - [task 2]

For example, the following playbook will be included on all servers of the marketing servers group and will ensure that the Apache webserver starts:

---
- hosts: [marketingservers]
  remote_user: webadmin
  tasks:
    - name: Ensure the Apache daemon has started
      service: name=httpd state=started
      become: yes
      become_method: sudo

In the playbook above is an example task:

tasks:
  - name: Ensure the Apache daemon has started
    service: name=httpd state=started
    become: yes
    become_method: sudo

Each task should have a name that is subsequently recorded and can help track progress. After the name line is the module that will be launched, in this case, it is a service module. Other attributes allow more options; in the Ansible example, sudo privileges are allowed.

Launch Ansible Playbook

You can launch a ready-made playbook using the following command:

ansible-playbook playbook.yml

For example:

ansible-playbook nginx.yml

However, if you need to filter the list of hosts so that the script applies to only one of these hosts, you can add a flag and specify a subset of hosts in the file:

ansible-playbook -l host_subset playbook.yml

For example:

ansible-playbook -l host3 nginx.yml

Registering results

When you install and configure services manually, it is almost always necessary to know the result of actions. You can configure this functionality through registration.

For each task, if desired, it is possible to register its result (failure or success) in a variable, which can be checked later. When using this functionality, it is recommended to instruct Ansible to ignore errors for such a task, since usually the playbook is interrupted in case of any problems.

Thus, if you need to check whether the task has been completed or not and decide on the next steps, you must use the registration functionality.

For example, specify playbook to upload a file index.php if it exists. If this task does not complete, then the download of the index.html file will begin:

---
- hosts: droplets
  tasks:
    - name: Installs nginx web server
      apt: pkg=nginx state=installed update_cache=true
      notify:
        - start nginx

    - name: Upload default index.php for host
      copy: src=static_files/index.php dest=/usr/share/nginx/www/ mode=0644
      register: php
      ignore_errors: True

    - name: Remove index.html for host
      command: rm /usr/share/nginx/www/index.html
      when: php|success

    - name: Upload default index.html for host
      copy: src=static_files/index.html dest=/usr/share/nginx/www/ mode=0644
      when: php|failed

  handlers:
    - name: start nginx
      service: name=nginx state=started

This script tries to upload a PHP file to the host. Ansible records the success of an operation in a variable called php. If this operation is successful, the next task is to delete the index.html file. If the operation failed, the index.html file will be loaded.