SJC*_*SJC 78 ansible ansible-template ansible-facts
如何获得角色中当前主机的IP地址?
我知道您可以获取主机所属的组列表以及主机的主机名,但我无法找到获取IP地址的解决方案.
您可以使用{{inventory_hostname}}和使用组来获取主机名{{group_names}}
我尝试过像{{ hostvars[{{ inventory_hostname }}]['ansible_ssh_host'] }}
和ip="{{ hostvars.{{ inventory_hostname }}.ansible_ssh_host }}"
tec*_*raf 102
所有地址的列表都存储在一个事实中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
Run Code Online (Sandbox Code Playgroud)
然后为每个网络接口分配地址......在这种情况下,您可以显示所有事实并找到具有您要使用的值的事实.
Pas*_*i H 59
您可以从hostvars,dict ansible_default_ipv4和key 获取IP地址address
hostvars[inventory_hostname]['ansible_default_ipv4']['address']
Run Code Online (Sandbox Code Playgroud)
和IPv6地址分别
hostvars[inventory_hostname]['ansible_default_ipv6']['address']
Run Code Online (Sandbox Code Playgroud)
一个示例剧本:
---
- hosts: localhost
tasks:
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv4']['address']
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv6']['address']
Run Code Online (Sandbox Code Playgroud)
Ors*_*ius 23
您可以在template.j2中{{ ansible_eth0.ipv4.address }}使用与使用相同的方式{{inventory_hostname}}.
ps:请参阅以下博客文章,了解有关如何收集有关远程主机信息的 更多信息.
'希望它有一天会帮助某人ッ
Raf*_*alS 12
在某些情况下,普通ansible_default_ipv4.address可能不是您所想的,请使用:
ansible_default_ipv4.address|default(ansible_all_ipv4_addresses[0])
Run Code Online (Sandbox Code Playgroud)
简单的调试命令:
ansible -i inventory/hosts.yaml -m debug -a "var=hostvars[inventory_hostname]" all
Run Code Online (Sandbox Code Playgroud)
输出:
"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"
},
Run Code Online (Sandbox Code Playgroud)
获取主机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"
}
Run Code Online (Sandbox Code Playgroud)
只需使用ansible_ssh_host变量
playbook_example.yml
- hosts: host1
tasks:
- name: Show host's ip
debug:
msg: "{{ ansible_ssh_host }}"
Run Code Online (Sandbox Code Playgroud)
主机.yml
[hosts]
host1 ansible_host=1.2.3.4
Run Code Online (Sandbox Code Playgroud)
结果
TASK [Show host's ip] *********************************************************************************************************************************************************************************************
ok: [host1] => {
"msg": "1.2.3.4"
}
Run Code Online (Sandbox Code Playgroud)
http://docs.ansible.com/ansible/latest/plugins/lookup/dig.html
所以在模板中,例如:
{{ lookup('dig', ansible_host) }}
Run Code Online (Sandbox Code Playgroud)
笔记:
但它仍然服务于 99%(形象地说)的用例。
小智 6
查找公共 IP 的另一种方法是使用uri模块:
- name: Find my public ip
uri:
url: http://ifconfig.me/ip
return_content: yes
register: ip_response
Run Code Online (Sandbox Code Playgroud)
您的 IP 将在 ip_response.content
如果您需要外部公共 IP并且您处于 AWS 或 Azure 等云环境中,则可以使用ipify_facts 模块:
# 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:
Run Code Online (Sandbox Code Playgroud)
这会将公共 IP 放入变量中ipify_public_ip。
| 归档时间: |
|
| 查看次数: |
186421 次 |
| 最近记录: |