import_playbook - Passing variables - error

30 views
Skip to first unread message

Geoff

unread,
May 23, 2019, 9:53:08 AM5/23/19
to Ansible Development
I have a playbook called:  testing_vars_pass.yml

It imports another playbook called:  testing_vars_call.yml

The playbook testing_vars_call.yml  expects a variable called:  myVar


Below is the contents of each playbook ...

$ cat testing_vars_pass.yml
---
- name: Test pass vars
  hosts: all
  vars:
     myVar: hello

  tasks:

  - name: Debug pass vars
    debug:
      msg: "{{ myVar }}"

- import_playbook: testing_vars_call.yml myVar="{{ myVar }}"


        ... and ...


$ cat testing_vars_call.yml
---
- name: Test call vars
  hosts: all
  vars:
    myVar: = "{{ myVar }}"

  tasks:

  - name: Debug call vars
    debug:
      msg: "{{ myVar }}"



This produces the following error ...

18 TASK [Debug call vars] *********************************************************

19 fatal: [1.1.1.1]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ myVar }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while templating '{{ myVar }}'.  [...]



The same error is seen if I use the syntax ...

- import_playbook: testing_vars_call.yml
  vars:
    myVar:  "{{ myOwnVar }}"



In case the fact that the 'myVars' variable had the same name in both playbooks, I changed the name in the first playbook ...

$ cat testing_vars_pass.yml
---
- name: Test pass vars
  hosts: all
  vars:
     myOwnVar: hello

  tasks:

  - name: Debug pass vars
    debug:
      msg: "{{ myOwnVar }}"

- import_playbook: testing_vars_call.yml myVar="{{ myOwnVar }}"


This time, I get a different error ...

18 TASK [Debug call vars] *********************************************************

19 fatal: [1.1.1.1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'myOwnVar' is undefined\n\nThe error appears to have been in '/var/lib/awx/projects/_6__unix/testing_vars_call.yml': line 9, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: Debug call vars\n    ^ here\n"}


What is the correct way to pass variables into playbooks via 'import_playbook'?

FG

unread,
Jan 24, 2020, 1:58:41 PM1/24/20
to Ansible Development

I am facing  same issue , Any leads ??? 



Regards

James Cassell

unread,
Jan 24, 2020, 9:58:39 PM1/24/20
to Ansible Development List

On Fri, Jan 24, 2020, at 1:58 PM, FG wrote:
>
> I am facing same issue , Any leads ???
>
>

Could you be more specific? What exactly is the issue you're seeing? Playbooks? Commands you're using? Output?

In any case, this is definitely a better question for ansible-project@ list

The post below had lots of errors, including self referring variables, and lack if understanding variables scope https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#scoping-variables


V/r,
James Cassell


>
> Regards
>
>
> On Thursday, May 23, 2019 at 9:53:08 AM UTC-4, Geoff wrote:
> > I have a playbook called: *testing_vars_pass.yml*
> >
> > It imports another playbook called: *testing_vars_call.yml*
> >
> > The playbook *testing_vars_call.yml* expects a variable called: *myVar*
> >
> >
> > Below is the contents of each playbook ...
> >
> > $ cat *testing_vars_pass.yml*
> > ---
> > - name: Test pass vars
> > hosts: all
> > vars:
> > myVar: hello
> >
> > tasks:
> >
> > - name: Debug pass vars
> > debug:
> > msg: "{{ myVar }}"
> >
> > - import_playbook: *testing_vars_call.yml* myVar="{{ myVar }}"
> >
> >
> > ... and ...
> >
> >
> > $ cat *testing_vars_call.yml*
> > ---
> > - name: Test call vars
> > hosts: all
> > vars:
> > myVar: = "{{ myVar }}"
> >
> > tasks:
> >
> > - name: Debug call vars
> > debug:
> > msg: "{{ myVar }}"
> >
> >
> >
> > This produces the following error ...
> >
> > 18 TASK [Debug call vars] *********************************************************
> >
> > 19 fatal: [1.1.1.1]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ myVar }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while templating '{{ myVar }}'. [...]
> >
> >
> >
> > The same error is seen if I use the syntax ...
> >
> > - import_playbook: testing_vars_call.yml
> > vars:
> > myVar: "{{ myOwnVar }}"
> >
> >
> >
> > In case the fact that the 'myVars' variable had the same name in both playbooks, I changed the name in the first playbook ...
> >
> > $ cat *testing_vars_pass.yml*
> > ---
> > - name: Test pass vars
> > hosts: all
> > vars:
> > my*Own*Var: hello
> >
> > tasks:
> >
> > - name: Debug pass vars
> > debug:
> > msg: "{{ my*Own*Var }}"
> >
> > - import_playbook: testing_vars_call.yml myVar="{{ my*Own*Var }}"
Reply all
Reply to author
Forward
0 new messages