Por favor, explique-me como posso resolver este problema?
Eu tenho esse arquivo defaults/main.yml
---
node1:
ip: 1.1.1.1
node2:
ip: 2.2.2.2
node3:
ip: 3.3.3.3
Agora, eu quero no arquivo de modelo ip.j2
by for loop
acessar os IPs de cada servidor e salvar em address
variable
Assim:
address= 1.1.1.1,2.2.2.2,3.3.3.3
Eu tentei este código:
address={% for x in {{nubmer_nodes}} %}
{{node[x].ip}}
{% if loop.last %},{% endif %}
{% endfor %}
Mas um erro ocorre. Como devo fazer isso?
Erro :
TASK [Gathering Facts] *********************************************************************
ok: [db2]
ok: [db3]
ok: [db1]
TASK [ssh : -- my loop --] *************************************************************************
fatal: [db1]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: expected token ':', got '}'. String: \r\naddress={% for x in {{number_nodes}} %}\r\n{{node[x].ip}}\r\n{% if loop.last %},{% endif %}\r\n{% endfor %}"}
fatal: [db2]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: expected token ':', got '}'. String: \r\naddress={% for x in {{number_nodes}} %}\r\n{{node[x].ip}}\r\n{% if loop.last %},{% endif %}\r\n{% endfor %}"}
fatal: [db3]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: expected token ':', got '}'. String: \r\naddress={% for x in {{number_nodes}} %}\r\n{{node[x].ip}}\r\n{% if loop.last %},{% endif %}\r\n{% endfor %}"}
to retry, use: --limit @/etc/ansible/playbooks/get_ip_ssh.retry
PLAY RECAP ********************************************************
db1 : ok=1 changed=0 unreachable=0 failed=1
db2 : ok=1 changed=0 unreachable=0 failed=1
db3 : ok=1 changed=0 unreachable=0 failed=1
Edite-1
Alterei o código template
e default/main.yml
. Eu tenho nomes (nós) mas não consigo acessar IPs ainda.
default/main.yml
:
nodes:
node1:
ip: 1.1.1.1
node2:
ip: 2.2.2.2
node3:
ip: 3.3.3.3
get-ip.j2
address={% for host in nodes %}{{host}}{% if not loop.last %},{% endif %}{% endfor %}
A saída é:
address=node1,node3,node2
Eu também usei este código:
address={% for host in nodes %}{{host.ip}}{% if not loop.last %},{% endif %}{% endfor %}
OR
address={% for host in nodes %}{{host.[ip]}}{% if not loop.last %},{% endif %}{% endfor %}
Mas não funciona ainda !!
Atualizar
Meu problema foi resolvido, eu uso este código:
address={% for host in nodes %}{{ nodes[host].ip }}{% if not loop.last %},{% endif %}{% endfor %}