Ansible 在主机上下文之间传递变量 [英] Ansible passing variables between host contexts

查看:48
本文介绍了Ansible 在主机上下文之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,当我希望能够将在一个主机组下注册的变量传递到另一个主机组时,但我不知道该怎么做,并且在该变量下找不到任何相关内容文档 http://docs.ansible.com/ansible/playbooks_variables.html

这是我想要看到的简化示例.我有一个剧本,可以调用许多不同的组并检查符号链接指向的位置.我希望能够在播放结束时将所有符号链接目标报告给控制台.

问题是注册的值只在定义它的主机组下有效.是否有正确的方法导出这些变量?

---- 主机:max_logger任务:- 外壳:ls -la/home/ubuntu/apps/max-logger/active |awk -F':' '{print $NF}'注册:max_logger_old_active- 主机:max_data任务:- 外壳:ls -la/home/ubuntu/apps/max-data/active |awk -F':' '{print $NF}'寄存器:max_data_old_active- 主机:本地主机"任务:- 调试:>msg="旧的 max_logger 构建是 {{ max_logger_old_active.stdout }}旧的 max_data 构建是 {{ max_data_old_active.stdout }}"

解决方案

你不需要在这里传递任何东西(你只需要访问).注册的变量存储为宿主事实,并且在整个 playbook 运行时将它们存储在内存中,因此您可以从所有后续播放中访问它们.

这可以使用 魔法变量hostvars.

然而,您需要引用一个主机名,它不一定与您在问题中发布的主机组名(例如 max_logger)匹配:

- 主机:本地主机"任务:- 调试:>msg="旧的 max_logger 构建是 {{ hostvars['max_logger_host'].max_logger_old_active.stdout }}旧的 max_data 构建是 {{ hostvars['max_data_host'].max_data_old_active.stdout }}"

你也可以写hostvars['max_data_host']['max_data_old_active']['stdout'].

As the title says when I'd like to be able to pass a variable that is registered under one host group to another, but I'm not sure how to do that and I couldn't find anything relevant under the variable documentation http://docs.ansible.com/ansible/playbooks_variables.html

This is a simplified example of what I am trying to see. I have a playbook that calls many different groups and checks where a symlink points. I'd like to be able to report all of the symlink targets to console at the end of the play.

The problem is the registered value is only valid under the host group that it was defined in. Is there a proper way of exporting these variables?

---
- hosts: max_logger
  tasks:
    - shell: ls -la /home/ubuntu/apps/max-logger/active | awk -F':' '{print $NF}'
      register: max_logger_old_active

- hosts: max_data
  tasks:
    - shell: ls -la /home/ubuntu/apps/max-data/active | awk -F':' '{print $NF}'
      register: max_data_old_active

- hosts: "localhost"
  tasks:
  - debug: >
      msg="The old max_logger build is {{ max_logger_old_active.stdout }}
           The old max_data build is {{ max_data_old_active.stdout }}"

解决方案

You don't need to pass anything here (you just need to access). Registered variables are stored as host facts and they are stored in memory for the time the whole playbook is run, so you can access them from all subsequent plays.

This can be achieved using magic variable hostvars.

You need however to refer to a host name, which doesn't necessarily match the host group name (e.g. max_logger) which you posted in the question:

- hosts: "localhost"
  tasks:
  - debug: >
      msg="The old max_logger build is {{ hostvars['max_logger_host'].max_logger_old_active.stdout }}
           The old max_data build is {{ hostvars['max_data_host'].max_data_old_active.stdout }}"

You can also write hostvars['max_data_host']['max_data_old_active']['stdout'].

这篇关于Ansible 在主机上下文之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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