Using Ansible Playbooks

The instructions define the use and work with Ansible Playbook, in addition to a quick introduction of their structure.

You can read about how to install Ansible on in our other guide.

What is Ansible Playbooks?

A playbook in Ansible specifies a collection of some activities to be done and dealt with to specific collections of web . Unlike some other tuning tools, Playbook does not describe the state of the maker, as well as Ansible establishes all the changes that need to be made by itself. However, playbooks must be made as idempotents, which suggests that they can be run more than when without adverse repercussions.

Playbooks are usually utilized to carry out initial web server arrangement – including customers and directories, managing packages as well as .

A playbook is a YAML file that usually has the following structure:

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

, the following playbook will belong to all servers in the marketingservers group and will run the web server:

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

The playbook above shows an example of a task:

  - 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 logged and can help track progress. After the name line (name) is the module that will be launched, in this case is the service module. Other attributes allow more options, the Ansible example allows sudo privileges.

Running Ansible Playbook

You can run the finished playbook using the following command:

ansible-playbook playbook.yml

For example:

ansible-playbook .yml

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

ansible-playbook -l host_subset playbook.yml

For example:

ansible-playbook -l host3 nginx.yml

Registration of results

When you install and configure services manually, you almost always need to know the outcome of your actions. You can configure this functionality using registration.

For each task, if desired, it is possible to log its result (failure or success) in a variable that can be checked later. When using this functionality, it’s a good idea to tell Ansible to ignore errors for such a task, as it’s common for the playbook to abort if there are any problems.

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

For example, let’s tell playbook to load the index. file if it exists. If this task fails, the index.html file will start loading:

- hosts:droplets
    -name: Installs nginx webserver
      apt: pkg=nginx state=installed update_cache=true
        - 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

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

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


Welcome to the world of DomainRooster, where roosters (and hens) rule the roost! We're a one-stop shop for all your entrepreneurial needs, bringing together domain names and website hosting, and all the tools you need to bring your ideas to life. With our help, you'll soar to new heights and hatch great success. Think of us as your trusty sidekick, always there to lend a wing and help you navigate the sometimes-complex world of domain names and web hosting. Our team of roosters are experts in their fields and are always on hand to answer any questions and provide guidance. So why wait? Sign up today and join the ranks of the world's greatest entrepreneurs. With DomainRooster, the sky's the limit! And remember, as the saying goes, "Successful people do what unsuccessful people are not willing to do." So don't be afraid to take that leap of faith - DomainRooster is here to help you reach for the stars. Caw on!