[Gelöst] Ansible Variable löschen?

Moin,

kann sich jemand diesen Müll hier erklären:

Ich hab möchte die Rolle für die Gretaps so ändern, dass man zu mehreren Servern Tunnel aufbauen kann, damit wir mit dem neuen Kartenbackend experimentieren können. Dazu hab ich folgendes in der gateways_gretap/templates/gretap.j2 geändert:

-{% set hosts = ['services'] %}
+{% set hosts = none  %}
+{% set hosts = groups['services'] %}

(die Minus und Plus am Anfang sind vom Diff)

Ganze Vorlage:

# This file is managed by ansible, don't make changes here - they will be overwritten.
{% set indexer = [0] %}
{% for domaene in domaenenliste|dictsort %}
{% set hosts = none  %}
{% set hosts = groups['services'] %}
{{ hosts | to_nice_json }}
{% for host in groups['gateways'] %}
{% if ansible_ssh_host != hostvars[host].ansible_ssh_host and domaene[0] in hostvars[host].domaenenliste and hosts.append(host) %}
{% endif %}
{% endfor %}
{{ hosts | to_nice_json }}
{% for host in hosts %}
{% if hostvars[host] is defined %}
{% if indexer.append(indexer.pop() + 1) %}{% endif %}{# increment indexer by 1 #}

# GRETAP Tunnel for domaene-{{domaene[0]}} to {{host}} ({{hostvars[host].ansible_ssh_host}})
auto t{{domaene[0]}}-{{host}}
iface t{{domaene[0]}}-{{host}} inet manual
        pre-up ip link add $IFACE type gretap local {{ansible_default_ipv4.address}} remote {{hostvars[host].ansible_ssh_host}} dev eth0 key {{domaene[0]|int}}
        pre-up ip link set dev $IFACE address de:ad:be:ef:{{indexer[0]}}:{{vm_id}}
        pre-up ip link set $IFACE up
        post-up batctl -m bat{{domaene[0]}} if add $IFACE ||:
        pre-down batctl -m bat{{domaene[0]}} if del $IFACE ||:
        post-down ip link del $IFACE
{% endif %}
{% endfor %}
{% endfor %}

Die to_nice_json sind nur Debugausgaben. So und da kommt dann das Folgende bei raus:

TASK [gateways_gretap : Create interfaces - inter_gre file] ********************
changed: [remue-08]
--- before
+++ after: dynamically generated
@@ -0,0 +1,317 @@
+# This file is managed by ansible, don't make changes here - they will be overwritten.
+[
+    "services", 
+    "karteneu"
+]
+[
+    "services", 
+    "karteneu", 
+    "des2"
+]
+
+# GRETAP Tunnel for domaene-07 to services (89.163.231.228)
+auto t07-services
+iface t07-services inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 89.163.231.228 dev eth0 key 7
+        pre-up ip link set dev $IFACE address de:ad:be:ef:1:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat07 if add $IFACE ||:
+        pre-down batctl -m bat07 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-07 to karteneu (148.251.208.172)
+auto t07-karteneu
+iface t07-karteneu inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.208.172 dev eth0 key 7
+        pre-up ip link set dev $IFACE address de:ad:be:ef:2:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat07 if add $IFACE ||:
+        pre-down batctl -m bat07 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-07 to des2 (148.251.45.45)
+auto t07-des2
+iface t07-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 7
+        pre-up ip link set dev $IFACE address de:ad:be:ef:3:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat07 if add $IFACE ||:
+        pre-down batctl -m bat07 if del $IFACE ||:
+        post-down ip link del $IFACE
+[
+    "services", 
+    "karteneu", 
+    "des2"
+]
+[
+    "services", 
+    "karteneu", 
+    "des2", 
+    "des2"
+]
+
+# GRETAP Tunnel for domaene-08 to services (89.163.231.228)
+auto t08-services
+iface t08-services inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 89.163.231.228 dev eth0 key 8
+        pre-up ip link set dev $IFACE address de:ad:be:ef:4:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat08 if add $IFACE ||:
+        pre-down batctl -m bat08 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-08 to karteneu (148.251.208.172)
+auto t08-karteneu
+iface t08-karteneu inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.208.172 dev eth0 key 8
+        pre-up ip link set dev $IFACE address de:ad:be:ef:5:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat08 if add $IFACE ||:
+        pre-down batctl -m bat08 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-08 to des2 (148.251.45.45)
+auto t08-des2
+iface t08-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 8
+        pre-up ip link set dev $IFACE address de:ad:be:ef:6:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat08 if add $IFACE ||:
+        pre-down batctl -m bat08 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-08 to des2 (148.251.45.45)
+auto t08-des2
+iface t08-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 8
+        pre-up ip link set dev $IFACE address de:ad:be:ef:7:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat08 if add $IFACE ||:
+        pre-down batctl -m bat08 if del $IFACE ||:
+        post-down ip link del $IFACE
+[
+    "services", 
+    "karteneu", 
+    "des2", 
+    "des2"
+]
+[
+    "services", 
+    "karteneu", 
+    "des2", 
+    "des2", 
+    "des2"
+]
+
+# GRETAP Tunnel for domaene-09 to services (89.163.231.228)
+auto t09-services
+iface t09-services inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 89.163.231.228 dev eth0 key 9
+        pre-up ip link set dev $IFACE address de:ad:be:ef:8:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat09 if add $IFACE ||:
+        pre-down batctl -m bat09 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-09 to karteneu (148.251.208.172)
+auto t09-karteneu
+iface t09-karteneu inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.208.172 dev eth0 key 9
+        pre-up ip link set dev $IFACE address de:ad:be:ef:9:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat09 if add $IFACE ||:
+        pre-down batctl -m bat09 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-09 to des2 (148.251.45.45)
+auto t09-des2
+iface t09-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 9
+        pre-up ip link set dev $IFACE address de:ad:be:ef:10:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat09 if add $IFACE ||:
+        pre-down batctl -m bat09 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-09 to des2 (148.251.45.45)
+auto t09-des2
+iface t09-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 9
+        pre-up ip link set dev $IFACE address de:ad:be:ef:11:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat09 if add $IFACE ||:
+        pre-down batctl -m bat09 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-09 to des2 (148.251.45.45)
+auto t09-des2
+iface t09-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 9
+        pre-up ip link set dev $IFACE address de:ad:be:ef:12:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat09 if add $IFACE ||:
+        pre-down batctl -m bat09 if del $IFACE ||:
+        post-down ip link del $IFACE
+[
+    "services", 
+    "karteneu", 
+    "des2", 
+    "des2", 
+    "des2"
+]
+[
+    "services", 
+    "karteneu", 
+    "des2", 
+    "des2", 
+    "des2", 
+    "remue-09"
+]
+
+# GRETAP Tunnel for domaene-10 to services (89.163.231.228)
+auto t10-services
+iface t10-services inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 89.163.231.228 dev eth0 key 10
+        pre-up ip link set dev $IFACE address de:ad:be:ef:13:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat10 if add $IFACE ||:
+        pre-down batctl -m bat10 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-10 to karteneu (148.251.208.172)
+auto t10-karteneu
+iface t10-karteneu inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.208.172 dev eth0 key 10
+        pre-up ip link set dev $IFACE address de:ad:be:ef:14:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat10 if add $IFACE ||:
+        pre-down batctl -m bat10 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-10 to des2 (148.251.45.45)
+auto t10-des2
+iface t10-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 10
+        pre-up ip link set dev $IFACE address de:ad:be:ef:15:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat10 if add $IFACE ||:
+        pre-down batctl -m bat10 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-10 to des2 (148.251.45.45)
+auto t10-des2
+iface t10-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 10
+        pre-up ip link set dev $IFACE address de:ad:be:ef:16:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat10 if add $IFACE ||:
+        pre-down batctl -m bat10 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-10 to des2 (148.251.45.45)
+auto t10-des2
+iface t10-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 10
+        pre-up ip link set dev $IFACE address de:ad:be:ef:17:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat10 if add $IFACE ||:
+        pre-down batctl -m bat10 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-10 to remue-09 (148.251.208.170)
+auto t10-remue-09
+iface t10-remue-09 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.208.170 dev eth0 key 10
+        pre-up ip link set dev $IFACE address de:ad:be:ef:18:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat10 if add $IFACE ||:
+        pre-down batctl -m bat10 if del $IFACE ||:
+        post-down ip link del $IFACE
+[
+    "services", 
+    "karteneu", 
+    "des2", 
+    "des2", 
+    "des2", 
+    "remue-09"
+]
+[
+    "services", 
+    "karteneu", 
+    "des2", 
+    "des2", 
+    "des2", 
+    "remue-09", 
+    "des2"
+]
+
+# GRETAP Tunnel for domaene-11 to services (89.163.231.228)
+auto t11-services
+iface t11-services inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 89.163.231.228 dev eth0 key 11
+        pre-up ip link set dev $IFACE address de:ad:be:ef:19:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat11 if add $IFACE ||:
+        pre-down batctl -m bat11 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-11 to karteneu (148.251.208.172)
+auto t11-karteneu
+iface t11-karteneu inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.208.172 dev eth0 key 11
+        pre-up ip link set dev $IFACE address de:ad:be:ef:20:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat11 if add $IFACE ||:
+        pre-down batctl -m bat11 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-11 to des2 (148.251.45.45)
+auto t11-des2
+iface t11-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 11
+        pre-up ip link set dev $IFACE address de:ad:be:ef:21:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat11 if add $IFACE ||:
+        pre-down batctl -m bat11 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-11 to des2 (148.251.45.45)
+auto t11-des2
+iface t11-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 11
+        pre-up ip link set dev $IFACE address de:ad:be:ef:22:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat11 if add $IFACE ||:
+        pre-down batctl -m bat11 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-11 to des2 (148.251.45.45)
+auto t11-des2
+iface t11-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 11
+        pre-up ip link set dev $IFACE address de:ad:be:ef:23:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat11 if add $IFACE ||:
+        pre-down batctl -m bat11 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-11 to remue-09 (148.251.208.170)
+auto t11-remue-09
+iface t11-remue-09 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.208.170 dev eth0 key 11
+        pre-up ip link set dev $IFACE address de:ad:be:ef:24:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat11 if add $IFACE ||:
+        pre-down batctl -m bat11 if del $IFACE ||:
+        post-down ip link del $IFACE
+
+# GRETAP Tunnel for domaene-11 to des2 (148.251.45.45)
+auto t11-des2
+iface t11-des2 inet manual
+        pre-up ip link add $IFACE type gretap local 148.251.208.175 remote 148.251.45.45 dev eth0 key 11
+        pre-up ip link set dev $IFACE address de:ad:be:ef:25:12
+        pre-up ip link set $IFACE up
+        post-up batctl -m bat11 if add $IFACE ||:
+        pre-down batctl -m bat11 if del $IFACE ||:
+        post-down ip link del $IFACE
+

Mit anderen Worten, Jinja2 löscht die Variable hosts nicht korrekt. Selbst mit None geht es nicht, hab mir gerade die Finger wund gegoogelt, nichts gefunden.

Der fügt nur immer neue an, löscht aber nicht die alten Einträge. Dadurch werden viel zu viele Tunnel angelegt. Scheinbar wird set list = neue_list so interpretiert, dass nur die ersten length(neue_list) Einträge in list überschrieben werden und die restlichen unverändert bleiben.

Hat jemand eine Idee? @Fungur @descilla

Grüße
Matthias

Ich kann mich noch daran erinnern, dass Ansible “set” in for-Schleifen gar nicht kann. Evtl. muss man mit hosts.remove() oder hosts.pop() in einer Schleife die hosts-Liste wieder leer machen.

versuchs mal mit hosts.update(…)

Mein “Hack” war ein dict oder list in die variable zu schreiben. dann kann man mit “normaler” python syntax modifizieren.

Beispiel: https://github.com/FreiFunkMuenster/ansible-hypervisor/blob/master/roles/vm-network/templates/interfaces.j2 Zeile 2 und Zeile 6

Ich seh gerade, es gibt auch clear(). Versuch mal hosts.clear(). Und danach ggf. das nötige mit hosts.append() wieder dran fügen.

Was ich bisher erfolglos probiert habe:

hosts.clear()
{% if hosts[:] = [] %}
{% endif %}
{% set hosts[:] = [] %}

Ist alles keine gültige Syntax.

Tippfehler bei hosts.clar() ?

Ne, Jinja2 kennt den Befehl glaube ich gar nicht. Das ist Python.

Da wir drei hier nicht weiterkommen, hab ich mal bei Stackoverflow eine Frage eingereicht: http://stackoverflow.com/questions/40877359/ansible-jinja2-reset-variable

Ich hab was funktionierendes:

# This file is managed by ansible, don't make changes here - they will be overwritten.
{% set indexer = [0] %}
{% set hosts = [] %}
{% for domaene in domaenenliste|dictsort %}
{% for dummy in hosts %}
{% if hosts.pop() %}
{% endif %}
{% endfor %}
{% for host in groups['services'] %}
{% if hosts.append(host) %}
{% endif %}
{% endfor %}
{% for host in groups['gateways'] %}
{% if ansible_ssh_host != hostvars[host].ansible_ssh_host and domaene[0] in hostvars[host].domaenenliste and hosts.append(host) %}
{% endif %}
{% endfor %}
{% for host in hosts %}
{% if hostvars[host] is defined %}
{% if indexer.append(indexer.pop() + 1) %}{% endif %}{# increment indexer by 1 #}

Bei mir hat das aber noch immer 2x services-Tunnel pro Domäne gebaut. Muss wohl irgendwie 2x in der groups[‘services’] drin stecken.

1 Like