Browse Source

get ip from inventory with filter

python3
Edoardo Putti 8 years ago
parent
commit
ad9a5d2c92
4 changed files with 9 additions and 11 deletions
  1. +4
    -6
      filter_plugin/ip_from_inventory.py
  2. +2
    -2
      roles/lxc_guest/tasks/main.yaml
  3. +2
    -2
      roles/lxc_guest/templates/config.j2
  4. +1
    -1
      templates/interfaces.j2

+ 4
- 6
filter_plugin/ip_from_inventory.py View File

@ -1,14 +1,12 @@
from ansible.hostvars import HostVars
def ip_from_inventory(hostname):
def ip_from_inventory(hostvars, hostname):
""" """
replace this ``{{ hostvars[hostname]['ansible_host'] }}`` replace this ``{{ hostvars[hostname]['ansible_host'] }}``
with something nicer such as `` {{ hostname | ip_from_inventory }}``
with something nicer such as `` {{ hostvars | ip_from_inventory(hostname) }}``
""" """
return HostVars[hostname]['ansible_host']
return hostvars[hostname]['ansible_host']
class FilterModule(object): class FilterModule(object):
def filter(self):
def filters(self):
return { return {
'ip_from_inventory': ip_from_inventory, 'ip_from_inventory': ip_from_inventory,
} }


+ 2
- 2
roles/lxc_guest/tasks/main.yaml View File

@ -69,9 +69,9 @@
verbosity: 2 verbosity: 2
- name: update container DNS configuration - name: update container DNS configuration
shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "grep -Pz1 'domain lilik.it\nnameserver {{ vm_gateway }}' /etc/resolv.conf || echo -e 'domain lilik.it\nnameserver {{ vm_gateway }}' > /etc/resolv.conf"
shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "grep -Pz1 'domain lilik.it\nnameserver {{ hostvars | ip_from_inventory('vm_gateway')) }}' /etc/resolv.conf || echo -e 'domain lilik.it\nnameserver {{ hostvars | ip_from_inventory('vm_gateway') }}' > /etc/resolv.conf"
register: container_dns_configuration register: container_dns_configuration
changed_when: container_dns_configuration.stdout != "domain lilik.it\nnameserver {{ vm_gateway }}\n\u0000"
changed_when: container_dns_configuration.stdout != "domain lilik.it\nnameserver {{ hostvars | ip_from_inventory('vm_gateway') }}\n\u0000"
- name: update container network configuration - name: update container network configuration
shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "grep -F 'iface eth0 inet manual' /etc/network/interfaces || sed -i 's/iface eth0 inet dhcp/iface eth0 inet manual/' /etc/network/interfaces" shell: lxc-attach -n {{ vm_name }} --clear-env -e -- bash -c "grep -F 'iface eth0 inet manual' /etc/network/interfaces || sed -i 's/iface eth0 inet dhcp/iface eth0 inet manual/' /etc/network/interfaces"


+ 2
- 2
roles/lxc_guest/templates/config.j2 View File

@ -10,6 +10,6 @@ lxc.network.type = veth
lxc.network.flags = up lxc.network.flags = up
lxc.network.link = br0 lxc.network.link = br0
lxc.network.name = eth0 lxc.network.name = eth0
lxc.network.ipv4 = {{ hostvars[vm_name]['ansible_host'] }}/24
lxc.network.ipv4.gateway = {{ vm_gateway }}
lxc.network.ipv4 = {{ hostvars | ip_from_inventory(vm_name) }}/24
lxc.network.ipv4.gateway = {{ hostvars | ip_from_inventory('vm_gateway') }}
lxc.start.auto = {% if auto_start %}1{% else %}0{% endif %} lxc.start.auto = {% if auto_start %}1{% else %}0{% endif %}

+ 1
- 1
templates/interfaces.j2 View File

@ -16,6 +16,6 @@ iface br0 inet manual
auto {{ ansible_default_ipv4.alias }} auto {{ ansible_default_ipv4.alias }}
iface {{ ansible_default_ipv4.alias }} inet static iface {{ ansible_default_ipv4.alias }} inet static
address {{ ansible_default_ipv4.address }} address {{ ansible_default_ipv4.address }}
gateway {{ management_gateway }}
gateway {{ hostvars | ip_from_inventory('management_gateway') }}
netmask 255.255.255.0 netmask 255.255.255.0
vlan-raw-device {{ ansible_default_ipv4.alias }} vlan-raw-device {{ ansible_default_ipv4.alias }}

Loading…
Cancel
Save