将Ansible变量从同一角色中的一个角色(在一个主机上运行)传递给在另一个主机上运行的另一个角色 [英] Pass Ansible variables from one role (running on one host) to another role running on another host within the same playbook

查看:222
本文介绍了将Ansible变量从同一角色中的一个角色(在一个主机上运行)传递给在另一个主机上运行的另一个角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本在不同主机上扮演不同角色的剧本. 是否可以将变量从在一台主机上运行的一个角色传递到在同一剧本运行中运行的另一台主机上的另一个角色?还是任何解决方法?

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 from the above host/role

角色1中设置变量db的任务如下所示:

The task in role1 that sets teh variable db looks like this:

- shell: cd /ACE/conf && grep ^db.url local1.properties | awk -F/ '{print $4}' | awk -F? '{print $1}'
  register: db

更新:在第一个主机上,这些值是动态的,就像始终更新的配置文件一样.将值存储在具有角色1的host1上的变量中之后,然后移至host2,运行role2并使用host1存储的变量中的值进行处理.

UPDATE: On the first host the values are dynamic, it's like a configuration file that is always updated. After I store the values in variables on host1 with the role1 I then move to the host2, run the role2 and do stuff with those values from variables stored by host1.

我尝试过hostvars:

I tried with hostvars:

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

我收到错误消息:

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

LBL在主机中存在,因为我担任第一个角色

LBL exists in hosts as on it I run the first role

我在一台主机上设置了一个变量,并且希望该变量可用于另一台主机.所有这些都在一个剧本中.能做到吗?

I set a variable on one host and I want that variable to be available to the other host. All this within a single playbook. Can it be done ?

hostvars无法像这样使用它:

hostvars is not working using it like this:

---   
- 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'] }}"

错误:

fatal: [10.104.148.138] => host not found: LBL

/etc/ansible/hosts

/etc/ansible/hosts

[root@NS1 ansible]# cat /etc/ansible/hosts
[LBL]
10.104.148.136
[LM]
10.104.148.138

推荐答案

问题出在您的库存中.

此消息:

fatal: [10.104.148.138] => host not found: LBL

是因为LBL是组而不是主机.组LBL中有一个主机:10.104.148.136

is because LBL is a group and not a host. Group LBL has one host in it: 10.104.148.136

执行以下操作之一:

1.将库存(/etc/ansible/hosts)更改为:

1. Change your inventory (/etc/ansible/hosts) to:

LBL ansible_ssh_host=10.104.148.136
LM ansible_ssh_host=10.104.148.138

2.或者,如果您真的知道自己在做什么,并且LBL是一个组,并且您想要保持这种状态,则可以使用以下命令访问变量:

2. or if you really know what you're doing and LBL is a group and you wanna keep it that way then access the variable with:

{{ hostvars['10.104.148.136']['db']['stdout'] }}

再次LBL是一个不是主机的组. 更多信息.

Again LBL is a group not a host. More info.

这篇关于将Ansible变量从同一角色中的一个角色(在一个主机上运行)传递给在另一个主机上运行的另一个角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆