Working with Command Module Options
In this step, you will explore some of the options available with the Ansible Command module to control its behavior.
Create a new file called command_options.yml
:
nano command_options.yml
Add the following content to the file:
---
- 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 10
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: 10
delay: 1
This playbook demonstrates various options of the Command module:
chdir
: Changes the working directory before executing the command.
environment
: Sets environment variables for the command.
ignore_errors
: Continues playbook execution even if the command fails.
async
and poll
: Runs the command asynchronously with a timeout.
Save the file and exit the editor.
Now, run the playbook:
ansible-playbook command_options.yml
You should see output similar to this:
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
This output demonstrates the different behaviors of the Command module options we explored.