[ansible-project] Re: Global variable to pas dynamic data between plays with different hosts
OK, so I looked at Ansible's code, and I now know how to implement this. I tried my modification locally and it works fine. It basically adds a module that adds an 'extra_vars' variable if it does not yet exist. That way, any variable set on the commandline using the '--extra-vars' argument cannot be overruled by the playbook. Now this scenario works : Inventory : group_all = { host_1, host_2, host_3 } group_one = { host_1 } group_two = { host_2, host3 } Playbook 'my_playbook.yml' : --- - hosts: group_one tasks: - name: list /tmp shell: ls /tmp register: out - name: set global variable setglobal: name=myvar value="{{out}}" - hosts: group_two tasks: - debug: msg="{{myvar}}" command : 'ansible-playbook my_playbook.yml --limit group_all' Anyone interested in this modification ? I can create a pull request... -- 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/e8f483b0-e215-4675-93ba-8243d19f5504%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[ansible-project] Re: Global variable to pas dynamic data between plays with different hosts
OK, redefining my testcase then : > > groupall = { host_a, host_b, host_c } > group1 = { host_x, host_y, host_a, host_z } group2 = { host_b, host_c } > > --- > - hosts: group1 > tasks: > - name: get the variable value > whatever_module: arg=arg_a > register: result > > - hosts: group2 > tasks: > - debug: var="{{result}}" > > The playbook is started with the '--limit groupall' argument. > > Yes, every host in 'group1' will have access to the result. What I want is to access that result in the other play ( for 'group2' ). ( BTW, I understand that the first play will only be executed on 'host_a' since that's the only host that is in 'groupall'...) For now I'll call it a playbook 'global' variable... Like an 'extra_var' , but then settable from within a play. 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/d0494030-664f-42c3-9a97-7f637c316bd4%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [ansible-project] Re: Global variable to pas dynamic data between plays with different hosts
On 30.11.2016 11:50, Pieter Voet wrote: Hi Kai, you said : Knowledge of members in group1 you find with groups['group1'] hostvars[groups['group1'][0]]['result'] actually, the 'group1' group contains a lot of hosts.. There is no quarantee that the first entry in that array actually is the host that applies to my playbook. Then I don't understand what you are trying to achieve, your testcase must be flawed. Every host in group1 will run that task, so every host in the group will have a "result" variable set. You must choose at some level who is going to be the "global" one. -- Kai Stian Olstad -- 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/47c4495663ab9207692a78f531b8c34d%40olstad.com. For more options, visit https://groups.google.com/d/optout.
[ansible-project] Re: Global variable to pas dynamic data between plays with different hosts
Hi Kai, you said : > Knowledge of members in group1 you find with groups['group1'] > hostvars[groups['group1'][0]]['result'] actually, the 'group1' group contains a lot of hosts.. There is no quarantee that the first entry in that array actually is the host that applies to my playbook. -- 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/a2a00485-9776-4b66-ba47-7a6fb3cda20b%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [ansible-project] Re: Global variable to pas dynamic data between plays with different hosts
On 30.11.2016 10:08, Pieter Voet wrote: True. And thanks for contributing. I knew this. I think I oversimplified my testcase... Thing is, our inventory has many groups, where hosts can be member of multiple groups.. Here's a modified testcase : groupall = { host_a, host_b, host_c } group1 = { host_a } group2 = { host_b, host_c } --- - hosts: group1 tasks: - name: get the variable value whatever_module: arg=arg_a register: result - hosts: group2 tasks: - debug: var="{{result}}" The playbook is started with the '--limit groupall' argument. Now, because of the groups, the second play cannot lookup 'host_a' in the hostvars, since it has no knowledge about the hosts in 'group1'... Any ideas ? Knowledge of members in group1 you find with groups['group1'] So this should work. hostvars[groups['group1'][0]]['result'] -- Kai Stian Olstad -- 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/6a91aa927e914a1246cd5b2c8937f757%40olstad.com. For more options, visit https://groups.google.com/d/optout.
[ansible-project] Re: Global variable to pas dynamic data between plays with different hosts
True. And thanks for contributing. I knew this. I think I oversimplified my testcase... Thing is, our inventory has many groups, where hosts can be member of multiple groups.. Here's a modified testcase : > > groupall = { host_a, host_b, host_c } > group1 = { host_a } group2 = { host_b, host_c } > > --- > - hosts: group1 > tasks: > - name: get the variable value > whatever_module: arg=arg_a > register: result > > - hosts: group2 > tasks: > - debug: var="{{result}}" > > The playbook is started with the '--limit groupall' argument. > Now, because of the groups, the second play cannot lookup 'host_a' in the hostvars, since it has no knowledge about the hosts in 'group1'... Any ideas ? -- 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/19233820-3376-43ae-8f2a-300165de1f8d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.