Given the foo defaults
shell> cat roles/foo/defaults/main.yml
foo_path: /tmp/test.txt
There are more options:
- Include the foo defaults into a dictionary and get the variable foo_path
shell> cat pb_bar.yml
- hosts: localhost
pre_tasks:
- include_vars:
file: roles/foo/defaults/main.yml
name: foo_defaults
- debug:
var: foo_defaults.foo_path
when: debug|d(false)|bool
- set_fact:
foo_path: "{{ foo_defaults.foo_path }}"
roles:
- bar
Use the variable in the role bar
shell> cat roles/bar/tasks/main.yml
- stat:
path: "{{ foo_path }}"
register: foo_path_st
- debug:
var: foo_path_st
when: debug|d(false)|bool
- assert:
that: foo_path_st.stat.exists
gives
shell> ansible-playbook pb_bar.yml -e debug=true
PLAY [localhost] *****************************************************************************
TASK [include_vars] **************************************************************************
ok: [localhost]
TASK [debug] *********************************************************************************
ok: [localhost] =>
foo_defaults.foo_path: /tmp/test.txt
TASK [set_fact] ******************************************************************************
ok: [localhost]
TASK [Assert foo_path] ***********************************************************************
skipping: [localhost]
TASK [bar : stat] ****************************************************************************
ok: [localhost]
TASK [bar : debug] ***************************************************************************
ok: [localhost] =>
foo_path_st:
ansible_facts:
discovered_interpreter_python: /usr/bin/python3
changed: false
failed: false
stat:
...
exists: true
...
path: /tmp/test.txt
TASK [bar : assert] **************************************************************************
ok: [localhost] => changed=false
msg: All assertions passed
PLAY RECAP ***********************************************************************************
localhost: ok=6 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
- Structure the roles' tasks
shell> tree roles
roles
├── bar
│ └── tasks
│ └── main.yml
└── foo
├── defaults
│ └── main.yml
└── tasks
├── assert_foo_path.yml
├── install_foo_path.yml
└── main.yml
5 directories, 5 files
shell> cat roles/foo/tasks/assert_foo_path.yml
- stat:
path: "{{ foo_path }}"
register: foo_path_st
- debug:
var: foo_path_st
when: debug|d(false)|bool
- assert:
that: foo_path_st.stat.exists
shell> cat roles/foo/tasks/install_foo_path.yml
- file:
path: "{{ foo_path }}"
state: touch
shell> cat roles/foo/tasks/main.yml
- name: Install foo_path
include_tasks: install_foo_path.yml
- name: Assert foo_path
include_tasks: assert_foo_path.yml
and use the module include_role
shell> cat roles/bar/tasks/main.yml
- name: Assert foo_path
include_role:
name: foo
tasks_from: assert_foo_path.yml
Create playbooks for testing
shell> cat pb_foo.yml
- hosts: localhost
roles:
- foo
shell> cat pb_bar.yml
- hosts: localhost
roles:
- bar
The play will fail if the file is missing
shell> ansible-playbook pb_bar.yml -e debug=false
PLAY [localhost] *****************************************************************************
TASK [Assert foo_path] ***********************************************************************
TASK [foo : stat] ****************************************************************************
ok: [localhost]
TASK [foo : debug] ***************************************************************************
skipping: [localhost]
TASK [foo : assert] **************************************************************************
fatal: [localhost]: FAILED! => changed=false
assertion: foo_path_st.stat.exists
evaluated_to: false
msg: Assertion failed
PLAY RECAP ***********************************************************************************
localhost: ok=1 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0
Create the file
shell> ansible-playbook pb_foo.yml -e debug=false
PLAY [localhost] *****************************************************************************
TASK [foo : Install foo_path] ****************************************************************
included: /export/scratch/tmp7/test-321/roles/foo/tasks/install_foo_path.yml for localhost
TASK [foo : file] ****************************************************************************
changed: [localhost]
TASK [foo : Assert foo_path] *****************************************************************
included: /export/scratch/tmp7/test-321/roles/foo/tasks/assert_foo_path.yml for localhost
TASK [foo : stat] ****************************************************************************
ok: [localhost]
TASK [foo : debug] ***************************************************************************
skipping: [localhost]
TASK [foo : assert] **************************************************************************
ok: [localhost] => changed=false
msg: All assertions passed
PLAY RECAP ***********************************************************************************
localhost: ok=5 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
The assert task will succeed now
TASK [foo : assert] **************************************************************************
ok: [localhost] => changed=false
msg: All assertions passed