以下是我的阅读方式
---
- hosts: localhost
connection: local
tasks:
- debug: var=ansible_all_ipv4_addresses
- debug: var=ansible_default_ipv4.address
这是我想要使用的地方:
---
- hosts: laborator
become: yes
become_user: root
tasks:
- name: Create directory for php page
file:
path: /var/www/html/virtual1
state: directory
owner: apache
group: apache
mode: 0775
recurse: yes
- name: ensure file exists
copy:
content: ""
dest: /var/www/html/virtual1/info.php
owner: apache
group: apache
force: no
mode: 0555
- name: Add a string to the new file
lineinfile: dest=/var/www/html/virtual1/info.php
regexp='^'
line='<?php phpinfo(); ?>'
state=present
- name: Change file permissions
file:
path: /var/www/virtual1/info.php
owner: apache
group: apache
mode: 0644
- name: Set some kernel parameters
lineinfile:
path: /etc/hosts
regexp: '^'
line: '192.168.115.198 laborator1' <<<-here*****
你可以看到我需要ip的行。我是新手,这是我的第二天,请指出我正确的方向。
谢谢。答案 0 :(得分:2)
如果您在游戏中使用gather_fact: yes
(除非您在gathering
config上禁用了,否则它是默认值),您将能够访问当前主机的ansible_default_ipv4.address
值。
如果您想访问另一个主机(有一个gather_fact),您可以使用hostvars[INVENTORY_HOSTNAME].ansible_default_ipv4.address
每个界面也有ansible_ethX.ipv4.address
。
您可以使用setup
module
ansible all -m setup
在您的情况下,可能是:
- name: Put IP of each laborator hosts in /etc/hosts
lineinfile:
path: /etc/hosts
regexp: '^'
line: '{{ hostvars[item].ansible_default_ipv4.address }} {{ hostvars[item].ansible_hostname }}'
with_items: '{{ groups.laborator }}'