Share variables between roles running on different hosts

5.591 weergaven
Naar het eerste ongelezen bericht

Adrian Paraschiv

ongelezen,
31 mrt 2015, 08:17:3631-03-2015
aan ansible...@googlegroups.com
Hello all,

I have a playbook that runs different roles on different hosts.
Is it possible to pass a variable from one role running on one host to another role on another host running within the same playbook run? Or any workaround ?

playbook
   host1
      role1
        here I get some variables: var1 var2 ...etc
   host2
      role2
         here I need to use var1 var2 ... etc.

Giovanni Tirloni

ongelezen,
31 mrt 2015, 08:58:5331-03-2015
aan ansible...@googlegroups.com

Adrian Paraschiv

ongelezen,
31 mrt 2015, 10:13:4931-03-2015
aan ansible...@googlegroups.com
Hi,

I tried with hostvars:

{{ hostvars.LBL.db.stdout }};"
{{ hostvars['LBL']['db'] }}
{{ hostvars['LBL']['db']['stdout'] }}

but it says:

in get_variables raise Exception("host not found: %s" % hostname) Exception: host not found: LBL
And LBL is defined, after all I'm running the first role on LBL and it's ok.
I'm using: ansible-1.8.4-1.el6.noarch

Giovanni Tirloni

ongelezen,
31 mrt 2015, 10:56:4731-03-2015
aan ansible...@googlegroups.com
Working example:

---
- name: test hostvars host1
hosts: host1
tasks:
- command: "ls /bin"
register: ls_out

- name: test hostvars host2
hosts: host2
tasks:
- debug:
var: "{{ hostvars['host1']['ls_out']['stdout'] }}"


Giovanni

Adrian Paraschiv

ongelezen,
31 mrt 2015, 11:18:5531-03-2015
aan ansible...@googlegroups.com
I modified it to suit my hosts:


---
- name: test hostvars host1
  hosts: LBL

  tasks:
    - command: "ls /bin"
      register: ls_out

- name: test hostvars host2
  hosts: LM
  tasks:
    - debug:
        var: "{{ hostvars['LBL']['ls_out']['stdout'] }}"

and it doesn't work:

fatal: [10.104.148.138] => Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/ansible/runner/__init__.py", line 590, in _executor
    exec_rc = self._executor_internal(host, new_stdin)
  File "/usr/lib/python2.6/site-packages/ansible/runner/__init__.py", line 792, in _executor_internal
    return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=complex_args)
  File "/usr/lib/python2.6/site-packages/ansible/runner/__init__.py", line 1004, in _executor_internal_inner
    complex_args = template.template(self.basedir, complex_args, inject, fail_on_undefined=self.error_on_undefined_vars)
  File "/usr/lib/python2.6/site-packages/ansible/utils/template.py", line 133, in template
    d[k] = template(basedir, v, templatevars, lookup_fatal, depth, expand_lists, convert_bare, fail_on_undefined, filter_fatal)
  File "/usr/lib/python2.6/site-packages/ansible/utils/template.py", line 119, in template
    varname = template_from_string(basedir, varname, templatevars, fail_on_undefined)
  File "/usr/lib/python2.6/site-packages/ansible/utils/template.py", line 365, in template_from_string
    res = jinja2.utils.concat(rf)
  File "<template>", line 8, in root
  File "/usr/lib64/python2.6/site-packages/jinja2/environment.py", line 331, in getitem
    return obj[argument]
  File "/usr/lib/python2.6/site-packages/ansible/runner/__init__.py", line 103, in __getitem__
    result = self.inventory.get_variables(host, vault_password=self.vault_password).copy()
  File "/usr/lib/python2.6/site-packages/ansible/inventory/__init__.py", line 442, in get_variables

    raise Exception("host not found: %s" % hostname)
Exception: host not found: LBL

FATAL: all hosts have already failed -- aborting

the ls /bin is been executed but it's the same error
this is the content of my hosts file:

[LBL]
10.104.148.136
[LM]
10.104.148.138

What version of ansible are you running ?

Giovanni Tirloni

ongelezen,
31 mrt 2015, 12:11:4031-03-2015
aan ansible...@googlegroups.com
On Tue, 31 Mar 2015 08:18 -0700, Adrian Paraschiv
<adrian.para...@gmail.com> wrote:
> What version of ansible are you running ?

Tested on 1.8.4 and 1.9.0.1

Giovanni

Adam Herzog

ongelezen,
31 mrt 2015, 14:16:4031-03-2015
aan ansible...@googlegroups.com
On Mar 31, 2015, at 8:18 AM, Adrian Paraschiv <adrian.para...@gmail.com> wrote:
>
> I modified it to suit my hosts:
>
> ---
> - name: test hostvars host1
> hosts: LBL
> tasks:
> - command: "ls /bin"
> register: ls_out
>
> - name: test hostvars host2
> hosts: LM
> tasks:
> - debug:
> var: "{{ hostvars['LBL']['ls_out']['stdout'] }}"
>
> and it doesn't work:
<snip>
> FATAL: all hosts have already failed -- aborting
>
> the ls /bin is been executed but it's the same error
> this is the content of my hosts file:
>
> [LBL]
> 10.104.148.136
> [LM]
> 10.104.148.138

LBL is the name of the group, not the host. You'd want something like

var: "{{ hostvars['10.104.148.136']['ls_out']['stdout'] }}"

You can also get the list of hosts in a group, using {{ groups['LBL'] }}, if you'd rather just get the first host from there or if you want to iterate over the entire group.

There's more info and examples about this on the link posted earlier in this thread: http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts

-A

--
Adam Herzog
ad...@adamherzog.com

Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten