K
K
Konstantin Rudenkov2016-12-08 17:51:06
Ansible
Konstantin Rudenkov, 2016-12-08 17:51:06

What could be wrong with my ansible?

Hello. I came across an interesting problem.
Input data (ansible 2.2):
1. Inventory

[api]
api01.domain.tld max_fails=12 fail_timeout=70 
api02.domain.tld max_fails=13 fail_timeout=70 
api03.domain.tld max_fails=12 fail_timeout=70 
api04.domain.tld max_fails=12 fail_timeout=70

2. Template for describing nginx upstreams (the second loop is important):
{% for upstream1 in nginx_upstreams1.iteritems() %}
upstream {{ upstream1[0] }} {
{% for item in upstream1[1] %}
  {{ item }};
{% endfor %}
{% for host in groups[upstream1[0]] %}
  # {{ host }}
  server {{ hostvars[host].ansible_eth0.ipv4.address }} ;
  {{ hostvars[host].max_fails }}
{% endfor %}
}
{% endfor %}

# max_fails {{ hostvars['api01.domain.tld'].max_fails }}
# max_fails {{ hostvars['api02.domain.tld'].max_fails }}

3. As a result, I get that
- {{ hostvars[host].max_fails }}- gives "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'max_fails'"
- when I specify the hostname in hostvars by hand, everything works correctly ( # max_fails {{ hostvars['api01 .domain.tld'].max_fails }} )
- when I watch ad-hoc debug output with -m debug, or output hostvars[host] to a rendered template, max_fails is there.
4. with fail_timeout the same situation.
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alexander, 2016-12-09
@beza2000

I suppose that instead of {{ hostvars[host].max_fails }}, you just need to write {{ max_fails }}
Depending on the current apiXX.domain.tld, the required max_fails will be taken.

P
Puma Thailand, 2016-12-17
@opium

it is obvious that the facts were not collected from the api if it swears at such an error
, the api section is probably empty

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question