获取当前目标主机的 IP 地址

如何获取角色中当前主机的 IP 地址?

我知道你可以得到组的名单,主机是一个成员和主机的主机名,但我无法找到一个解决方案,以获得 IP 地址。

您可以使用 {{inventory_hostname}}获得主机名,使用 {{group_names}}获得组名

我试过像 {{ hostvars[{{ inventory_hostname }}]['ansible_ssh_host'] }}这样的东西 和 ip="{{ hostvars.{{ inventory_hostname }}.ansible_ssh_host }}"

383160 次浏览

您可以从 hostvars、 dictansible_default_ipv4和密钥 address获得 IP 地址

hostvars[inventory_hostname]['ansible_default_ipv4']['address']

IPv6地址

hostvars[inventory_hostname]['ansible_default_ipv6']['address']

一个例子:

---
- hosts: localhost
tasks:
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv4']['address']
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv6']['address']

所有地址的列表存储在事实 ansible_all_ipv4_addresses中,这是 ansible_default_ipv4.address中的默认地址。

---
- hosts: localhost
connection: local
tasks:
- debug: var=ansible_all_ipv4_addresses
- debug: var=ansible_default_ipv4.address

然后有地址分配给每个网络接口... 在这种情况下,你可以显示所有的事实,并找到一个有你想要使用的价值。

您可以在 template.j2 \{\{ ansible_eth0.ipv4.address }}中使用与使用 \{\{inventory_hostname}}相同的方法。

Ps: 请参考下面的博客文章,以获得更多关于如何收集远程主机信息的信息(https://www.data-essential.com/anable-HOW-TO-Collection-INFORMATION-ABOUT-REMOTE-HOSTS-WITH-GATHERS-acts/”rel = “ noReferrer”) .

希望有一天能帮到某人

如果你想要 外部公共知识产权,并且你处在像 AWS 或 Azure 这样的云环境中,你可以使用 Ipify _ acts 模块:

# TODO: SECURITY: This requires that we trust ipify to provide the correct public IP. We could run our own ipify server.
- name: Get my public IP from ipify.org
ipify_facts:

这将把公共 IP 放到变量 ipify_public_ip中。

Http://docs.ansible.com/ansible/latest/plugins/lookup/dig.html

因此,在模板中,例如:

\{\{ lookup('dig', ansible_host) }}

备注:

  • 因为不仅 DNS 名称可以用于库存检查,如果它不是 IP 已经更好地添加
  • 很明显,这个收据不能满足间接主机规范的要求(比如使用跳转主机)

但它仍然服务于99% 的用例(比喻地说)。

下面的代码片段将返回远程计算机的公共 ip 和默认 ip (即: LAN)

这也将打印引号的 ip,以避免使用配置文件的混乱。

>> main.yml


---
- hosts: localhost
tasks:
- name: ipify
ipify_facts:
- debug: var=hostvars[inventory_hostname]['ipify_public_ip']
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv4']['address']
- name: template
template:
src: debug.j2
dest: /tmp/debug.ansible


>> templates/debug.j2


public_ip=\{\{ hostvars[inventory_hostname]['ipify_public_ip'] }}
public_ip_in_quotes="\{\{ hostvars[inventory_hostname]['ipify_public_ip'] }}"


default_ipv4=\{\{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}
default_ipv4_in_quotes="\{\{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"

另一种寻找公共 IP 的方法是使用 uri模块:

    - name: Find my public ip
uri:
url: http://ifconfig.me/ip
return_content: yes
register: ip_response

你的 IP 地址是 ip_response.content

普通的 ansible_default_ipv4.address可能不是你在 有些案子中想的那样,使用:

ansible_default_ipv4.address|default(ansible_all_ipv4_addresses[0])

简单的调试命令:

ansible -i inventory/hosts.yaml -m debug -a "var=hostvars[inventory_hostname]" all

产出:

"hostvars[inventory_hostname]": {
"ansible_check_mode": false,
"ansible_diff_mode": false,
"ansible_facts": {},
"ansible_forks": 5,
"ansible_host": "192.168.10.125",
"ansible_inventory_sources": [
"/root/workspace/ansible-minicros/inventory/hosts.yaml"
],
"ansible_playbook_python": "/usr/bin/python2",
"ansible_port": 65532,
"ansible_verbosity": 0,
"ansible_version": {
"full": "2.8.5",
"major": 2,
"minor": 8,
"revision": 5,
"string": "2.8.5"
},

获取主机 IP 地址:

ansible -i inventory/hosts.yaml -m debug -a "var=hostvars[inventory_hostname].ansible_host" all


zk01 | SUCCESS => {
"hostvars[inventory_hostname].ansible_host": "192.168.10.125"
}

只要使用 ansible_ssh_host变量

Playbook _ example. yml

- hosts: host1
tasks:
- name: Show host's ip
debug:
msg: "\{\{ ansible_ssh_host }}"

招待所

[hosts]
host1   ansible_host=1.2.3.4

结果

TASK [Show host's ip] *********************************************************************************************************************************************************************************************
ok: [host1] => {
"msg": "1.2.3.4"
}

在我的例子中,我需要保留 IP,然后使用 delegate_to运行一个命令,所以我在变量中写了以下内容:

leader_ip: '\{\{ansible_ssh_host}}'

然后我用了 leader_ip

另一种解决方案是使用 hostvar:

hostvars[INVENTORY_HOSTNAME]['ansible_host']

这种方法对我很有效:

this_node_ip: '\{\{ hostvars[inventory_hostname]['ansible_env'].SSH_CONNECTION.split(' ')[2] }}'