Esegui attività solo se l'host non appartiene a un gruppo
-
21-12-2019 - |
Domanda
Mi piacerebbe eseguire un'attività ansible solo se l'host del Playbook corrente non appartiene a un determinato gruppo.In Semi Pseudo Codice:
- name: my command
command: echo stuff
when: "if {{ ansible_hostname }} not in {{ ansible_current_groups }}"
.
Come dovrei fare questo?
Soluzione
Ecco un altro modo per farlo:
- name: my command
command: echo stuff
when: "'groupname' not in group_names"
.
group_names
è una variabile magica come documentata qui: http://docs.ansible.com/playbooks_variables.html#magic-variablebles-and-how-to-access-information-about-other-hosts :
.Group_names è un elenco (array) di tutti i gruppi che l'host corrente è in.
Altri suggerimenti
È possibile impostare una variabile di controllo nei file VARS situati in group_vars/
o direttamente nel file host come questo:
[vagrant:vars]
test_var=true
[location-1]
192.168.33.10 hostname=apollo
[location-2]
192.168.33.20 hostname=zeus
[vagrant:children]
location-1
location-2
.
ed esegui attività come questa:
- name: "test"
command: "echo {{test_var}}"
when: test_var is defined and test_var
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow