The best way to write an Ansible playbook

Picture: Jack Wallen

Ansible is a good instrument for managing a lot of servers. With it, you may create extremely versatile, automated duties that run from a centralized server to behave on distant hosts. With these duties, referred to as playbooks, you may undertake admin duties that may in any other case require you to log onto every distant machine manually and run these instructions individually.

I’ve already walked you thru the method of putting in Ansible on Ubuntu Server 18.04 (See: The best way to set up Ansible on Ubuntu Server 18.04). Now it is time to discover ways to write a primary Ansible playbook. When you perceive how Playbooks are written, you may create very advanced scripts that may do any variety of admin duties in your distant knowledge heart servers.

SEE: Catastrophe restoration and enterprise continuity plan (Tech Professional Analysis)

What you want

Clearly, you want Ansible put in and working, with distant hosts configured. Additionally, you will want a distant Linux server in your knowledge heart to check on and a person with sudo privileges. I will exhibit the creation of a Playbook that can act on a distant Linux server.

The playbook

The Ansible playbook is a set of directions that will likely be executed on a single host or a gaggle of hosts. It’s written as a .yaml file (with correct YAML formatting). As an instance you need to set up a LAMP stack and your goal server is We’ll name this playbook lampstack.yaml. The primary part (that can set up the LAMP stack) of this Playbook will seem like this:

#Set up LAMP Stack On Ubuntu Server
turn out to be: sure

What you see within the above are the next:

Three dashes are required for YAML formatting.The road starting with # is solely a remark, telling us what the playbook file is.The hosts line is our hosts declaration. This will likely be learn from the /and many others/ansible/hosts file, so ANSIBLE_HOST will have to be a bunch listed in that file.turn out to be: sure That is set to sure to activate privilege escalation.

The subsequent part is the duties part and appears like this:

– apache2
– mysql-server
– php
– php-mysql
state: current
update_cache: sure

The above part declares:

apt: We’re calling apt to put in packagespkg: What follows are the packages to be put in.-apache2, -mysql-server, -php, -php-mysql declares which packages are to be put in.state: Signifies we’re set up the newest packages.update_cache: Instructs apt to replace its cache.

The subsequent part begins each the Apache and MySQL providers and appears like this:

– identify: begin apache service
turn out to be: sure
identify: apache2
state: began
enabled: sure

– identify: begin mysql service
turn out to be: sure
identify: mysql
state: began
enabled: sure

Lastly, we’ll create the goal listing (in case the set up does not) after which copy the index.html into it. This part appears to be like like this:

– identify: create goal listing
file: path=/var/www/html state=listing mode=0755

– identify: deploy index.html
turn out to be: sure
src: /tmp/index.html
dest: /var/www/html/index.html

Put these three items collectively within the single file (named lampstack.yaml – Determine A) and put it aside.

Determine A: Our full playbook file.

Subsequent, create the index.html file, with the next content material:

Whats up, TechRepublic!

Save that file as /tmp/index.html.

Deploying the playbook

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

ansible-playbook lampstack.yml -c paramiko –user=USERNAME –extra-vars “ansible_sudo_pass=”PASSWORD”

the place USERNAME is a distant person who has sudo privileges and PASSWORD is that distant customers’ sudo password.

If the playbook is written appropriately, it ought to handle all included duties. You need to then be capable to level a browser to http://SERVER_IP (the place SERVER_IP is the IP deal with of the goal host, outlined in /and many others/ansible/hosts) and see Whats up, TechRepublic! displayed (Determine B).

Determine B: Our index.html file is displayed.

And that is all there’s to creating your first Ansible playbook. There’s much more to study, so I recommend heading over to the official Ansible Playbook documentation and studying find out how to create much more advanced playbooks.

Knowledge Heart Tendencies E-newsletter

DevOps, virtualization, the hybrid cloud, storage, and operational effectivity are simply a number of the knowledge heart subjects we’ll spotlight.
Delivered Mondays and Wednesdays

Enroll at present

Additionally see

Related posts

Leave a Comment