DEV Community

Lucas M. Ríos
Lucas M. Ríos

Posted on

Provisioning Infrastructure with Ansible

🦾 Introduction to provision with Ansible


🔗Related content

You can find repo related in:

🐱‍🏍GitHub

You can connect with me in:

🧬LinkedIn


Resume 🧾

🚀 Ansible is a powerful configuration management tool that can be used for provisioning infrastructure.

🛠️ Provisioning refers to the process of configuring and setting up remote systems or infrastructure.

📝 Ansible uses Playbooks, which are written in YAML format, to define the desired state of the infrastructure.

📦 Ansible provides many built-in modules for performing provisioning tasks, such as installing packages, configuring network settings, managing users and groups, and more.

💻 To provision infrastructure with Ansible, you'll need to:

Install Ansible
Define your infrastructure using Playbooks
Create an inventory file
Execute the playbook using the ansible-playbook command
🖥️ Ansible can also be used for provisioning Windows systems, but this requires some additional setup and configuration, such as configuring the WinRM service on the target Windows system and specifying the WinRM connection details in the inventory file.

Overall, provisioning with Ansible provides a way to automate the process of configuring and setting up infrastructure, reducing the risk of human error and increasing efficiency. 🤖


What is provisining in Ansible?

In Ansible, provisioning refers to the process of configuring and setting up remote systems or infrastructure. Ansible is a configuration management tool that uses a declarative language to describe the desired state of the infrastructure. This means that instead of specifying individual tasks to be performed, you describe the desired end state of the system, and Ansible figures out how to get there.

Provisioning in Ansible involves defining the configuration of the remote systems or infrastructure in a playbook, which is a YAML file that lists the tasks to be performed. These tasks can include installing packages, configuring network settings, setting up users and groups, and more. Ansible then connects to the remote systems over SSH and executes the tasks in the playbook.

Ansible provides a number of built-in modules that can be used to perform provisioning tasks, such as the apt module for managing packages on Ubuntu systems, the yum module for managing packages on Red Hat-based systems, and the user module for managing users and groups.

Overall, provisioning with Ansible provides a way to automate the process of configuring and setting up infrastructure, reducing the risk of human error and increasing efficiency.


How I can provision infrastructure with Ansible?

To provision infrastructure with Ansible, you'll need to follow these basic steps:

  1. Install Ansible on your local machine or on a server that will be used to run Ansible commands.

  2. Define the desired state of your infrastructure using Ansible Playbooks, which are written in YAML format. Playbooks contain a list of tasks that will be executed on the remote systems.

  3. Create an inventory file that lists the hostnames or IP addresses of the systems you want to provision, as well as any necessary connection details such as SSH keys or usernames/passwords.

  4. Execute the playbook using the ansible-playbook command, specifying the inventory file and playbook file.

Here's an example of what a simple Ansible playbook might look like:

---
- name: Provision a web server
  hosts: webservers
  become: true

  tasks:
  - name: Install Apache web server
    apt:
      name: apache2
      state: present
Enter fullscreen mode Exit fullscreen mode

This playbook will provision a web server by installing the Apache web server package using the apt module. The hosts parameter specifies the target systems, and become: true tells Ansible to use sudo to execute the tasks as a privileged user.

To execute this playbook, you would run the following command:

ansible-playbook -i inv.yaml playbook.yml
Enter fullscreen mode Exit fullscreen mode

where inv.yaml is the name of your inventory file, and playbook.yml is the name of your playbook file.

Ansible provides many built-in modules that can be used to perform various provisioning tasks, such as installing packages, configuring network settings, managing users and groups, and more. You can also create your own custom modules if needed.

Look we talk to provision in systems with base Unix.


And in Windows?

Ansible can also be used for provisioning Windows systems. However, provisioning Windows systems with Ansible requires some additional setup and configuration compared to provisioning Linux systems. Here are the basic steps to provision a Windows system with Ansible:

  1. Install Ansible on a Linux system or a Windows system with the Windows Subsystem for Linux (WSL) enabled. Ansible cannot be installed directly on a Windows system.

  2. Configure the WinRM service on the target Windows system to allow remote management. WinRM is Microsoft's implementation of the WS-Management protocol, which allows remote management of Windows systems.

  3. Create an inventory file that lists the target Windows system and specifies the connection details, including the WinRM username and password.

  4. Write a playbook that contains tasks to configure the Windows system as desired. Ansible provides several built-in modules for configuring Windows systems, including modules for managing users, groups, and Windows features.

  5. Execute the playbook using the ansible-playbook command, specifying the inventory file and playbook file.

Here's an example of what a simple Ansible playbook for Windows might look like:

---
- name: Provision a Windows server
  hosts: windows
  gather_facts: no
  become: yes

  tasks:
  - name: Install IIS web server
    win_feature:
      name: Web-Server
      state: present
Enter fullscreen mode Exit fullscreen mode

This playbook installs the IIS web server feature on a Windows system using the win_feature module. The hosts parameter specifies the target Windows system, and become: yes tells Ansible to execute the tasks as an administrator.

To execute this playbook, you would run the following command:

ansible-playbook -i inv.yaml playbook.yml --extra-vars "ansible_user=<winrm_username> ansible_password=<winrm_password> ansible_connection=winrm ansible_winrm_transport=basic ansible_winrm_server_cert_validation=ignore"
Enter fullscreen mode Exit fullscreen mode

where inv.yaml is the name of your inventory file, playbook.yml is the name of your playbook file, and winrm_username and winrm_password are the username and password for the WinRM connection. The --extra-vars parameter is used to pass the WinRM connection details to Ansible. The ansible_winrm_server_cert_validation=ignore option is used to disable certificate validation, which is often necessary in testing or development environments.

Overall, provisioning Windows systems with Ansible requires some additional setup and configuration compared to provisioning Linux systems, but Ansible provides many built-in modules that can be used to configure and manage Windows systems.


Say thanks, give like and share if this has been of help/interest 😁🖖


Top comments (1)

Collapse
 
apetryla profile image
Aidas Petryla

Thank You for the article! Ansible is super awesome. Have been working with it for over a year and lately I'm using it for continuous deployment of my website. Greatly recommend for taking a look at this tool!