Everything about Ansible

Everything about Ansible

Day 57 to 59 for 90DaysOfDevOps

ยท

7 min read

Day 57: What is Ansible?

Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It allows users to automate repetitive tasks, such as provisioning servers, deploying applications, and managing configurations, across a large number of servers.

Some key properties and concepts of Ansible:

  1. Agentless: Ansible is agentless, meaning it doesn't require any agent software to be installed on managed hosts. Instead, it uses SSH (Secure Shell) and Python to communicate with remote hosts, making it lightweight and easy to set up.

  2. Infrastructure as Code (IaC): Ansible promotes the Infrastructure as Code paradigm, allowing you to describe your infrastructure and configuration in simple, human-readable YAML (Yet Another Markup Language) files called playbooks. These playbooks define the desired state of the system, and Ansible takes care of bringing the system to that state.

  3. Declarative: Ansible playbooks are declarative, meaning you describe the desired state of the system rather than specifying a sequence of steps to achieve that state. Ansible handles the details of how to achieve the desired state, which simplifies automation and makes playbooks more readable and maintainable.

  4. Idempotent: Ansible tasks are idempotent, meaning they can be run multiple times without changing the result beyond the initial desired state. This ensures that running a playbook multiple times has the same effect as running it once, reducing the risk of unintended changes and making automation more reliable.

  5. Modules: Ansible provides a large collection of built-in modules that perform specific tasks, such as managing packages, files, services, users, and more. Modules can be executed directly from the command line or used within playbooks to automate various tasks on managed hosts.

  6. Inventory: Ansible uses an inventory file to define the hosts it will manage. The inventory file can be a simple text file or generated dynamically from external sources. It allows you to organize hosts into groups and assign variables to them, making it easy to target specific hosts or groups of hosts in your playbooks.

  7. Playbooks: Playbooks are YAML files that define a set of tasks to be executed on remote hosts. Each task consists of one or more Ansible modules along with their arguments. Playbooks can also include variables, conditionals, loops, and handlers to define complex automation workflows.

  8. Roles: Roles are a way to organize and reuse Ansible playbooks and associated files. A role encapsulates a set of related tasks, variables, files, and templates into a reusable package that can be easily shared and reused across different projects.

  9. Templates: Ansible allows you to use Jinja2 templating language to dynamically generate configuration files and scripts based on variables and conditions. Templates are useful for creating configuration files that need to be customized for each host or environment.

  10. Vault: Ansible Vault allows you to encrypt sensitive data, such as passwords and private keys, within playbooks and files. This ensures that sensitive information is stored securely and can only be accessed by authorized users with the appropriate decryption keys.

Day 58: Ansible Playbooks

Ansible playbooks are a core component of Ansible automation. They are YAML files that define a series of tasks to be executed on remote hosts. Playbooks allow you to automate complex tasks, configure systems, deploy applications, and orchestrate entire workflows in a declarative manner.

Ansible playbooks:

  1. YAML Format: Playbooks are written in YAML (YAML Ain't Markup Language), a human-readable data serialization format. YAML syntax is indentation-based, making it easy to read and write.

  2. Tasks: Playbooks consist of a list of tasks, each representing a specific action to be performed on managed hosts. Tasks can include Ansible modules, shell commands, or even calls to other playbooks or roles.

  3. Modules: Ansible provides a wide range of built-in modules that perform various tasks, such as managing files, packages, services, users, and more. Playbooks use these modules to interact with remote hosts and configure them according to the desired state.

  4. Hosts: Playbooks can target one or more hosts defined in the Ansible inventory. You can specify hosts directly in the playbook or use patterns to target hosts based on attributes such as hostname, IP address, or group membership.

  5. Variables: Playbooks can define variables to parameterize tasks and make them reusable across different hosts or environments. Variables can be defined at different levels, including playbook level, role level, or inventory level.

  6. Handlers: Handlers are tasks that are triggered by other tasks when specific conditions are met. For example, you can define a handler to restart a service only if it has been modified by a preceding task.

  7. Conditionals and Loops: Playbooks support conditional statements and loops, allowing you to control the flow of execution based on specific conditions or iterate over lists of items.

  8. Roles: Playbooks can organize tasks into roles, which encapsulate a set of related tasks, variables, and files into a reusable package. Roles allow you to modularize your automation code and promote code reuse across different projects.

  9. Includes and Imports: Playbooks can include or import other YAML files, allowing you to split large playbooks into smaller, more manageable chunks. This improves readability and maintainability of your automation code.

  10. Error Handling: Playbooks include mechanisms for error handling, such as retries, ignore_errors, and fail-fast options, to handle unexpected situations gracefully and ensure reliable execution.

Playbook example:

---
- name: Install and start Nginx
  hosts: web_servers
  become: true  # Run tasks with sudo privileges

  tasks:
    - name: Install Nginx package
      apt:
        name: nginx  # Package name
        state: present  # Ensure the package is installed

    - name: Ensure Nginx service is running
      service:
        name: nginx  # Service name
        state: started  # Ensure the service is running
        enabled: true  # Enable the service to start on boot

In this example:

  • name: Descriptive name of the playbook.

  • hosts: Specifies the target hosts where the tasks will be executed. In this case, it targets hosts in the web_servers group, which should be defined in the Ansible inventory.

  • become: Indicates that tasks should be executed with sudo privileges.

  • tasks: List of tasks to be executed.

For each task:

  • name: Descriptive name of the task.

  • apt: Ansible module used to manage packages on Debian/Ubuntu systems. It installs the Nginx package (name: nginx) and ensures it is present (state: present).

  • service: Ansible module used to manage services. It ensures the Nginx service is running (state: started) and enabled to start on boot (enabled: true).

To run this playbook, you would save it to a file (e.g., install_nginx.yml) and use the ansible-playbook command:

ansible-playbook install_nginx.yml

This playbook will be applied to all hosts in the web_servers group defined in the Ansible inventory, installing Nginx and ensuring it is running on each host.

Day 59: Ansible Task

  • Install Ansible in host server

  • copy the private key from local to Host server (Ansible_host) at (/home/ubuntu/.ssh)

  • access the inventory file using sudo vim /etc/ansible/hosts

  • Create a playbook to install Nginx

  • deploy a sample webpage using the ansible playbook

Install Ansible in host server

yamlCopy code# install_ansible.yml
---
- name: Install Ansible
  hosts: localhost
  become: true

  tasks:
    - name: Install Ansible
      apt:
        name: ansible
        state: present

Copy the private key from local to Host server (Ansible_host) at (/home/ubuntu/.ssh)

yamlCopy code# copy_private_key.yml
---
- name: Copy private key to Host server
  hosts: ansible_host
  become: true

  tasks:
    - name: Copy private key
      copy:
        src: home/download/private_key.pem
        dest: /home/ubuntu/.ssh/
        mode: 0600

Access the inventory file using sudo vim /etc/ansible/hosts

For this task, you'll manually access the inventory file and update it with the appropriate host configurations.

Create a playbook to install Nginx

# install_nginx.yml
---
- name: Install Nginx
  hosts: web_servers
  become: true

  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present

Deploy a sample webpage using the Ansible playbook

# deploy_sample_webpage.yml
---
- name: Deploy sample webpage
  hosts: web_servers
  become: true

  tasks:
    - name: Copy index.html
      copy:
        content: "<html><body><h1>Hello from Ansible!</h1></body></html>"
        dest: /var/www/html/index.html

You would execute these playbooks one by one using the ansible-playbook command:

ansible-playbook install_ansible.yml
ansible-playbook copy_private_key.yml
# Manually edit /etc/ansible/hosts
ansible-playbook install_nginx.yml
ansible-playbook deploy_sample_webpage.yml

.

.

.

.

๐Ÿ™Œ Thank you for reading my blog! ๐Ÿš€ If you found this information helpful, drop a comment and spread the knowledge! ๐Ÿ” For more Kubernetes insights and updates, feel free to follow me on:

LinkedIn: Sumit Katkar ๐ŸŒ

HashNode: Sumit Katkar's Blog ๐Ÿ“

Happy Learning! ๐ŸŽ‰

ย