我们使用 Vagrant+ VirtualBox 创建虚拟机

这里对于Vagrant以及VirtualBox的安装使用就不再过多介绍,有需要小伙伴请移步到以下文章参考了解学习。

目录介绍

  • ansible-code[1-4]为宿主机与虚拟机之间的共享目录

    ├─ansible-code1   # hostname:controller  ip:192.168.56.5  box: CentOS
    ├─ansible-code2   # hostname:node1  ip:192.168.56.6  box: CentOS
    ├─ansible-code3   # hostname:node2  ip:192.168.56.7  box: CentOS
    └─ansible-code4   # hostname:node3  ip:192.168.56.4  box: Ubuntu
    └─box             # 放box镜像
    └─init.sh
    └─vagrantfile

提前下载centos.box 和 ubuntu.box 到本地的box目录

vagrant box add centos ./box/centos.box 
vagrant box add ubuntu ./box/ubuntu.box 
vagrant box list
centos      (virtualbox, 0)
ubuntu      (virtualbox, 0)

init.sh

  • 设置时区
  • 设置可以密码登陆
  • 在controller机器上设置节点host
  • 安装一些软件
  • 在controller使用expect免交互式生成并发送ssh_key到节点服务器
#!/usr/bin/sh

run_ssh_keygen(){
        rm -rf $rsa_pub
        /usr/bin/expect<<EOF
        set timeout 10
        spawn ssh-keygen -t rsa -b 2048
        expect {
               "Enter file in" {send "\n"; exp_continue}
               "Overwrite (y/n)" {send "y\n"; exp_continue}
               "Enter passphrase" {send "\n"; exp_continue}
               "passphrase again" {send "\n"; exp_continue}
           }
EOF
}

send_ssh_key(){
        pwd=vagrant
        /usr/bin/expect<<EOF
        set timeout 30
        spawn ssh-copy-id vagrant@$1
        expect {
              "connecting (yes/no)?" {send "yes\n"; exp_continue}
              "password:" {send "$pwd\n"; exp_continue}
        }
EOF
}

sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sudo systemctl restart sshd

if [ "$HOSTNAME" = "controller" ]; then
        sudo yum install -y epel-release git vim gcc expect glibc-static telnet ansible
        sudo sh -c "echo 192.168.56.6 node1 >> /etc/hosts"
        sudo sh -c "echo 192.168.56.7 node2 >> /etc/hosts"
        sudo sh -c "echo 192.168.56.4 node3 >> /etc/hosts"

        rsa_pub=$HOME/.ssh/id_rsa.pub

        if [ ! -f $rsa_pub ]; then
            run_ssh_keygen
        fi

        if [ -f $rsa_pub ]; then
            for node in 1 2 3
            do
                send_ssh_key "node$node"
            done
        fi
fi

vagrantfile

hosts = [
    {
         :box => 'centos',
         :define => 'controller',
         :hostname =>'controller',
         :private_network => '192.168.56.5',
         :vb_name => 'controller',
         :synced_folder =>{
          :local => 'E:/code/test/vm/Ansible/ansible-code1',
          :virtual => '/www/ansible-code/test'
         }
    },
    {
     :box => 'centos',
         :define => 'node1',
         :hostname =>'node1',
         :private_network => '192.168.56.6',
         :vb_name => 'node1',
         :synced_folder =>{
            :local => 'E:/code/test/vm/Ansible/ansible-code2',
            :virtual => '/www/ansible-code/test'
          }
         
    },
    {
     :box => 'centos',
         :define => 'node2',
         :hostname =>'node2',
         :private_network => '192.168.56.7',
         :vb_name => 'node2',
         :synced_folder =>{
            :local => 'E:/code/test/vm/Ansible/ansible-code3',
            :virtual => '/www/ansible-code/test'
         }
    },
    {
         :box => 'ubuntu',
         :define => 'node3',
         :hostname =>'node3',
         :private_network => '192.168.56.4',
         :vb_name => 'node3',
         :synced_folder =>{
            :local => 'E:/code/test/vm/Ansible/ansible-code4',
            :virtual => '/www/ansible-code/test'
         }
    }
]

Vagrant.configure("2") do |config|
    hosts.each do |item|
         config.vm.define item[:define] do |host|
        host.vm.box = item[:box]
            host.vm.hostname = item[:hostname]
            host.vm.network "private_network", ip: item[:private_network]
            if item[:synced_folder]
               host.vm.synced_folder item[:synced_folder][:local], item[:synced_folder][:virtual],mount_options: ["dmode=775","fmode=664"]
            end
            host.vm.provider "virtualbox" do |vb|
                vb.memory = "1024"
                vb.cpus = "1"
                vb.name = item[:vb_name]
                vb.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ]
            end
         end
    end
    config.vm.provision "shell", privileged: false, path: "./init.sh"
end

Dingtalk_20220721175147.jpg

github 源码地址

测试 ansible 连通性

mkdir my_ansible_dir && cd my_ansible_dir
cp -rpP /etc/ansible/* .
rm -rf roles/
>hosts
echo -e "[all]\nnode1\nnode2\nnode3" >hosts
sed -i "14c inventory      = ./hosts" ansible.cfg
[vagrant@controller my_ansible_dir]$ ansible all -m ping
node3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
Last modification:July 22, 2022
如果觉得我的文章对你有用,请随意赞赏