ansible proxy_to变量未定义

时间:2019-03-24 18:45:30

标签: ansible ansible-2.x

我在尝试将事实委托给一组主机时遇到问题。我有一个主节点生成一个值,并且我想确保我的“工人”组可以访问该值(而不是通过主机名定位主节点)。

可用2.7.9 流浪汉2.2.4

VagrantFile

ansible_groups = {
    "local" => [],
    "swarm" => [],
    "swarm-master-first" => [],
    "swarm-master" => [],
    "swarm-worker" => []
  }

  (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

产生库存:

# 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

playbook.yml

- 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

输出

PLAY [swarm-master-first] ******************************************************

TASK [set_fact] ****************************************************************
ok: [swarm_master1 -> 127.0.0.1] => (item=swarm_worker1)

...

PLAY [swarm-worker] ************************************************************

TASK [Gathering Facts] *********************************************************
ok: [swarm_worker1]

TASK [debug] *******************************************************************
ok: [swarm_worker1] => {
    "swarm_token": "VARIABLE IS NOT DEFINED!"
}

我在这里做错了什么? 谢谢

1 个答案:

答案 0 :(得分:0)

变量的范围是剧本。变量“ swarm_token”的范围是第一本手册。第二本剧本对“ swarm_token”一无所知。