用 Ansible 来交付 Vagrant 实例
Ansible Vagrant
![](/data/attachment/album/201610/21/091639zf4wsqslsz3g5002.jpg)


Ansible 是一款系统管理员进行自动化运维的强大工具。Ansible 让配置、交付、管理各种容器、软件部署[变得非常简单](https://ansible.com/)。基于轻量级模块的架构非常适合系统管理，一个优点就是如果某个节点没有被 Ansible 管理的话，它的资源就不会被使用。


这篇文章介绍用 Ansible 来配置 [Vagrant 实例](https://www.vagrantup.com/)，它是一个配置好的基础虚拟机映像，包含了开发环境中需要用到的工具。你可以用它来部署开发环境，然后和其他成员协同工作。用 Ansible，你可以用你的开发包自动化交付 Vagrant 实例。


我们用 Fedora 24 做主机，用 [CentOS](https://centos.org/) 7 来作 Vagrant 实例。


### 设置工作环境


在用 Ansible 配置 Vagrant 实例时，你需要做几件准备的事情。首先在宿主机上安装 Ansible 和 Vagrant，在你的主机上运行下面的命令来安装：



```
sudo dnf install ansible vagrant vagrant-libvirt

```

上面的命令将 Ansible 和 Vagrant 在你的宿主机上，以及包括 Vagrant 的 libvirt 接口。Vagrant 并没有提供托管你的虚拟机的功能，它需要第三方工具比如：libirt、VirtualBox、VMWare 等等。这些工具可以直接与你的 Fedora 系统上的 libvirt 和 KVM 协同工作。


接着确认你的账户在正确的 wheel 用户组当中，确保你可以运行系统管理员命令。如果你的账号在安装过程中就创建为管理员，那么你就肯定在这个用户组里。运行下面的命令查看：



```
id | grep wheel

```

如果你能看到输出，那么你的账户就在这个组里，可以进行下一步。如果没有的话，你需要运行下面的命令，这一步需要你提供 root 账户的密码，将 <username> 换成你的用户名：



```
su -c 'usermod -a -G wheel <username>'

```

然后，你需要注销然后重新登录，确保在用户组里。


现在要建立你的第一个 Vagrant 实例了，你需要用 Ansible 来配置它。


### 设置 Vagrant 实例


配置一个镜像实例之前，你需要先创建它。创建一个目录，存放 Vagrant 实例相关的文件，并且将它作为当前工作目录，用下面这条命令：



```
mkdir -p ~/lampbox && cd ~/lampbox

```

在创建镜像实例之前，你需要搞清楚目的，这个镜像实例是一个运行 CentOS 7 基础系统，模板包括 Apache 的 Web 服务，MariaDB（MySQL 原开发者创建的一个流行的开源数据库）数据库和 PHP 服务。


初始化 Vagrant 实例，用 `vagrant init` 命令：



```
vagrant init centos/7

```

这个命令初始化 Vagrant 实例，并创建一个名为 Vagrantfile 的文件，包含一些预先配置的变量。打开并编辑它，下面的命令显示了用于这次配置的基本镜像实例。



```
config.vm.box = "centos/7"

```

现在设置端口转发，以便你配置完毕 Vagrant 实例并让它运行起来之后可以测试它。将下述配置加入到 Vagrantfile 的最终的 `end` 语句之前：



```
config.vm.network "forwarded_port", guest: 80, host: 8080

```

这个命令将 Vagrant 实例 的 80 端口映射为主机的 8080 端口。


下一步是设置 Ansible 作为配置 Vagrant 实例的工具，将下述配置加入到 Vagrantfile 最终的 `end` 语句之前，将 Ansible 作为<ruby> 配置工具 <rp>  （ </rp> <rt>  provisioning provider </rt> <rp>  ） </rp></ruby>：



```
config.vm.provision :ansible do |ansible|
  ansible.playbook = "lamp.yml"
end

```

（必须将这三行在最后的 `end` 语句之前加入）注意 `ansible.playbook = "lamp.yml"` 这一句定义了配置镜像实例的 Ansible playbook 的名字。


### 创建 Ansible playbook


在 Ansible 之中，playbook 是指在你的远端节点执行的策略，换句话说，它管理远端节点的配置和部署。详细的说，playbook 是一个 Yaml 文件，在里面写入你要在远端节点上将要执行的任务。所以，你需要创建一个名为 lamp.yml 的 playbook 来配置镜像实例。


在 Vagrantfile 相同的目录里创建一个 lamp.yml 文件，将下面的内容粘贴到文件当中：



```
---
- hosts: all
  become: yes
  become_user: root
  tasks:
  - name: Install Apache
    yum: name=httpd state=latest
  - name: Install MariaDB
    yum: name=mariadb-server state=latest
  - name: Install PHP5
    yum: name=php state=latest
  - name: Start the Apache server
    service: name=httpd state=started
  - name: Install firewalld
    yum: name=firewalld state=latest
  - name: Start firewalld
    service: name=firewalld state=started
  - name: Open firewall
    command: firewall-cmd --add-service=http --permanent

```

每一行代表的意思：


* `hosts: all` 指定该 playbook 需要在 Ansible 配置文件中定义的所有主机上都执行，因为还没定义主机， playbook 将只在本地运行。
* `sudo: true` 表明该任务需要用 root 权限运行。（LCTT 译注：此语句上述配置中缺失。）
* `tasks:` 指定当 playbook 运行是需要执行的任务，在这一节之下：
	+ `name: ...` 描述任务的名字
	+ `yum: ...` 描述该任务应该由 yum 模块执行，可选的 key=value 键值对将由 yum 模块所使用。


当 playbook 运行时，它会安装最新的 Apache Web 服务（http），MariaDB 和 PHP。当安装完毕并启动防火墙 firewalld，给 Apache 打开一个端口。你可以通过编写 playbook 来完成这些。现在可以配置它了。


### 配置镜像 实例


用 Ansible 配置 Vagrant 实例只需要以下几步了：



```
vagrant up --provider libvirt

```

上面的命令运行 Vagrant 实例，将实例的基础镜像下载到宿主机当中（如果还没下载的话），然后运行 lamp.yml 来进行配置。


如果一切正常，输出应该和下面的例子类似：


![](/data/attachment/album/201610/21/091640ldafzermm4or34k8.png)


这个输出显示镜像实例已经被配置好了，现在检查服务是否可用，在宿主机上打开浏览器，输入 `http://localhost:8080`，记住本地主机的 8080 端口是 Vagrant 实例映射过来的 80 端口。你应该可以看到如下的 Apache 的欢迎界面。


![](/data/attachment/album/201610/21/091642qa5zk5xka6c6gw32.png)


要修改你的 Vagrant 实例，你可以修改 lamp.yml，你能从 Ansible 的[官网](http://docs.ansible.com/ansible/index.html)上找到很多文章。然后运行下面的命令来重新配置：



```
vagrant provision

```

### 总结


现在我们知道怎么用 Ansible 来配置 Vagrant 实例了。这只是一个基本的例子，但是你可以用这些工具来实现不同的例子。比如你可以用所需工具的最新版本来部署一个完整的应用。现在你可以用 Ansible 来配置你自己的远端服务器和容器了。




---


via: <https://fedoramagazine.org/using-ansible-provision-vagrant-boxes/>


作者：[Saurabh Badhwar](http://h4xr.id.fedoraproject.org/) 译者：[jiajia9linuxer](https://github.com/jiajia9linuxer) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Ansible is a great tool for system administrators who want to automate system administration tasks. From configuration management to provisioning and managing containers for application deployments, Ansible [makes it easy](https://ansible.com). The lightweight module based architecture is ideal for system administration. One advantage is that when the node is not being managed by Ansible, no resources are used.

This article covers how to use Ansible to provision [Vagrant boxes](https://www.vagrantup.com/). A Vagrant box in simple terms is a virtual machine prepackaged with tools required to run the development environment. You can use these boxes to distribute the development environment used by other team members for project work. Using Ansible, you can automate provisioning the Vagrant boxes with your development packages.

This tutorial uses Fedora 24 as the host system and [CentOS](https://centos.org) 7 as the Vagrant box.

## Setting up prerequisites

To configure Vagrant boxes using Ansible, you’ll need a few things setup. This tutorial requires you to install Ansible and Vagrant on the host machine. On your host machine, execute the following command to install the tools:

sudo dnf install ansible vagrant vagrant-libvirt

The above command installs both Ansible and Vagrant on your host system, along with Vagrant’s libvirt provider. Vagrant doesn’t provide functionality to host your virtual machine guests (VMs). Rather, it depends on third party providers such as libvirt, VirtualBox, VMWare, etc. to host the VMs. This provider works directly with libvirt and KVM on your Fedora system.

Next, make sure your user is in the *wheel* group. This special group allows you to run system administration commands. If you created your user as an administrator, such as during installation, you’ll have this group membership. Run the following command:

id | grep wheel

If you see output, your user is in the group, and you can move on to the next section. If not, run the following command. You’ll need to provide the password for the *root* account. Substitute your user name for the text *<username>*:

su -c 'usermod -a -G wheel <username>'

Then you will need to logout, and log back in, to inherit the group membership properly.

Now it’s time to create your first Vagrant box, which you’ll then configure using Ansible.

## Setting up the Vagrant box

Before you use Ansible to provision a box, you must create the box. To start, create a new directory which will store files related to the Vagrant box. To create this directory and make it the current working directory, issue the following command:

mkdir -p ~/lampbox && cd ~/lampbox

Before you create the box, you should understand the goal. This box is a simple example that runs CentOS 7 as its base system, along with the Apache web server, MariaDB (the popular open source database server from the original developers of MySQL) and PHP.

To initialize the Vagrant box, use the *vagrant init* command:

vagrant init centos/7

This command initializes the Vagrant box and creates a file named *Vagrantfile,* with some pre-configured variables. Open this file so you can modify it. The following line lists the base box used by this configuration.

config.vm.box = "centos/7"

Now setup port forwarding, so after you finish setup and the Vagrant box is running, you can test the server. To setup port forwarding, add the following line just before the *end* statement in Vagrantfile:

config.vm.network "forwarded_port", guest: 80, host: 8080

This option maps port 80 of the Vagrant Box to port 8080 of the host machine.

The next step is to set Ansible as our provisioning provider for the Vagrant Box. Add the following lines before the *end* statement in your Vagrantfile to set Ansible as the provisioning provider:

config.vm.provision :ansible do |ansible| ansible.playbook = "lamp.yml" end

(You must add all three lines before the final *end* statement.) Notice the statement *ansible.playbook = “lamp.yml”*. This statement defines the name of the playbook used to provision the box.

## Creating the Ansible playbook

In Ansible, playbooks describe a policy to be enforced on your remote nodes. Put another way, playbooks manage configurations and deployments on remote nodes. Technically speaking, a playbook is a YAML file in which you write tasks to perform on remote nodes. In this tutorial, you’ll create a playbook named *lamp.yml* to provision the box.

To make the playbook, create a file named *lamp.yml* in the same directory where your Vagrantfile is located and add the following lines to it:

--- - hosts: all become: yes become_user: root tasks: - name: Install Apache yum: name=httpd state=latest - name: Install MariaDB yum: name=mariadb-server state=latest - name: Install PHP5 yum: name=php state=latest - name: Start the Apache server service: name=httpd state=started - name: Install firewalld yum: name=firewalld state=latest - name: Start firewalld service: name=firewalld state=started - name: Open firewall command: firewall-cmd --add-service=http --permanent

An explanation of each line of *lamp.yml* follows.

*hosts: all*specifies the playbook should run over every host defined in the Ansible configuration. Since no hosts are configured hosts yet, the playbook will run on localhost.*sudo: true*states the tasks should be performed with root privileges.*tasks:*specifies the tasks to perform when the playbook runs. Under the*tasks*section:*name: …*provides a descriptive name to the task*yum: …*specifies the task should be executed by the*yum*module. The options*name*and*state*are*key=value*pairs for use by the*yum*module.


When this playbook executes, it installs the latest versions of the Apache (httpd) web server, MariaDB, and PHP. Then it installs and starts firewalld, and opens a port for the Apache server. You’re now done writing the playbook for the box. Now it’s time to provision it.

## Provisioning the box

A few final steps remain before using the Vagrant Box provisioned using Ansible. To run this provisioning, execute the following command:

vagrant up --provider libvirt

The above command starts the Vagrant box, downloads the base box image to the host system if not already present, and then runs the playbook *lamp.yml* to provision.

If everything works fine, the output looks somewhat similar to this example:

This output shows that the box has been provisioned. Now check whether the server is accessible. To confirm, open your web browser on the host machine and point it to the address *http://localhost:8080. *Remember, port 8080 of the local host is forwarded to port 80 of the Vagrant box. You should be greeted with the Apache welcome page like the one shown below:

To make changes to your Vagrant box, first edit the Ansible playbook *lamp.yml*. You can find plentiful documentation on Ansible at [its official website](http://docs.ansible.com/ansible/index.html). Then run the following command to re-provision the box:

vagrant provision

## Conclusion

You’ve now seen how to use Ansible to provision Vagrant boxes. This was a basic example, but you can use these tools for many other use cases. For example, you can deploy complete applications along with up-to-date version of required tools. Be creative as you use Ansible to provision your remote nodes or containers.


## Cătălin George Feștilă

There are hundred percent sure, but you will not find .box files for Fedora alpha released on Vagrant. It would be very helpful if the training for the development of such virtualization would increase by tutorials us.

## Andy Mender

Very nice article. Makes me want to start spawning VMs instantly ;).

## Cătălin George Feștilă

Will be great if we have also Fedora Alpha released like .box file for Vagrant.

## Jason Scalia

Hello,

Just a note that you could use the firewalld Ansible module instead of using command for it:

http://docs.ansible.com/ansible/firewalld_module.html

## Paul W. Frields

You could, except for the fact that module is not in the version of ansible currently available in CentOS 7. Hopefully it will be updated in the future, but until then, the playbook here will work properly.

## Jason Scalia

Thanks Paul. Ansible is being invoked from the fedora host though isn’t it and not from the centos 7 box? Either way, as you mentioned, it will work as described. I was more concerned from a best practices standpoint.

## johnnybee

Interesting that you use the ‘become’ option which has superseded the ‘sudo’ option in your lamp.yml file:

become: yes

become_user: root

But in your explanation of the lamp.yml file, you explain sudo:

sudo: true

Case of bad copy/paste?

## Frédéric Henri

Since vagrant 1.8.4 (July 2016) you can also use the ansible_local provisioner so you don’t need to install ansible on your host but it will install and run ansible directly on the guest – its very convenient then to share vagrant file to other team members and they dont need to install it

## Frédéric Henri

sorry ansible_local was introduced in vagrant 1.8.0 !