On Sun, 2 Jul 2023 17:12:33 +0200 (CEST)
dulhaver via Ansible Project <[email protected]> wrote:

> - ansible.builtin.set_fact:
>     level: keycloak
>   when: ansible_fqdn == VM-0312.step.zrz.internal

Make your live easier and put the logic into the *vars*. For example,

  shell> cat group_vars/all/level.yml
  level_dict:
    VM-0311.step.zrz.internal: passkey
    VM-0312.step.zrz.internal: keycloak
    VM-0313.step.zrz.internal: falsekey
  level: "{{ level_dict[ansible_fqdn] }}"

Given the inventory for testing

  shell> cat hosts
  host1 ansible_fqdn=VM-0311.step.zrz.internal
  host2 ansible_fqdn=VM-0312.step.zrz.internal
  host3 ansible_fqdn=VM-0313.step.zrz.internal

use *set_fact" if you want to 'instantiate' the variable (put the
variable into the *hostvars*). You can omit *set_fact* if you don't
need hostvars.*.level. For example, the play

  - hosts: all
    tasks:
      - set_fact:
          level: "{{ level }}"
      - debug:
          var: level

gives (abridged)

  ok: [host1] => 
    level: passkey
  ok: [host2] => 
    level: keycloak
  ok: [shot3] => 
    level: falsekey

As a result, the code is cleaner. Also the concentration of data into
a single point of failure (a dictionary and assignment isolated in
*vars*) makes the code more robust.

-- 
Vladimir Botka

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20230703113413.5372d6c1%40gmail.com.

Attachment: pgp4HQt7dfawf.pgp
Description: OpenPGP digital signature

Reply via email to