在 Windows 上使用 vagrant 进行多机 ansible 设置 [英] Multi-machine ansible setup with vagrant on windows

查看:39
本文介绍了在 Windows 上使用 vagrant 进行多机 ansible 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要 ansible 在我的 Windows 8 机器上配置虚拟机 vm [通过 Vagrant].一切都需要在本地运行,因为 Ansible 不能在 Windows 上运行,我用 ansible 作为控制机器引导一个 debian vm.此代码作为示例.在与系统苦苦挣扎后,我让它有些工作,但并不完全(虽然 ansible 没有告诉我).

I want ansible to provision virtual box vm's on my windows 8 machine [via Vagrant]. Everything needs to run locally and since Ansible doesn't run on Windows, I bootstrap a debian vm with ansible as the control machine. This code served as an example. After struggling with the system I got it somewhat working, but not completely (although ansible doesn't tell me).

如果需要,使用 ansible [在 vm]、vagrant 和 virtualbox [在 Windows 主机上] 的多机设置需要什么配置:

What configuration is required for a multi-machine setup using ansible [in a vm], vagrant and virtualbox [on windows host] if we want:

  1. ssh 从主机访问 ansible-vm 以及所有从机

  1. ssh acces from the host machine to the ansible-vm as well as all the slaves

ssh 从 ansible-vm 访问所有从属

ssh acces from the ansible-vm to all the slaves

如果可能,能够将多机网络与主机网络隔离

being able to shield the multi-machine network from the host's network, if possible

问题

运行 ansible -m ping -all -i path-to-hosts 会产生 ssh 错误.似乎 ansible 尝试访问名为 web1 和 db1 的机器,但找不到这样的主机.

Problem

Running ansible -m ping -all -i path-to-hosts yields ssh errors. It seems ansible tries to reach the machines named web1 and db1, but can't find such hosts.

为用户建立连接:vagrantREMOTE_MODULE ping为用户建立连接:vagrantREMOTE_MODULE pingEXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/home/vagrant/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'ConnectTimeout=10', 'web1', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1398362619.41-142470238612762 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1398362619.41-142470238619.41-142470238612&& chmod a+rx-1398362619.41-142470238612762'"]未为 web1 找到 EXEC 先前已知的主机文件EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/home/vagrant/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'ConnectTimeout=10', 'db1', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1398362619.41-4982781019922 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1398362619.41-4982781019827810199222 && chmod a+rx-1398362619.41-4982781019922'"]找不到 db1 的 EXEC 先前已知的主机文件网1 |失败 => SSH 遇到未知错误.输出是:OpenSSH_6.0p1 Debian-4、OpenSSL 1.0.1e 2013 年 2 月 11 日debug1:读取配置数据/etc/ssh/ssh_configdebug1:/etc/ssh/ssh_config 第 19 行:为 * 应用选项debug1:自动复用:尝试现有的主debug1:控制套接字/home/vagrant/.ansible/cp/ansible-ssh-web1-22-vagrant"不存在debug2:ssh_connect:needpriv 0ssh:无法解析主机名 web1:名称或服务未知

ESTABLISH CONNECTION FOR USER: vagrant REMOTE_MODULE ping ESTABLISH CONNECTION FOR USER: vagrant REMOTE_MODULE ping EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/home/vagrant/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'ConnectTimeout=10', 'web1', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1398362619.41-142470238612762 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1398362619.41-142470238612762 && echo $HOME/.ansible/tmp/ansible-tmp-1398362619.41-142470238612762'"] EXEC previous known host file not found for web1 EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/home/vagrant/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'ConnectTimeout=10', 'db1', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1398362619.41-4982781019922 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1398362619.41-4982781019922 && echo $HOME/.ansible/tmp/ansible-tmp-1398362619.41-4982781019922'"] EXEC previous known host file not found for db1 web1 | FAILED => SSH encountered an unknown error. The output was: OpenSSH_6.0p1 Debian-4, OpenSSL 1.0.1e 11 Feb 2013 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: Applying options for * debug1: auto-mux: Trying existing master debug1: Control socket "/home/vagrant/.ansible/cp/ansible-ssh-web1-22-vagrant" does not exist debug2: ssh_connect: needpriv 0 ssh: Could not resolve hostname web1: Name or service not known

db1 |失败 => SSH 遇到未知错误.输出是:OpenSSH_6.0p1 Debian-4、OpenSSL 1.0.1e 2013 年 2 月 11 日debug1:读取配置数据/etc/ssh/ssh_configdebug1:/etc/ssh/ssh_config 第 19 行:为 * 应用选项debug1:自动复用:尝试现有的主debug1:控制套接字/home/vagrant/.ansible/cp/ansible-ssh-db1-22-vagrant"不存在debug2:ssh_connect:needpriv 0ssh:无法解析主机名 db1:名称或服务未知

db1 | FAILED => SSH encountered an unknown error. The output was: OpenSSH_6.0p1 Debian-4, OpenSSL 1.0.1e 11 Feb 2013 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: Applying options for * debug1: auto-mux: Trying existing master debug1: Control socket "/home/vagrant/.ansible/cp/ansible-ssh-db1-22-vagrant" does not exist debug2: ssh_connect: needpriv 0 ssh: Could not resolve hostname db1: Name or service not known

代码

以下代码尝试提供1. ansible-master:运行ansible的控制机器1. db1:数据库服务器1. web1:一个网络服务器

Code

The following code tries to provision 1. ansible-master: the control machine running ansible 1. db1: a database server 1. web1: a web server

Vagrant.configure("2") do |config|
  config.vm.box = "wheezy64"
  config.vm.box_url = "http://puppet-vagrant-boxes.puppetlabs.com/debian-70rc1-x64-vbox4210.box"
  config.vm.synced_folder ".", "/vagrant", :mount_options => ['dmode=777','fmode=666']

  config.vm.network :public_network

  config.vm.provider "virtualbox" do |v|
      v.customize [
          "modifyvm", :id,
          "--groups", "/Vagrant/Ansible",
         # "--natdnshostresolver1", "on"
      ]
  end

  config.vm.define :ansiblemaster do |ansiblemaster|
   # ansiblemaster.vm.network :private_network, ip: "192.168.111.101"
   ansiblemaster.vm.hostname = "ansiblemaster"
   # ansiblemaster.vm.network :forwarded_port, guest: 80, host: 8080
    ansiblemaster.ssh.forward_agent = true


    ansiblemaster.vm.provider :virtualbox do |vb|
      vb.customize ["modifyvm", :id, "--memory", 512]
      vb.customize ["modifyvm", :id, "--name", "ansible-master"]
      vb.name = "ansiblemaster"
    end

    ansiblemaster.vm.provision :shell, :inline =>
      "if [[ ! -f /apt-get-run ]]; then sudo apt-get update && sudo touch /apt-get-run; fi"



    ansiblemaster.vm.provision :shell do |sh|
      sh.path = "provision.sh"
      sh.args = "./ansible provisioning/site.yml provisioning/hosts/dev_hosts"
    end
  end

  config.vm.define :web1 do |slave|
   slave.vm.hostname = "web1"
   # slave.vm.network :private_network, ip: "192.168.111.201"
    slave.vm.synced_folder "./src", "/var/www/site", id: "proj-root"
    slave.vm.provider :virtualbox do |vb|
      vb.name = "web1"
      vb.customize ["modifyvm", :id, "--memory", "512"]
    end
  end

  config.vm.define :db1 do |slave|
    slave.vm.hostname = "db1"
    #slave.vm.network :private_network, ip: "192.168.111.202"
    slave.vm.provider :virtualbox do |vb|
      vb.name = "db1"
      vb.customize ["modifyvm", :id, "--memory", "512"]
    end
  end
end

Provision.sh

#!/bin/bash

ANSIBLE_DIR=$1
ANSIBLE_PLAYBOOK=$2
ANSIBLE_HOSTS=$3
TEMP_HOSTS="/tmp/ansible_hosts"

if [ ! -f /vagrant/$ANSIBLE_PLAYBOOK ]; then
  echo "Cannot find Ansible playbook"
  exit 1
fi

if [ ! -f /vagrant/$ANSIBLE_HOSTS ]; then
  echo "Cannot find Ansible hosts"
  exit 2
fi

if [ ! -d $ANSIBLE_DIR ]; then
  echo "Updating apt cache"
  apt-get update
  echo "Installing Ansible dependencies and Git"
  apt-get install -y git python-yaml python-paramiko python-jinja2
  echo "Cloning Ansible"
  git clone git://github.com/ansible/ansible.git ${ANSIBLE_DIR}
fi

cd ${ANSIBLE_DIR}
cp /vagrant/${ANSIBLE_HOSTS} ${TEMP_HOSTS} && chmod -x ${TEMP_HOSTS}
echo "Running Ansible"
echo "dir is nu: " $(pwd)

source hacking/env-setup
echo "source ${ANSIBLE_DIR}/hacking/env-setup" >> /home/vagrant/.bashrc
ansible-playbook /vagrant/${ANSIBLE_PLAYBOOK} --inventory-file=${TEMP_HOSTS} --connection=local

rm ${TEMP_HOSTS}

配置/主机/dev_hosts

[webservers]
web1

[dbservers]
db1

推荐答案

回答我自己的问题:问题是通过升级ansible并在Provision.sh中导入其他机器的ssh密钥解决的.

To answer my own question: the problem was resolved by upgrading ansible and importing the ssh keys of the other machines in Provision.sh.

# fix permissions on private key file
chmod 600 /home/vagrant/.ssh/id_rsa

# add web/database hosts to known_hosts (IP is defined in Vagrantfile)
ssh-keyscan -H 192.168.51.4 >> /home/vagrant/.ssh/known_hosts
ssh-keyscan -H 192.168.52.4 >> /home/vagrant/.ssh/known_hosts
chown vagrant:vagrant /home/vagrant/.ssh/known_hosts

# reload ssh in order to load the known hosts
/etc/init.d/ssh reload

这篇关于在 Windows 上使用 vagrant 进行多机 ansible 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆