在Vagrant中完成所有虚拟机的构建后,Anisble遍历剧本两次

时间:2018-06-22 21:51:30

标签: ansible vagrant vagrantfile vagrant-provision

我有一个100%有效的Vagrantfile。预期的结果确实是直接的。启动8个Ubuntu 16.04 vm,然后使用Ansible剧本配置所有服务器。但是,到最后它还是很烦人,由于某种原因,剧本运行了两次,我不知道为什么。这是我的Vagrantfile。

# This guide is optimized for Vagrant 1.7 and above.
# Although versions 1.6.x should behave very similarly, it is recommended
# to upgrade instead of disabling the requirement below.
Vagrant.require_version ">= 1.7.0"

Vagrant.configure(2) do |config|
  N = 8
        (1..N).each do |hpc_id|
          config.vm.define "hpc#{hpc_id}" do |hpc|
            hpc.vm.hostname = "hpc#{hpc_id}"
            hpc.vm.network "private_network", ip: "192.168.30.#{200+hpc_id}"
            hpc.vm.box = "ubuntu/xenial64"
            hpc.ssh.insert_key = false

            hpc.vm.provider 'virtualbox' do |vb|
              vb.name = "hpc#{hpc_id}"
              vb.memory = 512
              vb.cpus = 1
              vb.gui = false

          if hpc_id == N
            hpc.vm.provision :ansible do |ansible|
              # Disable default limit to connect to all the hpcs
              ansible.limit = "all"
              ansible.playbook = "site.yml"
              ansible.become_user = "root"
              ansible.groups = {
                 "Slurm_Primary_Controller" => ["hpc1"],
                 "Slurm_Backup_Controller" => ["hpc2"],
                 "Slurm_Primary_Database" => ["hpc3"],
                 "Slurm_Backup_Database" => ["hpc4"],
                 "My_SQL_Database" => ["hpc5"],
                 "Slurm_Worker" => ["hpc6", "hpc7", "hpc8"],
              }
           end
         end
       end
     end
   end
end

以下是可运行的剧本的输出。为了简洁起见,我没有包括无用的构建输出,因为所有虚拟机都没有问题。但是,我可以根据需要添加它。

    hpc8: Running ansible-playbook...

PLAY [hpc1] ********************************************************************

TASK [install python2 for ansible] *********************************************
Friday 22 June 2018  13:34:35 -0700 (0:00:00.106)       0:00:00.106 ***********
changed: [hpc1]

TASK [Gathering Facts] *********************************************************
Friday 22 June 2018  13:35:08 -0700 (0:00:32.548)       0:00:32.654 ***********
ok: [hpc1]

TASK [hosts-setup : install required package] **********************************
Friday 22 June 2018  13:35:10 -0700 (0:00:01.934)       0:00:34.589 ***********
changed: [hpc1] => (item=[u'htop'])

PLAY [hpc2] ********************************************************************

TASK [install python2 for ansible] *********************************************
Friday 22 June 2018  13:35:24 -0700 (0:00:14.522)       0:00:49.111 ***********
changed: [hpc2]

TASK [Gathering Facts] *********************************************************
Friday 22 June 2018  13:35:56 -0700 (0:00:32.010)       0:01:21.121 ***********
ok: [hpc2]

TASK [hosts-setup : install required package] **********************************
Friday 22 June 2018  13:35:57 -0700 (0:00:01.406)       0:01:22.528 ***********
changed: [hpc2] => (item=[u'htop'])

PLAY [hpc3] ********************************************************************

TASK [install python2 for ansible] *********************************************
Friday 22 June 2018  13:36:13 -0700 (0:00:15.293)       0:01:37.822 ***********
changed: [hpc3]

TASK [Gathering Facts] *********************************************************
Friday 22 June 2018  13:36:44 -0700 (0:00:31.712)       0:02:09.535 ***********
ok: [hpc3]

TASK [hosts-setup : install required package] **********************************
Friday 22 June 2018  13:36:46 -0700 (0:00:01.873)       0:02:11.409 ***********
changed: [hpc3] => (item=[u'htop'])

PLAY [hpc4] ********************************************************************

TASK [install python2 for ansible] *********************************************
Friday 22 June 2018  13:37:01 -0700 (0:00:14.514)       0:02:25.923 ***********
changed: [hpc4]

TASK [Gathering Facts] *********************************************************
Friday 22 June 2018  13:37:30 -0700 (0:00:29.562)       0:02:55.486 ***********
ok: [hpc4]

TASK [hosts-setup : install required package] **********************************
Friday 22 June 2018  13:37:32 -0700 (0:00:01.337)       0:02:56.824 ***********
changed: [hpc4] => (item=[u'htop'])

PLAY [hpc5] ********************************************************************

TASK [install python2 for ansible] *********************************************
Friday 22 June 2018  13:37:47 -0700 (0:00:14.825)       0:03:11.650 ***********
changed: [hpc5]

TASK [Gathering Facts] *********************************************************
Friday 22 June 2018  13:38:16 -0700 (0:00:29.061)       0:03:40.712 ***********
ok: [hpc5]

TASK [hosts-setup : install required package] **********************************
Friday 22 June 2018  13:38:17 -0700 (0:00:01.376)       0:03:42.089 ***********
changed: [hpc5] => (item=[u'htop'])

PLAY [hpc6 hpc7 hpc8] **********************************************************

TASK [install python2 for ansible] *********************************************
Friday 22 June 2018  13:38:29 -0700 (0:00:11.708)       0:03:53.797 ***********
changed: [hpc8]
changed: [hpc7]
changed: [hpc6]

TASK [Gathering Facts] *********************************************************
Friday 22 June 2018  13:39:56 -0700 (0:01:27.055)       0:05:20.852 ***********
ok: [hpc8]
ok: [hpc6]
ok: [hpc7]

TASK [hosts-setup : install required package] **********************************
Friday 22 June 2018  13:39:59 -0700 (0:00:02.874)       0:05:23.727 ***********
changed: [hpc8] => (item=[u'htop'])
changed: [hpc6] => (item=[u'htop'])
changed: [hpc7] => (item=[u'htop'])

PLAY RECAP *********************************************************************
hpc1                       : ok=3    changed=2    unreachable=0    failed=0
hpc2                       : ok=3    changed=2    unreachable=0    failed=0
hpc3                       : ok=3    changed=2    unreachable=0    failed=0
hpc4                       : ok=3    changed=2    unreachable=0    failed=0
hpc5                       : ok=3    changed=2    unreachable=0    failed=0
hpc6                       : ok=3    changed=2    unreachable=0    failed=0
hpc7                       : ok=3    changed=2    unreachable=0    failed=0
hpc8                       : ok=3    changed=2    unreachable=0    failed=0

Friday 22 June 2018  13:40:32 -0700 (0:00:33.328)       0:05:57.056 ***********
===============================================================================
install python2 for ansible -------------------------------------------- 87.06s
hosts-setup : install required package --------------------------------- 33.33s
install python2 for ansible -------------------------------------------- 32.55s
install python2 for ansible -------------------------------------------- 32.01s
install python2 for ansible -------------------------------------------- 31.71s
install python2 for ansible -------------------------------------------- 29.56s
install python2 for ansible -------------------------------------------- 29.06s
hosts-setup : install required package --------------------------------- 15.29s
hosts-setup : install required package --------------------------------- 14.83s
hosts-setup : install required package --------------------------------- 14.52s
hosts-setup : install required package --------------------------------- 14.51s
hosts-setup : install required package --------------------------------- 11.71s
Gathering Facts --------------------------------------------------------- 2.87s
Gathering Facts --------------------------------------------------------- 1.94s
Gathering Facts --------------------------------------------------------- 1.87s
Gathering Facts --------------------------------------------------------- 1.41s
Gathering Facts --------------------------------------------------------- 1.38s
Gathering Facts --------------------------------------------------------- 1.34s
==> hpc8: Running provisioner: ansible...
Vagrant has automatically selected the compatibility mode '2.0'
according to the Ansible version installed (2.5.3).

Alternatively, the compatibility mode can be specified in your Vagrantfile:
https://www.vagrantup.com/docs/provisioning/ansible_common.html#compatibility_mode

    hpc8: Running ansible-playbook...

PLAY [hpc1] ********************************************************************

TASK [install python2 for ansible] *********************************************
Friday 22 June 2018  13:40:36 -0700 (0:00:00.106)       0:00:00.106 ***********
changed: [hpc1]

TASK [Gathering Facts] *********************************************************
Friday 22 June 2018  13:40:37 -0700 (0:00:00.654)       0:00:00.761 ***********
ok: [hpc1]

TASK [hosts-setup : install required package] **********************************
Friday 22 June 2018  13:40:38 -0700 (0:00:01.135)       0:00:01.897 ***********
ok: [hpc1] => (item=[u'htop'])

PLAY [hpc2] ********************************************************************

TASK [install python2 for ansible] *********************************************
Friday 22 June 2018  13:40:39 -0700 (0:00:01.110)       0:00:03.007 ***********
changed: [hpc2]

TASK [Gathering Facts] *********************************************************
Friday 22 June 2018  13:40:40 -0700 (0:00:00.690)       0:00:03.698 ***********
ok: [hpc2]

TASK [hosts-setup : install required package] **********************************
Friday 22 June 2018  13:40:41 -0700 (0:00:00.818)       0:00:04.516 ***********
ok: [hpc2] => (item=[u'htop'])

PLAY [hpc3] ********************************************************************

TASK [install python2 for ansible] *********************************************
Friday 22 June 2018  13:40:42 -0700 (0:00:00.960)       0:00:05.476 ***********
changed: [hpc3]

TASK [Gathering Facts] *********************************************************
Friday 22 June 2018  13:40:43 -0700 (0:00:01.098)       0:00:06.574 ***********
ok: [hpc3]

TASK [hosts-setup : install required package] **********************************
Friday 22 June 2018  13:40:44 -0700 (0:00:00.849)       0:00:07.424 ***********
ok: [hpc3] => (item=[u'htop'])

PLAY [hpc4] ********************************************************************

TASK [install python2 for ansible] *********************************************
Friday 22 June 2018  13:40:45 -0700 (0:00:00.968)       0:00:08.392 ***********
changed: [hpc4]

TASK [Gathering Facts] *********************************************************
Friday 22 June 2018  13:40:45 -0700 (0:00:00.615)       0:00:09.008 ***********
ok: [hpc4]

TASK [hosts-setup : install required package] **********************************
Friday 22 June 2018  13:40:46 -0700 (0:00:00.769)       0:00:09.777 ***********
ok: [hpc4] => (item=[u'htop'])

PLAY [hpc5] ********************************************************************

TASK [install python2 for ansible] *********************************************
Friday 22 June 2018  13:40:47 -0700 (0:00:00.993)       0:00:10.771 ***********
changed: [hpc5]

TASK [Gathering Facts] *********************************************************
Friday 22 June 2018  13:40:48 -0700 (0:00:00.592)       0:00:11.363 ***********
ok: [hpc5]

TASK [hosts-setup : install required package] **********************************
Friday 22 June 2018  13:40:48 -0700 (0:00:00.802)       0:00:12.166 ***********
ok: [hpc5] => (item=[u'htop'])

PLAY [hpc6 hpc7 hpc8] **********************************************************

TASK [install python2 for ansible] *********************************************
Friday 22 June 2018  13:40:49 -0700 (0:00:00.864)       0:00:13.031 ***********
changed: [hpc6]
changed: [hpc7]
changed: [hpc8]

TASK [Gathering Facts] *********************************************************
Friday 22 June 2018  13:40:49 -0700 (0:00:00.179)       0:00:13.210 ***********
ok: [hpc6]
ok: [hpc7]
ok: [hpc8]

TASK [hosts-setup : install required package] **********************************
Friday 22 June 2018  13:40:51 -0700 (0:00:01.498)       0:00:14.708 ***********
ok: [hpc6] => (item=[u'htop'])
ok: [hpc8] => (item=[u'htop'])
ok: [hpc7] => (item=[u'htop'])

PLAY RECAP *********************************************************************
hpc1                       : ok=3    changed=1    unreachable=0    failed=0
hpc2                       : ok=3    changed=1    unreachable=0    failed=0
hpc3                       : ok=3    changed=1    unreachable=0    failed=0
hpc4                       : ok=3    changed=1    unreachable=0    failed=0
hpc5                       : ok=3    changed=1    unreachable=0    failed=0
hpc6                       : ok=3    changed=1    unreachable=0    failed=0
hpc7                       : ok=3    changed=1    unreachable=0    failed=0
hpc8                       : ok=3    changed=1    unreachable=0    failed=0

Friday 22 June 2018  13:40:52 -0700 (0:00:01.522)       0:00:16.231 ***********
===============================================================================
hosts-setup : install required package ---------------------------------- 1.52s
Gathering Facts --------------------------------------------------------- 1.50s
Gathering Facts --------------------------------------------------------- 1.14s
hosts-setup : install required package ---------------------------------- 1.11s
install python2 for ansible --------------------------------------------- 1.10s
hosts-setup : install required package ---------------------------------- 0.99s
hosts-setup : install required package ---------------------------------- 0.97s
hosts-setup : install required package ---------------------------------- 0.96s
hosts-setup : install required package ---------------------------------- 0.86s
Gathering Facts --------------------------------------------------------- 0.85s
Gathering Facts --------------------------------------------------------- 0.82s
Gathering Facts --------------------------------------------------------- 0.80s
Gathering Facts --------------------------------------------------------- 0.77s
install python2 for ansible --------------------------------------------- 0.69s
install python2 for ansible --------------------------------------------- 0.65s
install python2 for ansible --------------------------------------------- 0.62s
install python2 for ansible --------------------------------------------- 0.59s
install python2 for ansible --------------------------------------------- 0.18s

如果您想从头到尾自己处理整个事情,请将其发布在Github上。我尚未完成Ansible剧本的制作,因此该项目显然是不完整的。

Github Code

2 个答案:

答案 0 :(得分:0)

hpc.vm.provision中删除hpc.vm.provider


有理由正确格式化和缩进代码。

答案 1 :(得分:0)

我已经解决了问题。这是正确的代码。在进入供应部分之前,我需要使用end关闭提供者部分。

Vagrant.require_version ">= 1.7.0"

Vagrant.configure(2) do |config|
    N = 8
    (1..N).each do |hpc_id|
      config.vm.define "hpc#{hpc_id}" do |hpc|
        hpc.vm.hostname = "hpc#{hpc_id}"
        hpc.vm.network "private_network", ip: "192.168.30.#{200+hpc_id}"
        hpc.vm.box = "ubuntu/xenial64"
        hpc.ssh.insert_key = false
        hpc.vm.provider 'virtualbox' do |vb|
          vb.name = "hpc#{hpc_id}"
          vb.memory = 512
          vb.cpus = 1
          vb.gui = false
        end

      if hpc_id == N
        hpc.vm.provision :ansible do |ansible|
          # Disable default limit to connect to all the hpcs
          ansible.limit = "all"
          ansible.playbook = "site.yml"
          ansible.become_user = "root"
          ansible.groups = {
             "Slurm_Primary_Controller" => ["hpc1"],
             "Slurm_Backup_Controller" => ["hpc2"],
             "Slurm_Primary_Database" => ["hpc3"],
             "Slurm_Backup_Database" => ["hpc4"],
             "My_SQL_Database" => ["hpc5"],
             "Slurm_Worker" => ["hpc6", "hpc7", "hpc8"],
          }
        end
      end
    end
  end
end