what is the best way to use ansible for reading hosts ip address and use it later? -


i have 3 hosts, first 1 controller , other 2 used lamp. have write playbook in ansible finds ip address of each host , use later in same ansible playbook settings. want save in variable , use later.

here how read it

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

this want use it:

--- - hosts: laborator   become: yes   become_user: root    tasks:     - name: create directory 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 string 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 kernel parameters       lineinfile:         path: /etc/hosts         regexp: '^'         line: '192.168.115.198 laborator1'  <<<-here***** 

you can see line need ip. i'm new ansible second day, please point me right direction.

thank you.

if use gather_fact: yes in play (it's default value unless disabled in on gathering config), able access ansible_default_ipv4.address value current host.
if want access host (on there gather_fact), can use hostvars[inventory_hostname].ansible_default_ipv4.address.
there ansible_ethx.ipv4.address each interface.

you can see available variables using setup module

ansible -m setup 

variables doc: http://docs.ansible.com/ansible/latest/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts

in case, be:

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

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -