ansible import_playbook with variable in name

  Kiến thức lập trình

I want to use a variable in the path of the playbook

ansible.builtin.import_playbook: "{{ home_directory_of_user }}/.ansible/.......

I do have:

- hosts: localhost
  gather_facts: false
  vars_files:
    - "{{ var_file }}"
  tasks:
    - name: var_file
      set_fact:
        ansible_collections_directory: "{{ ansible_collections_directory }}"

- name: Import the k3s playbook with flannel
  ansible.builtin.import_playbook: "{{ hostvars['localhost']['ansible_collections_directory'] }}/techno_tim/k3s_ansible/site.yml"
    vars:
      ansible_collections_directory: "{{ hostvars['localhost']['ansible_collections_directory'] }}"

With this I do get this error:

ERROR! 'hostvars' is undefined.

When I debug the value, I do get the correct path, so it has to be about how to set the variable in the name of the import_playbook

Does anybody know how to use a variable in the playbook name?

Thanks

LEAVE A COMMENT