【问题标题】:Ansible hostname and IP addressAnsible 主机名和 IP 地址
【发布时间】:2021-01-18 09:18:54
【问题描述】:

如何使用 hosts 库存文件中的主机名和 IP 地址的值?

例如,我在hosts 文件中只有一台主机,名称为 FQDN,但这是在 DNS 服务器上注册的。

我尝试了一些变量,但总是得到主机名。但是,两者都需要:(

对 DNS 服务器的请求输出:

 nslookup host1.dinamarca.com
 Server:        10.10.1.1
 Address:   10.10.1.1#53

 Name:  host1.dinamarca.com
 Address: 192.168.1.10

示例host 文件:(只有一个主机)

 host1.dinamarca.com

我使用以下命令调用服务:

 ansible-playbook --ask-pass -i hosts test.yml

我的 test.yml 文件:

 ---
 - name: test1
   hosts: host1.dinamarca.com
   remote_user: usertest

   tasks:
   - name: show ansible_ssh_host
     debug:
       msg: "{{ ansible_ssh_host }}"
   - name: show inventary_hostname
     debug: var=inventory_hostname

   - name: show ansible_hostname
     debug: var=ansible_hostname
 ...

输出是:

TASK [show ansible_ssh_host]            ****************************************************************************************************************************************
ok: [host1.dinamarca.com] => {
         "msg": "host1.dinamarca.com"
}

TASK [show inventary_hostname]      **************************************************************************************************************************************
ok: [host1.dinamarca.com] => {
         "inventory_hostname": "host1.dinamarca.com"
}

TASK [show ansible_hostname]      ****************************************************************************************************************************************
ok: [host1.dinamarca.com] => {
    "ansible_hostname": "host1"
}

PLAY RECAP      ************************************************************************************************     *************************************************************
host1.dinamarca.com     : ok=4    changed=0    unreachable=0    failed=0    skipped=0              rescued=0    ignored=0  

【问题讨论】:

  • 你希望输出是什么?
  • 我相信 dig lookup{{ lookup("dig", inventory_hostname) }} 一样是你所追求的

标签: variables dns ansible ip hostname


【解决方案1】:

有一个名为 ansible_fqdn 的 Ansible 事实。如果您需要主机名和 FQDN,则可以执行以下任务:

tasks:
  - name: show ansible_ssh_host
    debug:
      msg: "{{ ansible_ssh_host }}"
  - name: show inventory_hostname
    debug:
      msg: "{{ inventory_hostname }}"
  - name: show ansible_hostname
    debug: 
      msg: "{{ ansible_fqdn }}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 2014-12-02
    • 1970-01-01
    • 2012-05-31
    • 2013-03-25
    • 2017-02-10
    相关资源
    最近更新 更多