Technology

what is Ansible Playbooks

When an organization manages multiple servers, the requirement for automating the tasks is a necessity. There are many cloud automation tools available in the market today. But, in recent times a tool called ansible is buzzing as the best automation tool. Like any other configuration management tool, ansible also requires coding knowledge of the YML language. Ansible is a “radically simple open-source IT automation engine”.

Ansible playbooks are easy to write and understand. The YML language is human readable, understandable but managing correct indentation plays a key role while creating playbooks. Unlike writing a long-running codebook to automate, ansible can automate with a simple YML code called Ansible playbook.

Contents

Ansible playbooks can automate:

  1. Configuration management
  2. Provisioning
  3. Application Deployment
  4. Orchestration

Why Ansible playbooks?

There are some strong reasons behind choosing Ansible as a configuration management tool. Here are some reasons behind choosing an ansible-playbook. Ansible playbooks are:

1.Simple:

Writing Ansible playbooks is quite easy as it doesn’t require any special coding skills. Playbooks are in human-readable format and Tasks are executed in format.

2.Powerful:

Ansible playbooks can automate multiple server tasks like configuration management, Application deployments, server provisioning, and orchestration.

3.Agentless:

This is another major benefit of using ansible as it is agentless and uses OpenSSH. Also, OpenSSH provides high security.

4.Efficient:

The tool is more efficient as it uses the default agent that comes with the tool. It follows a push-based mechanism so that clients can relax as it pushes changes to the target system automatically. It requires fewer resources involvement as it can perform most of the required tasks on its own. Moreover, Ansible is an open-source tool it doesn’t require any licensing.

 

Ansible Inventory file:

We know that Ansible works against multiple managed nodes or hosts in infrastructure at the same time. It performs these tasks using a list or group of lists which are known as inventory. This inventory file contains the list of hosts (nodes) that are managed by an Ansible control node. Information related to hosts is available at the following default path:

Default location:  /etc/ansible/hosts

Anyone can create their own custom host’s file at any location and can use this while running the playbook by adding -i along with the command. Below is the example ansible-playbook command to run a custom inventory file.

Use -i option: ansible -i my_hosts

Another method is to define your inventory file in ansible.cfg file so that it considers that file as the default inventory file.

Ansible playbook examples

The ansible playbooks can launch tasks synchronously or asynchronously. Let’s see some simple basic ansible playbooks to understand how to write Ansible playbooks.

  • Write Ansible playbooks in YML format and save them using the .yml extension.
  • Start creating an ansible playbook with three consequent dashes (—)
  • Every task in the playbook starts with a single dash (-) and a single space
  • Remember that hosts and tasks are mandatory in a playbook
  • Structure the data of ansible-playbook using proper indentation
  • Perform the tasks using modules
  • Like in any other language here also comments start with a # symbol
  1. Create a new user in managed nodes(servers) using the following playbook
---
  - name: Playbook to create new user #name of the playbook
    hosts: all                        #host group name or all hosts
    become: true                      #run playbook as root user
    tasks:                            #startin a new task
    - name: creating new user         #name of the task
      user:                           #task details
        name: john      

2. Simple playbook to install a package in managed nodes (except ubuntu nodes) using the yum module

---
  - name: Playbook to install GIT in managed nodes
    hosts: webservers
    become: true
    tasks:
      - name: install GIT
        yum:
          name: git
          state: installed

3. Ansible playbook to create/remove a file/directory using the file module

#creating a file in appservers
---
- name: Playbook to create a file 
    hosts: appservers
    become: true
    tasks:
      - name: Create a new file in appservers
        file:
          path: /home/ansadmin/samplefile
          state: touch
#creating a directory in appservers
---
- name: playbook create a directory
    hosts: appservers
    become: true
    tasks:
      - name: Creating a directory
        file:
          path: /home/ansadmin/sampldirectory
          state: directory
  # playbook to remove a file or directory
--- 
 - name: playbook to remove a file or directory
    hosts: appservers
    become: true
    tasks:
      - name: remove a file/directory
        file:
          path: /home/ansadmin/sampldirectory or sample file
          state: absent

4. Copy a file from one node to another node using the copy module – Ansible playbook example

---
- name: playbook to copy a file
    hosts: all
    become: true
    tasks:
      - name: copy a file
        copy:                              
          src: /opt/ansible/samplefile.html  #source location
          dest: /home/ansadmin               #destination location
          mode: 0600                         #file permissions
          owner: john                        #file owner

5. Multitasking Ansible playbooks using yum module and service module (except ubuntu server)

---
- name: playbook to install a package
    hosts: all
    become: true
    tasks:
      - name: install httpd         #installing httpd
        yum:                              
          name: httpd
          state: installed              
      - name: start httpd service   #starting httpd service
        service: 
          name: httpd
          state: started
---
- name: playbook to uninstall a package
    hosts: all
    become: true
    tasks:
      - name: stop httpd service       #stoppping httpd service
        service:                              
          name: httpd
          state: stopped
      - name: uninstall httpd          #unstalling httpd
        yum: 
          name: httpd
          state: removed

6. Sample playbook to Install any package onto ubuntu servers

---
  - name: install apache2 on ubuntu server
    hosts: ubuntu server
    become: true
    tasks:
      - name: install apache2
        apt:
          name: apache2
          state: present
      - name: start apache2
        service:
          name: apache2
          state: started

Ansible playbook commands

Here are some basic ansible commands to perform operations on ansible playbooks.

  1. Create an Ansible playbook                                 –> vi playbookname.yml
  2. view playbook                                                       –> cat playbookname.yml
  3. run the playbook                                                    –> ansible-playbook -i playbookname.yml
  4. Check syntax of playbook                                      –> ansible-playbook -i playbookname.yml –check
  5. copy one ansible playbook to another playbook    –> cp playbooksrc.yml playbookdest.yml

Conclusion

Simplicity in writing Ansible playbooks is helping it to overcome its competitors like chef and puppet. Today many cloud providers are choosing ansible tool for configuration management and deployments. Anyone with no prior programming language can easily work with ansible and can create Ansible playbooks. There are many online resources like Youtube, Udemy, docs are available to enhance the ansible knowledge. one can install the Ansible package and practice from any machine as it is an open-source tool. Hope this article is helpful, do comment to know more about ansible or cloud-related queries.

What is Azure Devops and Why Azure Devops?

Top 10 reasons to learn AWS in 2021

Top 10 Technologies to learn in 2021

Leave a Reply

Your email address will not be published. Required fields are marked *