Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If set_fact is scoped to a host, can I use 'dummy' host as a global variable map?

Tags:

ansible

I have defined two group of hosts: wmaster and wnodes. Each group runs in its play:

- hosts: wmaster
  roles:
    - all
    - swarm-mode
  vars:
    - swarm_master: true

- hosts: wnodes
  roles:
    - all
    - swarm-mode

I use host variables (swarm_master) to define different behavior of some role.

Now, my first playbook performs some initialization and I need to share data with the nodes. What I did is to use set_fact in first play, and then to lookup in the second play:

- set_fact:
    docker_worker_token: "{{ hostvars[smarm_master_ip].foo }}"

I don't like using the swarm_master_ip. How about to add a dummy host: global with e.g. address 1.1.1.1 that does not get any role, and serves just for holding the global facts/variables?

like image 993
igr Avatar asked Dec 15 '22 04:12

igr


1 Answers

If you're using Ansible 2 then you can take use of delegate_facts during your first play:

- name: set fact on swarm nodes
  set_fact: docker_worker_token="{{ some_var }}"
  delegate_to: "{{ item }}"
  delegate_facts: True
  with_items: "{{ groups['wnodes'] }}"

This should delegate the set_fact task to every host in the wnodes group and will also delegate the resulting fact to those hosts as well instead of setting the fact on the inventory host currently being targeted by the first play.

like image 122
ydaetskcoR Avatar answered Apr 16 '23 17:04

ydaetskcoR