Home > OS >  Ansible Modify a Variable if any registed fact changed
Ansible Modify a Variable if any registed fact changed

Time:09-10

I'm trying to modify a value, if all tasks are ok, it should have a value = A, if one or more tasks need change it should have a value = B. I am registering all task results in a variable and I have a conditional to change the subject and then send an email.

- name: Task05:summary, set setting_config fact
  set_fact:
    settings_config: false
  when: task01.changed == false and task02.changed == false

- name: Task006:summary, set summary fact 
  set_fact:
    sujectmail : "{{ 'Ansible email Summary Status:green ' if settings_config == false  else 'Ansible email Report  Status: red!' }}"

- name: task10 report HTML report for infrastructure
  template:
    src: "{{ settings_report_template }}"
    dest: "{{ settings_report_file }}"
  delegate_to: localhost
  run_once: true
  check_mode: no
 
- name: task11 Send email report 
  mail:
    host: "{{ mail_gateway }}"
    port: "{{ mail_gateway_port }}"
    sender: "{{ mail_sender }}"
    subtype: html
    to: "{{ mail_recipients }}"
    subject: "{{sujectm}}"
    body: "{{ lookup('file', config_check_report_file) }}"
    secure: starttls
  register: mail_output
  delegate_to: localhost
  run_once: true
  check_mode: no

The problem with that is task10/task11 is selecting only one host. Is there any way that it can find in all registered facts and if it finds "settings_config" variable it applies the corresponding value instead just one or the latest host?

CodePudding user response:

Q: "Find in all registered 'settings_config'."

A: Put the below declaration into the vars and create the dictionary

    configs: "{{ dict(ansible_play_hosts|
                      zip(ansible_play_hosts|
                          map('extract', hostvars, 'settings_config')|
                          list)) }}"

For example, given the inventory for testing

shell> cat hosts
host_1 tasks_changed=true
host_2 tasks_changed=true
host_3 tasks_changed=false

the playbook

- hosts: host_1,host_2,host_3
  gather_facts: false
  vars:

    configs: "{{ dict(ansible_play_hosts|
                      zip(ansible_play_hosts|
                          map('extract', hostvars, 'settings_config')|
                          list)) }}"

  tasks:

    - name: Task05:summary, set setting_config fact
      set_fact:
        settings_config: "{{ tasks_changed|bool }}"

    - debug:
        var: configs
      run_once: true

gives (abridged)

  configs:
    host_1: true
    host_2: true
    host_3: false

Notes

  • Simplify the code. Instead of the condition
- name: Task05:summary, set setting_config fact
  set_fact:
    settings_config: false
  when: task01.changed == false and task02.changed == false

set the variable directly. You don't have to test the existence of this variable later.

- name: Task05:summary, set setting_config fact
  set_fact:
    settings_config: task01.changed or task02.changed
  • Related