DEV Community

Mohammad
Mohammad

Posted on

Mastering Automation with Ansible

What is Ansible ?

Image description

Ansible is an open-source automation tool that is used for configuration management, application deployment, task automation, and orchestration. It simplifies the management of IT infrastructure by allowing you to define and automate the tasks and processes required to maintain and deploy software, configure systems, and manage various resources in a consistent and efficient manner.

Ansible Architecture

Image description

At a high level, Ansible works by connecting to target hosts (remote systems) and executing tasks defined in playbooks. Here's a minimal overview of how Ansible operates:

Playbook Creation: You create a playbook using a YAML-formatted file. The playbook contains a list of tasks and specifies which hosts or groups of hosts these tasks should run on.

Inventory Definition: You define an inventory, which lists the hosts or groups of hosts that Ansible will manage. This can be done in an inventory file or using dynamic inventory sources.

Playbook Execution: You run the Ansible playbook on a control node. Ansible connects to the target hosts via SSH (for Unix-based systems) or WinRM (for Windows-based systems) to execute tasks. It uses the SSH or WinRM credentials specified in the inventory file.

Task Execution: Ansible iterates through the tasks in the playbook, executing them one by one on the target hosts. Each task corresponds to a module, which performs a specific action (e.g., installing software, configuring a service, copying files).

Gathering Facts: Before executing tasks, Ansible typically gathers system information or "facts" from the target hosts. These facts can be used as variables in the playbook.

Idempotent Execution: Ansible is idempotent, which means it ensures the desired state of the system, and running the same playbook multiple times will not have unintended side effects. It will only make necessary changes to achieve the desired state.

Reporting: Ansible provides feedback and reports on the success or failure of each task on each target host, allowing you to monitor the automation process.

Handlers: If tasks trigger handlers (e.g., a service restart), those handlers are executed at the appropriate point in the playbook.

Completion: Once all tasks are executed, the Ansible playbook completes, and you receive a summary of the results, indicating which tasks were successful and which failed.

Ansible Installation as a docker container

You can run Ansible as a Docker container to use it without installing it directly on your local machine. Here's how to set up Ansible inside a Docker container:

1. Pull the Ansible Docker Image: You can use the official Ansible Docker image from Docker Hub. Pull the image using the following command:

   docker pull ansible/ansible:latest
Enter fullscreen mode Exit fullscreen mode

This command fetches the latest Ansible image from Docker Hub.

2. Run the Ansible Container: You can start an interactive session in the Ansible container to run Ansible commands. Here's a basic command:

   docker run -it --rm -v /path/to/your/ansible/playbooks:/ansible/playbooks ansible/ansible:latest
Enter fullscreen mode Exit fullscreen mode
  • -it: This flag allows you to interact with the container.
  • --rm: This flag removes the container after you exit the session.
  • -v /path/to/your/ansible/playbooks:/ansible/playbooks: This flag mounts your local Ansible playbooks directory into the container.

Replace /path/to/your/ansible/playbooks with the actual path to your Ansible playbooks on your local machine.

3. Inside the Ansible Container: Once inside the container, you can run Ansible commands as you would on a regular system. For example:

   ansible-playbook /ansible/playbooks/your_playbook.yml
Enter fullscreen mode Exit fullscreen mode

This command runs an Ansible playbook located in the mounted directory.

By running Ansible inside a Docker container, you keep your local machine clean from Ansible installations and dependencies. It also allows you to easily manage different versions of Ansible for various projects by pulling different Docker images as needed.

Ansible playbooks

Ansible playbooks are configuration management and automation files written in YAML format. Playbooks define a set of tasks, roles, and variables to be executed on one or more target hosts. Here's a simple example of an Ansible playbook to give you an introduction to how playbooks work.

Suppose you want to create a playbook that installs and configures the Nginx web server on a set of remote servers.

1. Inventory File (hosts.ini): Create an Ansible inventory file listing the target servers you want to manage. For example:

[web_servers]
server1 ansible_host=webserver1.example.com
server2 ansible_host=webserver2.example.com
Enter fullscreen mode Exit fullscreen mode

2. Ansible Playbook (webserver.yml): Create an Ansible playbook to install and configure Nginx on the target servers:

---
- name: Install and Configure Nginx
  hosts: web_servers
  become: yes
  tasks:
    - name: Update package cache
      package:
        name: "{{ item }}"
        state: latest
      loop:
        - nginx
      when: ansible_os_family == 'Debian'

    - name: Install Nginx (for Red Hat/CentOS)
      yum:
        name: nginx
        state: latest
      when: ansible_os_family == 'RedHat'

    - name: Start Nginx service
      service:
        name: nginx
        state: started
        enabled: yes
Enter fullscreen mode Exit fullscreen mode

This playbook consists of the following elements:

  • name: A description of the playbook.
  • hosts: The group of hosts from the inventory file that this playbook targets.
  • become: This indicates that tasks should be run with sudo privileges.
  • tasks: A list of tasks to perform on the target hosts.

In this example, we first update the package cache for Debian-based systems, then install Nginx using the package module. For Red Hat/CentOS-based systems, we use the yum module to install Nginx. Finally, we use the service module to start and enable the Nginx service.

3. Running the Playbook: You can execute the playbook using the ansible-playbook command:

ansible-playbook -i hosts.ini webserver.yml
Enter fullscreen mode Exit fullscreen mode

This will apply the tasks defined in the playbook to the hosts listed in the inventory file.

Of course, this is a basic example. Ansible playbooks can become much more complex as you include variables, roles, conditionals, and more advanced tasks. You can also use Ansible Galaxy to find and reuse pre-made roles to simplify your playbook development.

Ansible Install MySQL

To install MySQL using Ansible, you'll need to create an Ansible playbook that defines the tasks required to install MySQL on your target servers. Here's a basic example of how you can do this:

*1. Inventory File (hosts.ini):
*
Create an Ansible inventory file that lists the target servers where you want to install MySQL. For example:

[mysql_servers]
server1 ansible_host=your_server1_ip
server2 ansible_host=your_server2_ip
Enter fullscreen mode Exit fullscreen mode

2. Ansible Playbook (install_mysql.yml): Create an Ansible playbook that installs MySQL. Here's an example playbook:

---
- name: Install MySQL
  hosts: mysql_servers
  become: yes
  tasks:
    - name: Update apt package cache (for Debian/Ubuntu)
      apt:
        update_cache: yes
      when: ansible_os_family == 'Debian'

    - name: Install MySQL Server
      apt:
        name: mysql-server
        state: present
      when: ansible_os_family == 'Debian'

    - name: Start MySQL Service
      service:
        name: mysql
        state: started
      when: ansible_os_family == 'Debian'

    - name: Install MySQL Server (for Red Hat/CentOS)
      yum:
        name: mysql-server
        state: present
      when: ansible_os_family == 'RedHat'

    - name: Start MySQL Service (for Red Hat/CentOS)
      service:
        name: mysqld
        state: started
      when: ansible_os_family == 'RedHat'
Enter fullscreen mode Exit fullscreen mode

In this playbook, we use the apt module for Debian/Ubuntu-based systems and the yum module for Red Hat/CentOS-based systems to install MySQL. The service module is used to ensure that the MySQL service is started.

3. Running the Playbook: You can execute the playbook using the ansible-playbook command, specifying the inventory file:

ansible-playbook -i hosts.ini install_mysql.yml
Enter fullscreen mode Exit fullscreen mode

Conclusion

Image description

Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It simplifies IT infrastructure management by allowing you to define and automate tasks in a structured manner. Ansible operates through playbooks, inventory management, and task execution. You can run Ansible in a Docker container for a clean local environment and efficient management of different Ansible versions. Ansible playbooks, written in YAML, automate tasks on target hosts, making it a versatile tool for various automation needs, from package installation to complex configurations, ensuring consistent and reliable infrastructure management.

Top comments (0)