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.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/89a683e5-4e55-4d5d-b776-f34dafbe7ee4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to