Ansible 主机名和 IP 地址

问题描述 投票:0回答:1

如何使用

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:

 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  
variables dns ansible ip hostname
1个回答
2
投票

有一个 Ansible 事实称为

ansible_fqdn
。如果您同时需要主机名和 FQDN,您可以执行如下任务:

tasks:
  - name: show ansible_ssh_host
    debug:
      msg: "{{ ansible_ssh_host }}"
  - name: show inventory_hostname
    debug:
      msg: "{{ inventory_hostname }}"
  - name: show ansible_fqdn
    debug: 
      msg: "{{ ansible_fqdn }}"
© www.soinside.com 2019 - 2024. All rights reserved.