Ansible:ansible_domain 或 facter_domain - 如何获取 list 文件中提到的主机名的域值

标签 ansible ansible-playbook ansible-2.x domain-name ansible-facts

Ansible 1.9.2/1.9.4
CentOS 6.7

在我的库存文件中,我有:

[zabbix_server]
zabbix.dev-white.projectname.jenkins

[some_other_zabbix_server]
someotherzabbix.dev-blue.project.jenkins ansible_ssh_host=10.120.133.11

当我运行时:
ansible -m setup -i hosts zabbix.dev-white.projectname.jenkins 

它给了我以下错误:

zabbix.dev-white.projectname.jenkins |失败 => 失败:没有可用的身份验证方法

在我的剧本中,我试图获取 zabbix 服务器的域值,即 dev-white.projectname.jenkins。我尝试了以下但用于显示域值的变量为空白/空,为什么?

注: inventory_hostname 的值显示正确!!但是 ansible_domain/facter_domain 的域值作为空白/空值出现。
- debug: msg="My server is= {{ hostvars[groups['zabbix_server'][0]].inventory_hostname }} and domain is= {{ hostvars[groups['zabbix_server'][0]].ansible_domain }} --- {{ hostvars[groups['zabbix_server'][0]]['inventory_hostname'] }} -- {{ hostvars[groups['zabbix_server'][0]]['ansible_domain'] }} -- -- {{ hostvars[groups['zabbix_server'][0]]['facter_domain'] }}"

为 some_other_zabbix_server 组变量(设置 ansible_ssh_host 变量)尝试了上面的代码,仍然没有值来显示其域。 PS: ansible_ssh_host 属性将在较新的 Ansible 版本中弃用。

最佳答案

一种方法是拆分和连接除第一部分之外的所有部分。

  - debug: var="{{ groups['zabbix_server'][0].split('.')[1:] | join('.') }}"

结果:
  "dev-white.projectname.jenkins"

关于Ansible:ansible_domain 或 facter_domain - 如何获取 list 文件中提到的主机名的域值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35695197/

相关文章:

regex - Ansible regex_replace-ho 提取特定值

linux - ansible:sudo 并更改用户

ansible - 尝试使用 ansible 在 vi​​rtualenv 中安装时权限被拒绝

amazon-ec2 - ansible 获取已经存在的 aws ebs 卷 ID

unix - Ansible命令来检查不同服务器中的Java版本

Ansible 主机变量未定义

docker - 如何在 Ansible 中使用 docker_image 和 docker_container 模块

debian - 使用 Ansible 使用 debconf-set-selections 自动化*仅* exim4 配置

ssh - Ansible - 为多个用户和角色管理多个 SSH key

regex - Ansible 正则表达式转义美元字符