How to write an Ansible playbook

Impression: Jack Wallen

Ansible is a great instrument for taking care of a significant selection of servers. With it, you can create amazingly adaptable, automatic jobs that operate from a centralized server to act on remote hosts. With these duties, named playbooks, you can undertake admin responsibilities that would normally have to have you to log on to each remote device manually and run those people instructions independently.

I’ve currently walked you by means of the process of putting in Ansible on Ubuntu Server 18.04 (See: How to put in Ansible on Ubuntu Server 18.04). Now it’s time to study how to publish a essential Ansible playbook. The moment you fully grasp how Playbooks are created, you can develop quite elaborate scripts that can do any quantity of admin tasks on your remote facts middle servers.

SEE: Catastrophe restoration and company continuity system (Tech Pro Exploration)

What you need

Of course, you want Ansible set up and running, with distant hosts configured. You will also have to have a remote Linux server in your data centre to test on and a person with sudo privileges. I will demonstrate the development of a Playbook that will act on a remote Linux server.

The playbook

The Ansible playbook is a set of guidance that will be executed on a one host or a group of hosts. It is created as a .yaml file (with good YAML formatting). Let’s say you want to install a LAMP stack and your goal server is We’ll phone this playbook lampstack.yaml. The initially portion (that will set up the LAMP stack) of this Playbook will search like this:

#Put in LAMP Stack On Ubuntu Server
  grow to be: certainly

What you see in the previously mentioned are the adhering to:

  • 3 dashes are necessary for YAML formatting.
  • The line starting with # is basically a comment, telling us what the playbook file is.
  • The hosts line is our hosts declaration. This will be examine from the /and so on/ansible/hosts file, so ANSIBLE_HOST will will need to be a host mentioned in that file.
  • come to be: yes This is set to of course to activate privilege escalation.

The future area is the responsibilities portion and appears to be like this:

          - apache2
          - mysql-server
          - php
          - php-mysql
         point out: present
         update_cache: yes

The previously mentioned section declares:

  • apt: We’re calling apt to set up packages
  • pkg: What follows are the deals to be set up.
  • -apache2, -mysql-server, -php, -php-mysql declares which deals are to be mounted.
  • condition: Implies we are install the newest packages.
  • update_cache: Instructs apt to update its cache.

The following portion starts the two the Apache and MySQL products and services and seems like this:

- name: get started apache company
   become: of course
       name: apache2
       state: started
       enabled: indeed

- name: begin mysql provider
   turn into: sure
       identify: mysql
       state: started
       enabled: indeed

At last, we are heading to produce the concentrate on directory (in case the set up will not) and then copy the index.html into it. This segment appears to be like like this:

- identify: produce concentrate on listing
   file: path=/var/www/html point out=listing mode=0755

- title: deploy index.html
   come to be: indeed
       src: /tmp/index.html
       dest: /var/www/html/index.html

Place those people three pieces alongside one another in the single file (named lampstack.yaml – Determine A) and help you save it.

Figure A

Determine A: Our total playbook file.

Next, make the index.html file, with the adhering to content material:


Hi, TechRepublic!

Save that file as /tmp/index.html.

Deploying the playbook

Now that our playbook is prepared, we can now deploy it with the next command:

ansible-playbook lampstack.yml -c paramiko --person=USERNAME --additional-vars "ansible_sudo_pass="PASSWORD"

the place USERNAME is a remote consumer who has sudo privileges and PASSWORD is that distant users’ sudo password.

If the playbook is penned effectively, it ought to choose care of all incorporated duties. You should then be ready to position a browser to http://SERVER_IP (where by SERVER_IP is the IP handle of the target host, described in /and many others/ansible/hosts) and see Hi, TechRepublic! exhibited (Figure B).

Figure B

Determine B: Our index.html file is displayed.

And that’s all there is to creating your very first Ansible playbook. There’s a ton additional to discover, so I propose heading about to the formal Ansible Playbook documentation and reading how to make even a lot more complicated playbooks.

Also see

Fibo Quantum

Be the first to comment

Leave a Reply

Your email address will not be published.