在Ansible Playbook中访问广告资源宿主变量 [英] accessing inventory host variable in ansible playbook

查看:222
本文介绍了在Ansible Playbook中访问广告资源宿主变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ansible 2.1.我具有以下清单主机文件,并且该角色由需要访问主机文件变量的播放所调用.关于如何访问它的任何想法(当前出现错误):

I am using ansible 2.1. I have the following inventory host file and a role being called by a play that needs access to the host file variable. Any thoughts on how to access it (currently getting an error):

主机文件

[test1]
test-1 ansible_ssh_host=abc.def.ghi.jkl ansible_ssh_port=1212

[test2]
test2-1 ansible_ssh_host=abc.def.ghi.mno ansible_ssh_port=1212

[test3]
test3-1 ansible_ssh_host=abc.def.ghi.pqr ansible_ssh_port=1212
test3-2 ansible_ssh_host=abc.def.ghi.stu ansible_ssh_port=1212

[all:children]
test1
test2
test3

角色 我尝试以以下方式访问角色:

role I have tried accessing the role in the following fashions:

{{ hostvars.ansible_ssh_host }} 

&&

{{ hostvars.test1.ansible_ssh_host }}

我正在尝试访问test1部分中的ansible_ssh_host.

I am trying to access the ansible_ssh_host in the test1 section.

错误

fatal: [localhost]: FAILED! => {"failed": true, "msg": "'ansible.vars.hostvars.HostVars object' has no attribute 'ansible'"}

推荐答案

您在关于hostvars的正确轨道上.
魔术变量用于访问有关其他主机的信息.

You are on the right track about hostvars.
This magic variable is used to access information about other hosts.

hostvars是一个以清单主机名作为键的哈希.
要访问每个主机的字段,请使用hostvars['test-1']hostvars['test2-1']等.

hostvars is a hash with inventory hostnames as keys.
To access fields of each host, use hostvars['test-1'], hostvars['test2-1'], etc.

ansible_ssh_host弃用从2.0开始支持ansible_host.
因此,您应该先删除"_ssh"从清单中托管参数(即成为"ansible_user","ansible_host"和"ansible_port"),然后以您的角色使用:

ansible_ssh_host is deprecated in favor of ansible_host since 2.0.
So you should first remove "_ssh" from inventory hosts arguments (i.e. to become "ansible_user", "ansible_host", and "ansible_port"), then in your role call it with:

{{ hostvars['your_host_group'].ansible_host }}

这篇关于在Ansible Playbook中访问广告资源宿主变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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