Artyum
Artyum

Reputation: 179

Ansible playbook_dir | basename

I want to remember current playbook folder name and pass it to another imported playbook. I set fact as "playbook_dir | basename" that returns proper folder name: appfolder. I pass it as bpm_folder to the imported playbook it is visible as appfolder (debug returns: "msg": "appfolder") but it doesn't work.

Inside imported playbook I got: skipping vars_file '../{{ bpm_folder }}/deployment_settings.yml' due to an undefined variable.

But when I add vars to imported playbook ant set "bpm_folder: 'appfolder'" it does work, even if this is exactly the same folder name.

The point is that I want to get the appfolder automatically. How to fix it?

---
- name: pre deploy
  hosts: myhosts
  gather_facts: no

  tasks:
    - set_fact:
        myfolder: "{{ playbook_dir | basename }}"

- name: start deploy
  import_playbook: '../Common/deploy.yml'
  vars:
    bpm_folder: 'appfolder' <- it is working
    bpm_folder: "{{ myfolder }}" <-- doent't work
ansible --version
  ansible [core 2.11.6]
  python version = 3.6.8 (default, Aug 13 2020, 07:46:32) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
  jinja version = 3.0.2
  libyaml = True

Best regards!

Upvotes: 3

Views: 6827

Answers (3)

Vladimir Botka
Vladimir Botka

Reputation: 68084

There is no reason it shouldn't work if the hosts are the same in both plays, e.g.

shell> cat deploy.yml
- hosts: localhost
  tasks:
    - debug:
        var: bpm_folder
shell> cat playbook.yml
- hosts: localhost
  tasks:
    - set_fact:
        my_folder: "{{ playbook_dir | basename }}"

- import_playbook: deploy.yml
  vars:
    bpm_folder: "{{ my_folder }}"

gives

TASK [debug] **********************************************************
ok: [localhost] => 
  bpm_folder: tmp8

The problem might appear when any host in the imported play is not included in the first play, e.g.

shell> cat deploy.yml
- hosts: host1,host2
  tasks:
    - debug:
        var: bpm_folder
shell> cat playbook.yml
- hosts: host1
  tasks:
    - set_fact:
        my_folder: "{{ playbook_dir | basename }}"

- import_playbook: deploy.yml
  vars:
    bpm_folder: "{{ my_folder }}"

gives

ok: [host1] => 
  bpm_folder: tmp8
ok: [host2] => 
  bpm_folder: VARIABLE IS NOT DEFINED!

You can fix it by including all hosts in the first play too. It is enough to run the set_fact once, e.g.

shell> cat playbook.yml
- hosts: host1,host2
  tasks:
    - set_fact:
        my_folder: "{{ playbook_dir | basename }}"
      run_once: true

- import_playbook: deploy.yml
  vars:
    bpm_folder: "{{ my_folder }}"

gives

ok: [host1] => 
  bpm_folder: tmp8
ok: [host2] => 
  bpm_folder: tmp8

Upvotes: 1

seshadri_c
seshadri_c

Reputation: 7340

Somehow this variable is not getting evaluated under the vars_file: section. Since you are able to get the value of bpm_folder when doing debug (tasks section) in deploy.yml, I'd suggest using include_vars module to load the variables.

For example in deploy.yml:

- hosts: myhosts

  tasks:
    - debug:
         msg: "{{ bpm_folder }}"
    - include_vars:
        file: "../{{ bpm_folder }}/deployment_settings.yml"

Upvotes: 1

U880D
U880D

Reputation: 12083

According your description it seems to be related to the difference between include_... and import_....

Since the import is static and pre-processed at the time playbooks are parsed, the variable isn't initialized at that stage.

Might import_task: ../Common/deploy.yml work for you?

Upvotes: 1

Related Questions