I want to add a non-ansible-galaxy git repository full of roles as a submodule to my infrastructure project.
My first attempt was to add it as a configuration option in my playbook.yaml
:
roles_path: "{{ roles_path ~ ':./Vendor/HTSALSWA/roles' }}"
... but it turns out this can't be configured at the playbook level. Other resources have told me to set it in the ansible.cfg
, which is in an INI format, and doesn't appear to support Jinja templating:
roles_path = "{{ roles_path ~ ':./Vendor/HTSALSWA/roles' }}"
How do I add something to the roles_path
non-destructively?
I want to add a non-ansible-galaxy git repository full of roles as a submodule to my infrastructure project.
My first attempt was to add it as a configuration option in my playbook.yaml
:
roles_path: "{{ roles_path ~ ':./Vendor/HTSALSWA/roles' }}"
... but it turns out this can't be configured at the playbook level. Other resources have told me to set it in the ansible.cfg
, which is in an INI format, and doesn't appear to support Jinja templating:
roles_path = "{{ roles_path ~ ':./Vendor/HTSALSWA/roles' }}"
How do I add something to the roles_path
non-destructively?
- It's not possible to configure DEFAULT_ROLES_PATH inside a play. – Vladimir Botka Commented Feb 8 at 3:03
1 Answer
Reset to default 1It's not possible to configure DEFAULT_ROLES_PATH inside a play. You can set it as a configuration key roles_path in a configuration file or as an environment variable ANSIBLE_ROLES_PATH. Both options must be available at the start of the play.
As a workaround, you can link the dynamic role(s) to a configured directory and include the role(s). The include_role is the only feasible option here because import_role and roles must be available at the start of the play.
For example, given the below project
shell> tree .
.
├── ansible.cfg
├── hosts
├── pb.yml
└── roles
└── foo
└── tasks
└── main.yml
shell> cat roles/foo/tasks/main.yml
- debug:
msg: role foo ...
declare the roles_path in the configuration file
shell> cat ansible.cfg
[defaults]
gathering = explicit
inventory = $PWD/hosts
roles_path = $PWD/roles:$PWD/roles_dyn
callback_result_format = yaml
Then, the dynamic role(s)
shell> tree /tmp/roles
/tmp/roles
└── bar
└── tasks
└── main.yml
shell > cat /tmp/roles/bar/tasks/main.yml
- debug:
msg: role bar ...
can be linked in the pre_tasks section
shell> cat pb.yml
- hosts: test
pre_tasks:
- name: Link roles_dyn
run_once: true
delegate_to: localhost
block:
- name: Assert roles_dyn_path is defined
assert:
that: roles_dyn_path is defined
- name: Link roles_dyn
file:
state: link
src: "{{ roles_dyn_path }}"
dest: roles_dyn
tasks:
- include_role:
name: foo
- include_role:
name: bar
Given the below inventory
shell> cat hosts
localhost ansible_connection=local
[test]
test_01
test_02
test_03
the play gives
shell> ansible-playbook pb.yml -e roles_dyn_path=/tmp/roles
PLAY [test] *********************************************************************************************************
TASK [Assert roles_dyn_path is defined] *****************************************************************************
ok: [test_01 -> localhost] =>
changed: false
msg: All assertions passed
TASK [Link roles_dyn] ***********************************************************************************************
changed: [test_01 -> localhost]
TASK [include_role : foo] *******************************************************************************************
included: foo for test_01, test_02, test_03
TASK [foo : debug] **************************************************************************************************
ok: [test_01] =>
msg: role foo ...
ok: [test_02] =>
msg: role foo ...
ok: [test_03] =>
msg: role foo ...
TASK [include_role : bar] *******************************************************************************************
included: bar for test_01, test_02, test_03
TASK [bar : debug] **************************************************************************************************
ok: [test_01] =>
msg: role bar ...
ok: [test_02] =>
msg: role bar ...
ok: [test_03] =>
msg: role bar ...
PLAY RECAP **********************************************************************************************************
test_01: ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_02: ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_03: ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0