Variable defined in main play is not passed to included playbook

52 views
Skip to first unread message

0lai.m...@gmail.com

unread,
Apr 10, 2017, 10:44:13 AM4/10/17
to Ansible Project
I posted an issue recently on github (https://github.com/ansible/ansible/issues/23394) and was advised to post here on the user list since the issue I am encountering is not a bug. I am re-posting the content here with the response I received.

****************
I have a variable defined from user input in the main playbook, which I would like to pass to an included playbook. The docs say I should be able to do this (http://docs.ansible.com/ansible/playbooks_roles.html) unless I misunderstood. I tried different variations in trying to pass the variable to the included playbook.

STEPS TO REPRODUCE:
Here's my first try:
myMainPlay.yml


  hosts: myHost
  vars_prompt:
    name: ticket_number
    prompt: "Ticket #"
    private: no
  pre_tasks:
    name: Create staging directory
    file:
      path: "{{ deploy_dir }}/RT{{ ticket_number }}/staging"
      state: directory
   include: testInclude.yml


testInclude.yml
   tasks:

     name: checking via debug
     debug:
       msg: "the value of ticket_number is {{ ticket_number }}"


The resulting error --> "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ticket_number' is undefined\n\nThe error appears to have been in '/mydirectory/testInclude.yml': line 8, 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: checking via debug\n ^ here\n"


Note that the main play was able to create the directory with the ticket_number passed in.


Next, I tried the following changes but with the same error --
[myMainPlay.yml] - include: testInclude.yml number="{{ ticket_number }}"
[testInclude.yml] msg: "the value of number is {{ number }}"


Based on this link --> (http://stackoverflow.com/questions/26784185/ansible-passing-evaluated-variables-to-includes) --> the first one should have worked without having to pass the variable explicitly in the include statement. Anyway, I tried explicitly passing variables in the include statement as shown above but it didn't work. It only worked when I passed something like number=1234567, and testInclude.yml was able to pick it up.


Response:

We believe the ticket you have filed is being somewhat misunderstood, as one thing works a little differently than stated.

Play vars are not shared across plays, even when one of them is included, only hostvars are available across plays.


************

So it looks like I misunderstood the Ansible document ("Using either syntax, variables passed in can then be used in the included files.") For now, I added "--extra-vars ticket_number=1234567" to the command line. However, I'd like to keep the command line invocation of ansible-playbook as short as possible.


Does anyone have any ideas/suggestions how I can pass in a user inputed value into an included playbook?


Thanks.

J Hawkesworth

unread,
Apr 11, 2017, 3:13:03 AM4/11/17
to Ansible Project
You could experiment with passing vars to a role.

roles:
 - role: do_things_with_ticket
      ticket_number_to_process: "{{ticket_number}}"

If you have lots of vars you need to set and don't want to set -e command line for each of them, write them to a file and use the -e @/path/to/some/file.yml to load the extra vars in.
This is pretty much what I do for our nightly integration build.  I run one playbook to generate a yaml file containing all the versions of all the software components and then pass the output .yml file to a second playbook that deploys any updates.

You might also be able to use include_vars if you have the vars in a file.

Hope this helps.

Jon
Reply all
Reply to author
Forward
0 new messages