Streamline Your IT Operations: Mastering Automation with Ansible

August 10, 2023by Dhawal

Ansible is an open-source automation tool that allows you to manage and configure computer systems and network devices in a simple and efficient way.

It is primarily used for automating tasks such as application deployment, configuration management, and infrastructure provisioning.

In this blog, we will cover all the important aspects of Ansible.

 

Here are some key features and concepts associated with Ansible:

1. Inventory
  • In the inventory file, you specify the machines you want to run ansible tasks on 
  • You can also provide an ansible username, password the machine’s username, and password.

inventory. yaml

---

all:
  vars:
    ansible_connection: ssh
    ansible_port: 22
    ansible_user: username
    ansible_password: password
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
2. Playbooks
  • Playbooks are files that are written in YAML
  • These files are used to run tasks of Ansible
  • Playbooks can include tasks for tasks like package installation, file manipulation, service management, and more.
  • You can create a playbook like below

    playbook. yaml

    ---
    
      - name: Install OpenJDK 8 
        apt:
          name: java-1.8.0-openjdk
          state: present
  • To run this playbook fire the below command

    ansible-playbook -i inventory.yaml playbook.yaml
  • This playbook will install java8 on the machine mentioned in the inventory.yaml file.
3. Modules:
  • Ansible uses modules to perform tasks on remote systems. 
  • Modules are small pieces of code that carry out specific tasks, such as copying files, managing packages, or starting services.
- name: Copy file with owner and permissions
  ansible.builtin.copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'

Here we have used a copy module that will copy a file from our machine to the machine specified in the inventory.

 

   4. Roles:
  • Roles are a way to organize your playbooks and associated files in a structured manner. 
  • They help you break down your automation into manageable components, making your playbooks more reusable and maintainable.
  • To create an ansible role use the following command

    ansible-galaxy init <role-name>
  • A folder will be created which will have the role name that you passed while creating the role.
  • You can verify the structure of the role by using the tree command.

    tree <role-name>

5. Variables:
  • You can define variables in Ansible to make your playbooks more flexible and customizable. 
  • Variables can be defined globally, within playbooks, or even within roles.
6. Conditional Execution:
  • Ansible allows you to add conditions to tasks, allowing certain tasks to run only when specific conditions are met
  • You can refer to the below example for the same.
- name: Install OpenJDK 8 on RHEL/CentOS
    yum:
      name: java-1.8.0-openjdk
      state: present
  when: ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat'


  • Ansible is widely used in DevOps practices for automating infrastructure provisioning, application deployment, configuration management, and more. 
  • It provides a declarative approach to automation, focusing on describing the desired state of systems rather than scripting out the steps to get there. 
  • This makes it a powerful tool for managing large and complex IT environments.

 

Conclusion: Embrace the Automation Revolution

As we conclude our journey through Ansible’s world, remember that automation isn’t just a trend – it’s a revolution. With Ansible, you’re not just saving time; you’re empowering yourself to innovate, solve complex challenges, and elevate your IT game. So, gear up to automate, optimize, and thrive in the dynamic world of IT with Ansible by your side. ??