Skip to main content

Command Palette

Search for a command to run...

Day 57 : Ansible recap

DevOps Learning

Updated
2 min read
Day 57 : Ansible recap

On Day 55 and Day 56, we covered the basics of Ansible. Today, we will continue by covering some basics of other Ansible components, which we will discuss in detail in the coming days.

Key Components

  1. Playbooks:

    • Written in YAML.

    • Define a series of tasks to be executed on managed nodes.

    • Example:

        - hosts: webservers
          tasks:
            - name: Install Apache
              yum:
                name: httpd
                state: present
      
  2. Modules:

    • Reusable, standalone scripts that Ansible runs on your nodes.

    • Examples include yum, apt, copy, service, etc.

    • Can be written in any language that can return JSON.

  3. Inventories:

    • Lists of managed nodes.

    • Can be static (defined in a file) or dynamic (generated by scripts).

    • Example:

        [webservers]
        web1.example.com
        web2.example.com
      
  4. Roles:

    • Collections of tasks, variables, files, templates, and modules.

    • Help organize playbooks and reuse code.

    • Example directory structure:

        roles/
          common/
            tasks/
            handlers/
            templates/
            files/
            vars/
            defaults/
            meta/
      
  5. Variables:

    • Allow you to store values that can be reused throughout your playbooks.

    • Can be defined in playbooks, inventories, or external files.

    • Example:

        [webservers]
        web1.example.com http_port=80
        web2.example.com http_port=8080
      
        [databases]
        db1.example.com
        db2.example.com
      
        [all:vars]
        ansible_user=admin
        ansible_ssh_private_key_file=~/.ssh/id_rsa
      
  6. Handlers:

    • Special tasks that are triggered by other tasks.

    • Typically used for service restarts.

    • Example:

        handlers:
          - name: restart apache
            service:
              name: httpd
              state: restarted
      
  7. Templates:

    • Use the Jinja2 templating engine to create dynamic configuration files.

    • Example template (httpd.conf.j2):

        Listen {{ http_port }}
      

Common Use Cases

  • Configuration Management: Ensure systems are in a desired state.

  • Application Deployment: Automate the deployment of applications.

  • Orchestration: Coordinate multiple systems and services.

  • Provisioning: Set up new servers and environments.

  • Continuous Delivery: Integrate with CI/CD pipelines for automated deployments.

Advantages

  • Agentless: No need to install agents on managed nodes.

  • Simple Syntax: Uses YAML, which is easy to read and write.

  • Idempotent: Ensures tasks are only executed when necessary.

  • Extensible: Supports custom modules and plugins.

Conclusion

Ansible is a powerful and flexible tool that can greatly simplify the management of your infrastructure. By leveraging its various components, you can automate complex tasks, ensure consistency across your environments, and improve overall efficiency.

Thank you for reading😉.