如何保持流浪汉配置井井有条

时间:2016-10-23 07:46:28

标签: shell configuration installation vagrant ansible

我正在配备带有ansible的流浪盒。但是,我的流浪主持人并不支持ansible,所以我必须从vagrant box中对localhost执行ansible。我使用shell配置程序,执行一个脚本,它将安装ansible并对同步文件夹中的一个剧本运行正确的ansible命令。

此解决方案有效,但由于配置数据在文件之间混乱,因此感觉很乱并且难以维护。正如您在下面看到的,存在重复的playbook路径,硬编码到Vagrantfile和配置脚本。而且正如我们所知,重复是邪恶的根源。相反,我想将所有配置字符串保留在Vagrantfile或外部文件中。在我的情况下,这可能吗?能够使用命令行参数覆盖这些值也是很好的。

这是流浪文件

VAGRANT_API_VERSION = 2
BOX_IMAGE = "ubuntu/trusty64"
PROVISIONING_SRC = "./provisioning"
PROVISIONING_DEST = "/etc/provisioning/vagrant"

Vagrant.configure(VAGRANT_API_VERSION) do |config|

  config.vm.box = BOX_IMAGE
  config.vm.network "private_network", ip: "192.168.22.22"
  config.vm.network "forwarded_port", guest: 8080, host: 8080
  config.vm.network "forwarded_port", guest: 8081, host: 8081
  config.vm.network "forwarded_port", guest: 2222, host: 2223
  config.vm.network "forwarded_port", guest: 5000, host: 5000
  config.vm.network "forwarded_port", guest: 50000, host: 50000
  config.vm.provision :shell, path: "provisioning.sh"

  config.vm.synced_folder "./provisioning", "/etc/provisioning/vagrant"

end

这是配置脚本

apt-get install -y python-pip
apt-get install -y git-core

if [ -z $PLAYBOOK_PATH ]; then
  echo "PLAYBOOK_PATH not set, using default"
  PLAYBOOK_PATH=/etc/provisioning/vagrant/playbook.yml
fi

if [ -n $PLAYBOOK_PATH ]; then
  echo "PLAYBOOK_PATH set at $PLAYBOOK_PATH"

  if [ -f $PLAYBOOK_PATH ]; then
    echo "PLAYBOOK FOUND, insalling ansible and starting provisioning"
    pip install ansible
    pip install markupsafe

    if ( ansible-playbook $PLAYBOOK_PATH ); then
      echo "provisioning OK"
    else
      echo "prvosioning failed"
    fi

  else
    echo "PLAYBOOK not found"
  fi

else
  echo "PLAYBOOK_PATH not set"
fi

最后这里是配置文件夹中的剧本

- name: Provision local docker host
  hosts: localhost
  become : yes
  roles:
   - docker.ubuntu
  tasks:
   - name: Add vagrant to docker group
     become: yes
     user: name=vagrant groups=docker append=yes

1 个答案:

答案 0 :(得分:1)

你不一定需要做这一切。

Vagrant支持running ansible on the guest VM aka ansible local

  

Vagrant Ansible Local配置程序允许您通过直接在来宾计算机上执行ansible-playbook 来使用Ansible playbooks来配置来宾

你可以拥有像

这样的Vagrant文​​件
VAGRANT_API_VERSION = 2
BOX_IMAGE = "ubuntu/trusty64"
PROVISIONING_SRC = "./provisioning"
PROVISIONING_DEST = "/etc/provisioning/vagrant"

Vagrant.configure(VAGRANT_API_VERSION) do |config|

  config.vm.box = BOX_IMAGE
  config.vm.network "private_network", ip: "192.168.22.22"
  config.vm.network "forwarded_port", guest: 8080, host: 8080
  config.vm.network "forwarded_port", guest: 8081, host: 8081
  config.vm.network "forwarded_port", guest: 2222, host: 2223
  config.vm.network "forwarded_port", guest: 5000, host: 5000
  config.vm.network "forwarded_port", guest: 50000, host: 50000

  config.vm.provision "ansible_local" do |ansible|
    ansible.playbook = "provisioning/playbook.yml"
  end

end