Trabajar con opciones del módulo Command
En este paso, explorarás algunas de las opciones disponibles con el módulo Command de Ansible para controlar su comportamiento.
Crea un nuevo archivo llamado command_options.yml
:
nano command_options.yml
Agrega el siguiente contenido al archivo:
---
- name: Explore Command module options
hosts: localhost
tasks:
- name: Run a command with a specific working directory
command: pwd
args:
chdir: /tmp
- name: Run a command with environment variables
command: echo $MY_VAR
environment:
MY_VAR: "Hello from Ansible"
- name: Run a command and ignore errors
command: ls /nonexistent_directory
ignore_errors: yes
- name: Run a command with a timeout
command: sleep 2
async: 5
poll: 0
register: sleep_result
- name: Check sleep command status
async_status:
jid: "{{ sleep_result.ansible_job_id }}"
register: job_result
until: job_result.finished
retries: 5
delay: 1
Este playbook demuestra varias opciones del módulo Command:
chdir
: Cambia el directorio de trabajo antes de ejecutar el comando.
environment
: Establece variables de entorno para el comando.
ignore_errors
: Continúa la ejecución del playbook incluso si el comando falla.
async
y poll
: Ejecuta el comando de forma asíncrona con un tiempo de espera.
Guarda el archivo y sal del editor.
Ahora, ejecuta el playbook:
ansible-playbook command_options.yml
Deberías ver una salida similar a esta:
PPLAY [Explore Command module options]
TASK [Gathering Facts]
ok: [localhost]
TASK [Run a command with a specific working directory]
changed: [localhost]
TASK [Run a command with environment variables]
changed: [localhost]
TASK [Run a command and ignore errors]
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["ls", "/nonexistent_directory"], "delta": "0:00:00.006113", "end": "2024-09-06 09:40:43.373350", "msg": "non-zero return code", "rc": 2, "start": "2024-09-06 09:40:43.367237", "stderr": "ls: cannot access '/nonexistent_directory': No such file or directory", "stderr_lines": ["ls: cannot access '/nonexistent_directory': No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [Run a command with a timeout]
changed: [localhost]
TASK [Check sleep command status]
FAILED - RETRYING: Check sleep command status (10 retries left).
FAILED - RETRYING: Check sleep command status (9 retries left).
FAILED - RETRYING: Check sleep command status (8 retries left).
FAILED - RETRYING: Check sleep command status (7 retries left).
FAILED - RETRYING: Check sleep command status (6 retries left).
FAILED - RETRYING: Check sleep command status (5 retries left).
FAILED - RETRYING: Check sleep command status (4 retries left).
FAILED - RETRYING: Check sleep command status (3 retries left).
FAILED - RETRYING: Check sleep command status (2 retries left).
FAILED - RETRYING: Check sleep command status (1 retries left).
fatal: [localhost]: FAILED! => {"ansible_job_id": "5877920468.2517", "attempts": 10, "changed": false, "finished": 0, "started": 1}
PLAY RECAP
Esta salida demuestra los diferentes comportamientos de las opciones del módulo Command que hemos explorado.