Cómo descargar archivos desde una URL específica utilizando el módulo get_url de Ansible

AnsibleAnsibleBeginner
Practicar Ahora

💡 Este tutorial está traducido por IA desde la versión en inglés. Para ver la versión original, puedes hacer clic aquí

Introduction

Ansible is a powerful open-source automation tool that simplifies the management of IT infrastructure. In this tutorial, we will explore the Ansible get_url module, which allows you to download files from a specific URL. This module is essential for automation tasks such as software installation, configuration management, and content deployment.

Throughout this tutorial, you will learn how to set up Ansible, use the get_url module with basic and advanced options, and implement practical examples to automate file downloads. By the end of this guide, you will have a solid understanding of how to leverage this module to streamline your file management processes.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL ansible(("Ansible")) -.-> ansible/ModuleOperationsGroup(["Module Operations"]) ansible(("Ansible")) -.-> ansible/PlaybookEssentialsGroup(["Playbook Essentials"]) ansible/ModuleOperationsGroup -.-> ansible/apt("Package Manager") ansible/ModuleOperationsGroup -.-> ansible/command("Execute Commands") ansible/ModuleOperationsGroup -.-> ansible/copy("Transfer Files") ansible/ModuleOperationsGroup -.-> ansible/file("Manage Files/Directories") ansible/ModuleOperationsGroup -.-> ansible/get_url("Download URL") ansible/ModuleOperationsGroup -.-> ansible/ping("Network Test") ansible/PlaybookEssentialsGroup -.-> ansible/playbook("Execute Playbook") subgraph Lab Skills ansible/apt -.-> lab-415256{{"Cómo descargar archivos desde una URL específica utilizando el módulo get_url de Ansible"}} ansible/command -.-> lab-415256{{"Cómo descargar archivos desde una URL específica utilizando el módulo get_url de Ansible"}} ansible/copy -.-> lab-415256{{"Cómo descargar archivos desde una URL específica utilizando el módulo get_url de Ansible"}} ansible/file -.-> lab-415256{{"Cómo descargar archivos desde una URL específica utilizando el módulo get_url de Ansible"}} ansible/get_url -.-> lab-415256{{"Cómo descargar archivos desde una URL específica utilizando el módulo get_url de Ansible"}} ansible/ping -.-> lab-415256{{"Cómo descargar archivos desde una URL específica utilizando el módulo get_url de Ansible"}} ansible/playbook -.-> lab-415256{{"Cómo descargar archivos desde una URL específica utilizando el módulo get_url de Ansible"}} end

Installing and Setting Up Ansible

Before we can use the get_url module, we need to make sure Ansible is properly installed and configured on our system.

Installing Ansible

Let's start by installing Ansible on the LabEx environment:

sudo apt update
sudo apt install -y ansible

After running these commands, verify that Ansible is installed correctly by checking its version:

ansible --version

You should see output similar to this, showing the Ansible version and configuration details:

ansible [core 2.12.x]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/labex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.10.x (default, ...) [GCC 11.x]

Creating a Working Directory

Next, let's create a dedicated directory for our Ansible project:

mkdir -p ~/project/ansible-get-url
cd ~/project/ansible-get-url

Setting Up a Simple Inventory

Ansible needs to know which hosts to manage. For this tutorial, we'll create a simple inventory file that targets the local machine:

echo "localhost ansible_connection=local" > inventory.ini

This inventory file specifies that we want to run Ansible against the localhost using a local connection.

Creating the Ansible Configuration File

To customize Ansible's behavior, let's create a simple configuration file:

cat > ansible.cfg << EOF
[defaults]
inventory = inventory.ini
host_key_checking = False
EOF

This configuration file tells Ansible to use our inventory file and disables host key checking, which simplifies connections in a test environment.

Testing the Ansible Setup

Let's verify that our Ansible configuration works correctly by running a simple ping command:

ansible localhost -m ping

You should see a successful response like:

localhost | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

This output confirms that Ansible is correctly installed and can communicate with the localhost. Now we're ready to use the get_url module in the next step.

Creating a Basic Playbook with get_url Module

Now that we have Ansible set up, let's create a simple playbook that uses the get_url module to download a file from a URL.

Understanding Ansible Playbooks

An Ansible playbook is a YAML file that defines a set of tasks to be executed on managed hosts. Playbooks are organized as a series of plays, where each play consists of:

  • A set of hosts to target
  • A set of tasks to execute

Creating Our First Playbook

Let's create a basic playbook that uses the get_url module. Create a file named download-file.yml in your project directory:

cd ~/project/ansible-get-url

Open the file editor and create the file with the following content:

---
- name: Download a file from URL
  hosts: localhost
  become: no
  tasks:
    - name: Create downloads directory
      file:
        path: ~/project/ansible-get-url/downloads
        state: directory
        mode: "0755"

    - name: Download a sample text file
      get_url:
        url: https://raw.githubusercontent.com/ansible/ansible/devel/README.md
        dest: ~/project/ansible-get-url/downloads/ansible-readme.md
        mode: "0644"

This playbook does the following:

  1. Creates a downloads directory to store our downloaded files
  2. Uses the get_url module to download the Ansible README.md file from GitHub

Understanding the get_url Module Parameters

Let's break down the key parameters in the get_url task:

  • url: Specifies the URL from which to download the file
  • dest: Defines the destination path where the file will be saved
  • mode: Sets the file permissions (0644 means read and write for owner, read for group and others)

Running the Playbook

Now that we have created our playbook, let's run it using the ansible-playbook command:

ansible-playbook download-file.yml

You should see output similar to this:

PLAY [Download a file from URL] ***********************************************

TASK [Gathering Facts] ********************************************************
ok: [localhost]

TASK [Create downloads directory] *********************************************
changed: [localhost]

TASK [Download a sample text file] ********************************************
changed: [localhost]

PLAY RECAP ********************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Verifying the Downloaded File

Let's verify that the file was downloaded correctly:

ls -l ~/project/ansible-get-url/downloads/

You should see the downloaded file:

-rw-r--r-- 1 labex labex 3154 Mar 15 12:34 ansible-readme.md

You can also view the content of the downloaded file:

head -n 10 ~/project/ansible-get-url/downloads/ansible-readme.md

This will display the first 10 lines of the Ansible README.md file, confirming that it was downloaded successfully.

Congratulations! You have successfully created and executed your first Ansible playbook using the get_url module to download a file from a URL.

Working with Advanced get_url Options

Now that we've covered the basics, let's explore some advanced options of the get_url module that can be useful in real-world scenarios.

Creating an Advanced Playbook

Let's create a new playbook named advanced-download.yml that demonstrates various advanced options:

cd ~/project/ansible-get-url

Open the file editor and create the file with the following content:

---
- name: Advanced get_url Module Options
  hosts: localhost
  become: no
  tasks:
    - name: Create advanced downloads directory
      file:
        path: ~/project/ansible-get-url/advanced-downloads
        state: directory
        mode: "0755"

    - name: Download with checksum validation
      get_url:
        url: https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
        dest: ~/project/ansible-get-url/advanced-downloads/jq
        mode: "0755"
        checksum: sha256:af986793a515d500ab2d35f8d2aecd656e764504b789b66d7e1a0b727a124c44

    - name: Download with timeout and retries
      get_url:
        url: https://raw.githubusercontent.com/ansible/ansible-examples/master/README.md
        dest: ~/project/ansible-get-url/advanced-downloads/examples-readme.md
        timeout: 30
        retries: 3
        delay: 5
        force: yes

    - name: Download only if file doesn't exist
      get_url:
        url: https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
        dest: ~/project/ansible-get-url/advanced-downloads/ansible-example.cfg
        force: no

Understanding Advanced Parameters

Let's examine the advanced parameters used in this playbook:

  1. Checksum Validation:

    • checksum: Specifies the expected checksum of the downloaded file (format: algorithm:checksum)
    • This ensures the downloaded file's integrity and prevents downloading if the file already exists with the correct checksum
  2. Timeout and Retries:

    • timeout: Maximum time in seconds for the HTTP request to complete
    • retries: Number of times to retry if the download fails
    • delay: Delay between retries in seconds
    • force: Whether to download the file even if it already exists
  3. Conditional Download:

    • force: no: The file will only be downloaded if it doesn't exist at the destination

Running the Advanced Playbook

Let's execute our advanced playbook:

ansible-playbook advanced-download.yml

You should see output similar to this:

PLAY [Advanced get_url Module Options] ****************************************

TASK [Gathering Facts] ********************************************************
ok: [localhost]

TASK [Create advanced downloads directory] ************************************
changed: [localhost]

TASK [Download with checksum validation] **************************************
changed: [localhost]

TASK [Download with timeout and retries] **************************************
changed: [localhost]

TASK [Download only if file doesn't exist] ************************************
changed: [localhost]

PLAY RECAP ********************************************************************
localhost                  : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Verifying the Advanced Downloads

Let's check the files we downloaded with the advanced options:

ls -l ~/project/ansible-get-url/advanced-downloads/

You should see all the downloaded files:

-rwxr-xr-x 1 labex labex 3773448 Mar 15 12:45 jq
-rw-r--r-- 1 labex labex     426 Mar 15 12:45 examples-readme.md
-rw-r--r-- 1 labex labex    1690 Mar 15 12:45 ansible-example.cfg

Let's test if the downloaded jq binary works:

~/project/ansible-get-url/advanced-downloads/jq --version

You should see output indicating the jq version (e.g., jq-1.6), confirming that the executable was properly downloaded with the correct checksum.

You've now successfully used various advanced options of the get_url module to download files with different requirements and constraints.

Implementing a Practical Web Application Deployment

Let's apply what we've learned in a more practical scenario: deploying a simple web application using Ansible's get_url module.

Setting Up the Web Application Project

First, let's create a directory structure for our web application:

mkdir -p ~/project/ansible-get-url/webapp/public
cd ~/project/ansible-get-url

Creating the Web Application Deployment Playbook

Now, we'll create a playbook named deploy-webapp.yml that will download necessary files for our web application:

---
- name: Deploy Web Application Files
  hosts: localhost
  become: no
  vars:
    webapp_dir: ~/project/ansible-get-url/webapp
    assets_base_url: https://raw.githubusercontent.com/ansible/ansible-documentation/devel/docs/docsite/rst/_static
  tasks:
    - name: Ensure webapp directories exist
      file:
        path: "{{ item }}"
        state: directory
        mode: "0755"
      loop:
        - "{{ webapp_dir }}"
        - "{{ webapp_dir }}/public"
        - "{{ webapp_dir }}/public/css"
        - "{{ webapp_dir }}/public/images"

    - name: Create index.html
      copy:
        dest: "{{ webapp_dir }}/public/index.html"
        content: |
          <!DOCTYPE html>
          <html>
          <head>
            <title>Ansible Demo App</title>
            <link rel="stylesheet" href="css/style.css">
          </head>
          <body>
            <div class="container">
              <h1>Welcome to Ansible Demo</h1>
              <p>This site was deployed using Ansible's get_url module!</p>
              <img src="images/ansible-logo.png" alt="Ansible Logo">
            </div>
          </body>
          </html>

    - name: Download CSS file
      get_url:
        url: "{{ assets_base_url }}/css/ansible.css"
        dest: "{{ webapp_dir }}/public/css/style.css"
        mode: "0644"
        timeout: 30
        retries: 3

    - name: Download Ansible logo
      get_url:
        url: "{{ assets_base_url }}/images/ansible_logo.svg"
        dest: "{{ webapp_dir }}/public/images/ansible-logo.png"
        mode: "0644"
        timeout: 30
        retries: 3

    - name: Install Python HTTP server
      apt:
        name: python3-http.server
        state: present
      become: yes

    - name: Display information about running the web server
      debug:
        msg: |
          Web application has been deployed!
          To start the web server, run:
          cd {{ webapp_dir }}/public && python3 -m http.server 8080
          Then access it at: http://localhost:8080

This playbook:

  1. Creates the necessary directory structure for our web application
  2. Creates an index.html file with basic HTML content
  3. Downloads a CSS file and an Ansible logo image using the get_url module
  4. Installs a Python HTTP server
  5. Displays information about how to run the web server

Running the Web Application Deployment Playbook

Let's execute the playbook:

ansible-playbook deploy-webapp.yml

You should see output indicating that the web application has been deployed successfully.

Starting the Web Server

Now, let's start the web server to test our deployed application:

cd ~/project/ansible-get-url/webapp/public
python3 -m http.server 8080 &

The web server is now running in the background on port 8080. If you were in a regular environment with browser access, you could access the web application at http://localhost:8080.

Checking the Deployed Files

Let's verify that all the required files have been deployed correctly:

ls -la ~/project/ansible-get-url/webapp/public/

You should see the index.html file and the directories we created.

ls -la ~/project/ansible-get-url/webapp/public/css/

This should show the downloaded CSS file.

ls -la ~/project/ansible-get-url/webapp/public/images/

This should show the downloaded Ansible logo image.

Viewing the HTML Content

Let's check the content of the index.html file:

cat ~/project/ansible-get-url/webapp/public/index.html

You should see the HTML content we created in the playbook.

Stopping the Web Server

When you're done testing, you can stop the web server by finding its process ID and killing it:

pkill -f "python3 -m http.server 8080"

Congratulations! You've successfully deployed a simple web application using Ansible's get_url module to download necessary files.

Summary

In this tutorial, you have gained practical experience using Ansible's get_url module to download files from specific URLs. You learned:

  • How to install and configure Ansible for local development
  • How to create basic playbooks using the get_url module to download files
  • How to utilize advanced options such as checksum validation, timeouts, retries, and conditional downloads
  • How to implement a practical web application deployment scenario using the get_url module

These skills are fundamental for automating file management tasks in various IT operations, including software deployment, content management, and system configuration. The get_url module is a versatile tool that can be integrated into more complex Ansible workflows to streamline your infrastructure management processes.

As you continue your Ansible journey, consider exploring related modules such as uri, unarchive, and synchronize, which complement the get_url module for more comprehensive file management automation.