title: ansible delegate_to variable undefined
body: <p>I have a problem where I'm trying to delegate facts to a group of hosts. I have a master node generating a value, and I want to ensure my group of "workers" has access to that value (rather than targeting the master via hostname).</p>
  
  <p>ansible 2.7.9
  vagrant 2.2.4</p>
  
  <p>VagrantFile</p>
  
  <pre><code>ansible_groups = {
      "local" =&gt; [],
      "swarm" =&gt; [],
      "swarm-master-first" =&gt; [],
      "swarm-master" =&gt; [],
      "swarm-worker" =&gt; []
    }
  
    (1..num_masters).each do |i|
      ansible_groups["local"].push("swarm_master#{i}")
      ansible_groups["swarm"].push("swarm_master#{i}")
      if i == 1
        ansible_groups["swarm-master-first"].push("swarm_master#{i}")
      end
      ansible_groups["swarm-master"].push("swarm_master#{i}")
    end
    (1..num_workers).each do |i|
      ansible_groups["local"].push("swarm_worker#{i}")
      ansible_groups["swarm"].push("swarm_worker#{i}")
      ansible_groups["swarm-worker"].push("swarm_worker#{i}")
    end
  
    (1..num_masters).each do |i|
      config.vm.define "swarm_master#{i}" do |swarm_master|
        swarm_master.vm.network "private_network", ip: "192.168.50.1#{i}"
        swarm_master.vm.hostname = "swarmmaster#{i}"
        swarm_master.vm.provider :virtualbox do |vb|
            vb.name = "swarm-master#{i}"
        end
        swarm_master.vm.provision "ansible" do |ansible|
          ansible.playbook = "site.yml"
          ansible.become = true
          ansible.become_user = "root"
          ansible.groups = ansible_groups
        end
      end
    end
  
    (1..num_workers).each do |i|
      config.vm.define "swarm_worker#{i}" do |node|
        node.vm.network "private_network", ip: "192.168.50.2#{i}"
        node.vm.hostname = "swarmworker#{i}"
        node.vm.provider :virtualbox do |vb|
            vb.name = "swarm-worker#{i}"
        end
        node.vm.provision "ansible" do |ansible|
          ansible.playbook = "site.yml"
          ansible.become = true
          ansible.become_user = "root"
          ansible.groups = ansible_groups
        end
      end
    end
  end
  
  </code></pre>
  
  <p>resulting in an inventory:</p>
  
  <pre><code># Generated by Vagrant
  
  swarm_master1 ansible_host=127.0.0.1 ansible_port=2222 ansible_user='vagrant' ansible_ssh_private_key_file='/Users/dmcminn/.vagrant.d/insecure_private_key'
  swarm_worker1 ansible_host=127.0.0.1 ansible_port=2200 ansible_user='vagrant' ansible_ssh_private_key_file='/Users/dmcminn/.vagrant.d/insecure_private_key'
  
  [local]
  swarm_master1
  swarm_worker1
  
  [swarm]
  swarm_master1
  swarm_worker1
  
  [swarm-master-first]
  swarm_master1
  
  [swarm-master]
  swarm_master1
  
  [swarm-worker]
  swarm_worker1
  </code></pre>
  
  <p>playbook.yml</p>
  
  <pre><code>- hosts: swarm-master-first
    tasks:
      - set_fact:
          swarm_token: "TEST"
        delegate_to: "{{ item }}"
        delegate_facts: True
        with_items: "{{ groups['swarm-worker'] }}"
      #- debug: var=swarm_token
  
  - hosts: swarm-worker
    gather_facts: yes
    tasks:
      - debug: var=swarm_token
  </code></pre>
  
  <p>output</p>
  
  <pre><code>PLAY [swarm-master-first] ******************************************************
  
  TASK [set_fact] ****************************************************************
  ok: [swarm_master1 -&gt; 127.0.0.1] =&gt; (item=swarm_worker1)
  
  ...
  
  PLAY [swarm-worker] ************************************************************
  
  TASK [Gathering Facts] *********************************************************
  ok: [swarm_worker1]
  
  TASK [debug] *******************************************************************
  ok: [swarm_worker1] =&gt; {
      "swarm_token": "VARIABLE IS NOT DEFINED!"
  }
  </code></pre>
  
  <p>What am I doing wrong here?
  Thanks</p>