简单记录一下在ansible脚本执行中获取单个节点(当前处理节点)ip地址的方法
方法一:
---- name: Set host ip value set_fact:node_ip: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"node_ip: "{{ ansible_facts.default_ipv4.address }}"- name: Display host ip informationdebug:var: node_ip
方法二:
---- name: Collect IP informationsetup:filter: ansible_default_ipv4- name: show host ip value debug:var: ansible_facts.default_ipv4.address- name: Set host ip value set_fact:node_ip: "{{ ansible_facts.default_ipv4.address }}"- name: Display host ip informationdebug:var: node_ip
上述方法都需要配置gather_facts: true 在一定程度上会影响执行的时间.
方法三:
---- name: Set host ip value set_fact:node_ip: "{{ ansible_host }}"- name: Display host ip informationdebug:var: node_ip
方法三不需要配置gather_facts: true 直接使用ansible_host获取当前处理节点的ip地址