diff --git a/filter_plugin/ip_from_inventory.py b/filter_plugin/ip_from_inventory.py index f19551a..80e324f 100644 --- a/filter_plugin/ip_from_inventory.py +++ b/filter_plugin/ip_from_inventory.py @@ -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'] }}`` - 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): - def filter(self): + def filters(self): return { 'ip_from_inventory': ip_from_inventory, } diff --git a/roles/lxc_guest/tasks/main.yaml b/roles/lxc_guest/tasks/main.yaml index 056cfe4..dc81326 100644 --- a/roles/lxc_guest/tasks/main.yaml +++ b/roles/lxc_guest/tasks/main.yaml @@ -69,9 +69,9 @@ verbosity: 2 - 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 - 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 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" diff --git a/roles/lxc_guest/templates/config.j2 b/roles/lxc_guest/templates/config.j2 index 033770c..8cab915 100644 --- a/roles/lxc_guest/templates/config.j2 +++ b/roles/lxc_guest/templates/config.j2 @@ -10,6 +10,6 @@ lxc.network.type = veth lxc.network.flags = up lxc.network.link = br0 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 %} diff --git a/templates/interfaces.j2 b/templates/interfaces.j2 index ae1e787..d711185 100644 --- a/templates/interfaces.j2 +++ b/templates/interfaces.j2 @@ -16,6 +16,6 @@ iface br0 inet manual auto {{ ansible_default_ipv4.alias }} iface {{ ansible_default_ipv4.alias }} inet static address {{ ansible_default_ipv4.address }} - gateway {{ management_gateway }} + gateway {{ hostvars | ip_from_inventory('management_gateway') }} netmask 255.255.255.0 vlan-raw-device {{ ansible_default_ipv4.alias }}