Skip to content

Instantly share code, notes, and snippets.

@b0bu
Created May 25, 2021 14:42
Show Gist options
  • Save b0bu/dd036335870b868b30be006416c28a42 to your computer and use it in GitHub Desktop.
Save b0bu/dd036335870b868b30be006416c28a42 to your computer and use it in GitHub Desktop.
ansible - pass interpolated values to import_playbook

This is possible:

---
- import_playbook: collection.base.onprem
  vars:
    icinga_servers: ["server1", "server2"]
    auter_cron_prep_spec: "0 5 * * Mon root"
    auter_cron_apply_spec: "0 6 * * Mon root"

However this is not

---
- import_playbook: collection.base.onprem
  vars:
    icinga_servers: "{{ group_var or host_var }}"
    auter_cron_prep_spec: "{{ group_var or host_var }}"
    auter_cron_apply_spec: "{{ group_var or host_var }}"

Ansible can't pass vars at host|group_var scope to playbook scope. This is an issue if you don't want all your servers patching and rebooting at the same time on the same day. You can however set facts which can then be passed to the playbook.

---
- hosts: all
  remote_user: root
  name: Set vars so that they can be passed to import_playbook
  tasks:
    - set_fact:
        prep_spec: "{{ auter_cron_prep_spec }}"
        apply_spec: "{{ auter_cron_apply_spec }}"
        icinga_servers: "{{ icinga_servers }}"

# import playbook can't pass interpolated rvalues "{{ var }}" between playbooks
# setting as facts allowed values to be passed in to expected lvalues
- import_playbook: collection.base.onprem
  vars:
    icinga_servers: "{{ icinga_servers }}"
    auter_cron_prep_spec: "{{ prep_spec }}"
    auter_cron_apply_spec: "{{ apply_spec }}"

Any roles needing access to these variable names will get the values of the set_fact above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment