Module and return
Tip
We recommend to use the Sandbox to play around with this example.
Tip
All the files from this section are in module-and-return.zip.
At the end of the execution of a module, a collection of values is return covering the execution. The return can be captured in a variable and used later, for example to conditionally execute the next step.
Playbook
playbook.yml
- name: Collecting information
hosts:
- web
tasks:
- name: Get stats of /usr/bin/git
ansible.builtin.stat:
path: /usr/bin/git
register: git_stat
- name: Print message
ansible.builtin.debug:
msg: '{{ git_stat }}'
- name: Run git config
ansible.builtin.command:
argv:
- git
- config
- set
- --global
- user.name
- ansible
when: git_stat.stat.exists
The above playbook has a single play with three tasks:
get information regarding the file
/usr/bin/git.print the information collected in the previous task.
execute
git configif/usr/bin/gitexists.
Running
cd module-and-return
ansible-playbook \
--ask-vault-pass \
-i inventories/production.yml \
--extra-vars @vault/production \
playbook.yaml
Note
The password for the vault used in the example is 123. You must use a strong passwords, for example, a minimum of 8 randomly generated characters.
returns
PLAY [Collecting information] **************************************************
TASK [Gathering Facts] *********************************************************
ok: [managed_node_01]
TASK [Get stats of /usr/bin/git] ***********************************************
ok: [managed_node_01]
TASK [Print message] ***********************************************************
ok: [managed_node_01] => {
"msg": {
"changed": false,
"failed": false,
"stat": {
"exists": false
}
}
}
TASK [Run git config] **********************************************************
skipping: [managed_node_01]
PLAY RECAP *********************************************************************
managed_node_01 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Note that the task Run git config was skipped.