随心所欲地运行Ansible Playbook

Vagrant是开发人员最喜欢的工具之一。 Vagrant是一种工具,旨在使用您选择的虚拟化环境(VirtualBox,KVM,VMware e.t.c)创建和配置轻量级,可复制的便携式开发环境。

将Ansible与Vagrant结合使用可自动为Vagrant计算机提供需求并设置软件依赖项。本指南不会详细介绍Vagrant和Ansible,而仅概述如何将Ansible与Vagrant结合使用。

创建一个项目目录。

$ mkdir ~/myvagrant
$ cd  ~/myvagrant

创建一个简单的Ansible Playbook,更新操作系统并安装基本软件包。可以扩展以涵盖需要设置的内容。

$ vim provision.yaml
---
- hosts: all
  become: yes
  become_method: sudo
  tasks:
 - name: Update OS
   package:
    name: '*'
    state: latest

  - name: Install Basic packages
    package:
      name: ['vim', 'zip', 'bash-completion', 'wget', 'tmux']

接下来,使用ansible置备工具的定义创建Vagrantfile。

$ vim Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'
Vagrant.configure("2") do |config|
  ##### VM definition #####
  config.vm.define "cent01" do |config|
  config.vm.hostname = "cent01"
  config.vm.box = "centos/8"
  config.vm.box_check_update = false
  config.vm.provision :ansible do |ansible|
    ansible.limit = "all"
    ansible.playbook = "provision.yaml"
  end
  config.vm.provider :libvirt do |v|
    v.memory = 1024
    v.cpus = 1
    end
  end
end

如Vagrantfile中所示,我们使用Provision.yaml Playbook。

准备好Playbook和Vagrantfile之后,运行vagrant 启动虚拟机的命令。

$ vagrant up
Bringing machine 'cent01' up with 'libvirt' provider...
==> cent01: Box 'centos/8' could not be found. Attempting to find and install...
    cent01: Box Provider: libvirt
    cent01: Box Version: >= 0
==> cent01: Loading metadata for box 'centos/8'
    cent01: URL: https://vagrantcloud.com/centos/8
==> cent01: Adding box 'centos/8' (v1905.1) for provider: libvirt
    cent01: Downloading: https://vagrantcloud.com/centos/boxes/8/versions/1905.1/providers/libvirt.box
    cent01: Download redirected to host: cloud.centos.org
    cent01: Calculating and comparing box checksum...
==> cent01: Successfully added box 'centos/8' (v1905.1) for 'libvirt'!
==> cent01: Uploading base box image as volume into libvirt storage...
==> cent01: Creating image (snapshot of base box volume).
==> cent01: Creating domain with the following settings...
==> cent01:  -- Name:              cent8_cent01
==> cent01:  -- Domain type:       kvm
==> cent01:  -- Cpus:              2
==> cent01:  -- Feature:           acpi
==> cent01:  -- Feature:           apic
==> cent01:  -- Feature:           pae
==> cent01:  -- Memory:            1024M
==> cent01:  -- Management MAC:    
==> cent01:  -- Loader:            
==> cent01:  -- Nvram:             
==> cent01:  -- Base box:          centos/8
==> cent01:  -- Storage pool:      default
==> cent01:  -- Image:             /var/home/jkmutai/.local/share/libvirt/images/cent8_cent01.img (11G)
==> cent01:  -- Volume Cache:      default
==> cent01:  -- Kernel:            
==> cent01:  -- Initrd:            
==> cent01:  -- Graphics Type:     vnc
==> cent01:  -- Graphics Port:     -1
==> cent01:  -- Graphics IP:       127.0.0.1
==> cent01:  -- Graphics Password: Not defined
==> cent01:  -- Video Type:        cirrus
==> cent01:  -- Video VRAM:        9216
==> cent01:  -- Sound Type:	
==> cent01:  -- Keymap:            en-us
==> cent01:  -- TPM Path:          
==> cent01:  -- INPUT:             type=mouse, bus=ps2
==> cent01: Creating shared folders metadata...
==> cent01: Starting domain.
==> cent01: Waiting for domain to get an IP address...
==> cent01: Waiting for SSH to become available...
    cent01: 
    cent01: Vagrant insecure key detected. Vagrant will automatically replace
    cent01: this with a newly generated keypair for better security.
    cent01: 
    cent01: Inserting generated public key within guest...
    cent01: Removing insecure key from the guest if it's present...
    cent01: Key inserted! Disconnecting and reconnecting using new SSH key...
==> cent01: Setting hostname...
==> cent01: Configuring and enabling network interfaces...
    cent01: SSH address: 192.168.122.48:22
    cent01: SSH username: vagrant
    cent01: SSH auth method: private key
==> cent01: Rsyncing folder: /var/home/jkmutai/myhacks/vagrant/cent8/ => /vagrant
==> cent01: Running provisioner: ansible...
Vagrant has automatically selected the compatibility mode '2.0'
according to the Ansible version installed (2.9.5).

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

    cent01: Running ansible-playbook...

PLAY [all] *********************************************************************

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

TASK [Update OS] ************************************************
changed: [cent01]

TASK [Install Basic packages] ************************************************
changed: [cent01]

PLAY RECAP *********************************************************************
cent01                     : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

如果您正在运行Vagrant计算机,请使用以下命令:

vagrant provision

执行成功。您可以使用以下命令来SSH VM:

$ vagrant ssh

已读 流浪者置备指南 了解有关受支持的预配器以及如何使用它们的更多信息。

其他指南:

在Windows上启用Hyper-V并安装Vagrant

如何在CentOS 8 / RHEL 8上安装Vagrant

在KVM / VirtualBox / VMWare / Parallels上运行带有Vagrant的CentOS 8 VM

如何在Fedora上安装Vagrant和VirtualBox

如何使用Vagrant在VirtualBox上运行Kali Linux

Sidebar