How to write an Ansible playbook


Buy Website Traffic | Increase Website Traffic | SEO Backlinks | Alexa Ranking

Picture: Jack Wallen

Ansible is a superb instrument for managing numerous servers. With it, you possibly can create extremely versatile, automated duties that run from a centralized server to behave on distant hosts. With these duties, referred to as playbooks, you possibly can undertake admin duties that will 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 way to set up Ansible on Ubuntu Server 18.04). Now it is time to learn to write a primary Ansible playbook. When you perceive how Playbooks are written, you possibly can create very complicated scripts that may do any variety of admin duties in your distant information middle 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 information middle to check on and a person with sudo privileges. I am going to display the creation of a Playbook that may 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 bunch of hosts. It’s written as a .yaml file (with correct YAML formatting). As an example you wish to set up a LAMP stack and your goal server is We’ll name this playbook lampstack.yaml. The primary part (that may set up the LAMP stack) of this Playbook will appear to be this:

#Set up LAMP Stack On Ubuntu Server
  grow 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 so on/ansible/hosts file, so ANSIBLE_HOST will should be a bunch listed in that file.
  • grow 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 packages
  • pkg: 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 companies and appears like this:

- identify: begin apache service
   grow to be: sure
       identify: apache2
       state: began
       enabled: sure

- identify: begin mysql service
   grow to be: sure
       identify: mysql
       state: began
       enabled: sure

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

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

- identify: deploy index.html
   grow 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.

Figure AFigure A

Determine A: Our full playbook file.

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

    <h1>Whats up, TechRepublic!</h1>

Save that file as /tmp/index.html.

Deploying the playbook

Now that our playbook is prepared, we are able to 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 care for all included duties. It’s best to then be capable of level a browser to http://SERVER_IP (the place SERVER_IP is the IP deal with of the goal host, outlined in /and so on/ansible/hosts) and see Whats up, TechRepublic! displayed (Determine B).

Figure BFigure B

Determine B: Our index.html file is displayed.

And that is all there may be to creating your first Ansible playbook. There’s much more to study, so I counsel heading over to the official Ansible Playbook documentation and studying tips on how to create much more complicated playbooks.

Additionally see

Buy Website Traffic | Increase Website Traffic | SEO Backlinks | Alexa Ranking

Source link