2017-06-20 55 views
1

我有一个包含两个播放的ansible playbook,第一个播放的输出将被第二个播放器使用。所以我使用寄存器变量来存储输出。问题是我的游戏打算在多主机而不是单个主机上工作,并且每次都会覆盖寄存器的输出,而我没有最后(n-1)次执行的值。使用寄存器变量来存储多主机播放的值

我的剧本是这样的:

- name: Perform task 
    hosts: db_server 
    connection: local 
    gather_facts: no 
    vars: 
    - method: "{{ method }}" 

    roles: 
    - {role: run_custom_module, tags: ["maintenance"]} 


- name: Second task based on output of first 
    hosts: db_server 
    connection: local 
    gather_facts: no 
    vars: 
    - method: "{{ method }}" 

    roles: 
    - {role: run_custom_module, when: result=="some_string"} 

作用run_custom_module看起来是这样的:

- name: Executing the custom module 
    run_custom_module: 
     task: "Run custom py module" 
     var: "{{ method }}" 
    register: result 

清单文件看起来像:

[db_server] 
1.1.1.1 
2.2.2.2 
3.3.3.3 

在with_items的情况下寄存器变量将输出存储在名为的列表中结果,请参考here。在类似的理由,我怎么能将多主机播放的输出存储在寄存器变量中,以便它可以在同一个剧本的第二个播放中使用?

+0

那么是什么问题? – techraf

+0

如何访问播放器中每个主机生成的输出?它被覆盖。 :( –

+1

就像你在你发布的代码中做的那样:一个注册变量就像是一个事实,Ansible单独存储每个主机的值 – techraf

回答

0

注册变量是事实。您可以通过hostvars访问其他主机上的事实。

例如,您可以访问result注册变量,用于主机1.1.1.1hostvars['1.1.1.1'].result

您可以用groups列出组中的所有主机。

例如,您可以列出db_server组中的所有主机,其中groups['db_server']

为了总结的东西了,你可以尝试这样的事:

--- 
- hosts: db_server 
    tasks: 
    - ping: 
     register: result 

    - debug: var=hostvars[item].result 
     with_items: "{{ groups['db_server'] }}"