'Deleted parameter files were not removed with anisble change
I've deleted the parameter in overrides.yml file but still that file entry is there in the instance.
For an instance: if i remove instance_name: xyz01 entry in the overrides.yml and ran the ansible playbook but still xyz01 entry will be there in the metrics server..why is this skipping? cat overrides.yml
abc:
- instance_name: xyz01
description: "Test instance "
color: ""
icon: ""
server_port: 123
management_port: 123
- instance_name: xyz02
description: "Test instance "
color: ""
icon: ""
server_port: 123
management_port: 123
if you see here, in order to create abc-xyz0* files, abc element is used, which does not have xyz03 cat abc.yml
- hosts: group-metrics
tasks:
- name: "Provision abc Metrics"
include_role:
name: metrics
vars:
abc: "{{ item }}"
with_items: "{{ abc | default([]) }}"
which uses this task
---
- name: "{{ abc.instance_name }} - Create abc Prometheus Exporter"
template:
src: metrics-exporter.yml.j2
dest: "/etc/prometheus/abc-{{ abc.instance_name }}.yml"
owner: prometheus
group: prometheus
mode: 0644
which uses this template metrics-exporter.yml.j2
- targets:
{% for host in groups[abc] %}
- {{ host }}:{{ abc.management_port | default(123) }}| abc-{{ abc.instance_name }}
{% endfor %}
abc group contains : 3 instances
Solution 1:[1]
The vars declaration below is wrong. First, the vars is evaluated, not with_items. Therefore item is missing
- name: "Provision abc Metrics"
include_role:
name: metrics
vars:
abc: "{{ item }}"
with_items: "{{ abc | default([]) }}"
You should have seen the error:
TASK [include_role : metrics] ***************************************
fatal: [localhost]: FAILED! =>
msg: '{{ item }}: ''item'' is undefined'
If you intend to use the variable abc for the iteration use loop_control. For example, given the data and the role below (simplified for testing)
abc:
- instance_name: xyz01
- instance_name: xyz02
shell> cat roles/metrics/tasks/main.yml
- debug:
var: abc
The task below
- include_role:
name: metrics
loop: "{{ abc|default([]) }}"
loop_control:
loop_var: abc
gives
TASK [include_role : metrics] ********************************************
[WARNING]: The loop variable 'abc' is already in use. You should set the `loop_var` value in
the `loop_control` option for the task to something else to avoid variable collisions and
unexpected behavior.
TASK [metrics : debug] ***************************************************
ok: [localhost] =>
abc:
instance_name: xyz01
TASK [metrics : debug] ***************************************************
ok: [localhost] =>
abc:
instance_name: xyz02
The iteration works but Ansible complains about abc being already used. Use another variable to fix it. For example,
shell> cat roles/metrics/tasks/main.yml
- debug:
var: abc_item
- include_role:
name: metrics
loop: "{{ abc|default([]) }}"
loop_control:
loop_var: abc_item
works fine
TASK [include_role : metrics] **********************************************
TASK [metrics : debug] *****************************************************
ok: [localhost] =>
abc_item:
instance_name: xyz01
TASK [metrics : debug] *****************************************************
ok: [localhost] =>
abc_item:
instance_name: xyz02
The next problem is the selection of the group in the template
{% for host in groups[abc] %}
...
The variable abc is a list, not a name of a group. If you intend to iterate the hosts from the group 'abc' declare it. For example
shell> cat hosts
[abc]
abc_1
abc_2
abc_3
Then use it in the template (simplified for testing)
shell> cat roles/metrics/templates/metrics-exporter.yml.j2
- targets:
{% for host in groups.abc %}
- {{ host }}:{{ abc_item.instance_name }}
{% endfor %}
The template task should work as expected
shell> cat roles/metrics/tasks/main.yml
- template:
src: metrics-exporter.yml.j2
dest: "/tmp/abc-{{ abc_item.instance_name }}.yml"
gives
shell> cat /tmp/abc-xyz01.yml
- targets:
- abc_1:xyz01
- abc_2:xyz01
- abc_3:xyz01
shell> cat /tmp/abc-xyz02.yml
- targets:
- abc_1:xyz02
- abc_2:xyz02
- abc_3:xyz02
If you want to control the presence of the files add an attribute to the list, e.g.
abc:
- instance_name: xyz01
present: false
- instance_name: xyz02
and use it in the tasks
shell> cat roles/metrics/tasks/main.yml
- file:
state: absent
dest: "/tmp/abc-{{ abc_item.instance_name }}.yml"
when: not abc_item.present|d(true)|bool
- template:
src: metrics-exporter.yml.j2
dest: "/tmp/abc-{{ abc_item.instance_name }}.yml"
when: abc_item.present|d(true)|bool
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
