Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ansible读取主机ip地址并在以后使用它的最佳方法是什么?_Ansible - Fatal编程技术网

使用ansible读取主机ip地址并在以后使用它的最佳方法是什么?

使用ansible读取主机ip地址并在以后使用它的最佳方法是什么?,ansible,Ansible,我有3台主机,第一台是控制器,另外两台用作指示灯。我必须在ansible中编写一个剧本,找到每个主机的ip地址,然后在同一个ansible剧本中使用它进行设置。 基本上我想把它保存在一个变量中,以后再使用它 我是这样读的 --- - hosts: localhost connection: local tasks: - debug: var=ansible_all_ipv4_addresses - debug: var=ansible_default_ipv4.addre

我有3台主机,第一台是控制器,另外两台用作指示灯。我必须在ansible中编写一个剧本,找到每个主机的ip地址,然后在同一个ansible剧本中使用它进行设置。 基本上我想把它保存在一个变量中,以后再使用它

我是这样读的

---
- 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*****
---
-主持人:实验室
变成:是的
成为用户:root
任务:
-名称:为php页面创建目录
文件:
路径:/var/www/html/virtual1
状态:目录
所有者:apache
组别:apache
模式:0775
递归:是的
-名称:确保文件存在
副本:
内容:“”
dest:/var/www/html/virtual1/info.php
所有者:apache
组别:apache
部队:没有
模式:0555
-名称:向新文件添加字符串
lineinfile:dest=/var/www/html/virtual1/info.php
regexp='^'
行=“”
状态=存在
-名称:更改文件权限
文件:
路径:/var/www/virtual1/info.php
所有者:apache
组别:apache
模式:0644
-name:设置一些内核参数
线条填充:
路径:/etc/hosts
regexp:“^”

行:“192.168.115.198 laborator1”如果您在游戏中使用
gather\u事实:yes
(这是默认值,除非您在中禁用),您将能够访问当前主机的
ansible\u default\u ipv4.address
值。
如果要访问另一台主机(在该主机上有一个聚集事实),可以使用
hostvars[INVENTORY\u HOSTNAME].ansible\u default\u ipv4.address

每个接口都有
ansible_ethX.ipv4.address

通过使用

变量文档:

在您的情况下,可能是:

- 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 }}'

如果您在游戏中使用
gather\u事实:yes
(这是默认值,除非您在中禁用),您将能够访问当前主机的
ansible\u default\u ipv4.address
值。
如果要访问另一台主机(在该主机上有一个聚集事实),可以使用
hostvars[INVENTORY\u HOSTNAME].ansible\u default\u ipv4.address

每个接口都有
ansible_ethX.ipv4.address

通过使用

变量文档:

在您的情况下,可能是:

- 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 }}'