使用Vagrant在CentOS7虚拟机上搭建最新版本的Ansible环境

在 CentOS7 上建立 ansible 环境。

Only need one option :
– 宗旨

    • できるだけ最短で構築したい

 

    自分用備忘録

目标

    ansible 環境を CentOS7 上にとりあえずてっとり早く作りたい方

自然界的一切事物,包括空气、水、土壤、生物等所组成的整体。

    • Mac OS X El Capitan

 

    • VirtualBox

 

    • Vagrant

Official CentOS7 Box (7.2)

参考文献

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

如果有必要的話,做好準備。

0-1. 更新方框

% vagrant box update --box centos/7

0-2. 创建和切换目录

% mkdir -p ~/vagrant/ansible/
% cd ~/vagrant/ansible/

1. 如果需要手动安装的话

$ sudo yum -y update

$ sudo yum -y install git python-setuptools libffi-devel python-devel gcc python-crypto openssl-devel

$ sudo easy_install pip

$ sudo pip install --upgrade setuptools

$ sudo -H pip install paramiko PyYAML Jinja2 httplib2 six
$ sudo pip install ansible
$ git clone git://github.com/ansible/ansible.git --recursive

$ sudo chown -R vagrant: /home/vagrant/ansible

$ echo "source /home/vagrant/ansible/hacking/env-setup > /dev/null" >> /home/vagrant/.bashrc

$ . .bashrc
$ ansible --version
ansible 2.2.0 (devel 70e63ddf6c) last updated 2016/09/15 06:21:30 (GMT +000)
  lib/ansible/modules/core: (detached HEAD 683e5e4d1a) last updated 2016/09/15 06:22:40 (GMT +000)
  lib/ansible/modules/extras: (detached HEAD 8749c40fee) last updated 2016/09/15 06:23:37 (GMT +000)
  config file =
  configured module search path = Default w/o overrides

使用bootstrap.sh自动进行配置处理。

2-1. 准备配置文件

$ cat << EOF > Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.box = "centos/7"
  config.vm.provision :shell, path: "bootstrap.sh"
end
EOF
cat Vagrantfile
$ cat << EOF > bootstrap.sh
#!/usr/bin/env bash

export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

sudo yum -y update

sudo yum -y install git python-setuptools libffi-devel python-devel gcc python-crypto openssl-devel
sudo easy_install pip
sudo pip install --upgrade setuptools
sudo -H pip install paramiko PyYAML Jinja2 httplib2 six
sudo pip install ansible
EOF
cat bootstrap.sh
$ cat << EOF > bootstrap.sh
#!/usr/bin/env bash

export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

sudo yum -y update

sudo yum -y install git python-setuptools libffi-devel python-devel gcc python-crypto openssl-devel
sudo easy_install pip
sudo -H pip install paramiko PyYAML Jinja2 httplib2 six
git clone git://github.com/ansible/ansible.git --recursive
sudo chown -R vagrant: /home/vagrant/ansible
echo "source /home/vagrant/ansible/hacking/env-setup > /dev/null" >> /home/vagrant/.bashrc
EOF
cat bootstrap.sh

2-2. 开启并连接虚拟机

% vagrant up
% vagrant ssh

3. 确认 Ansible 是否正常运行

$ which ansible
~/ansible/bin/ansible

$ ansible --version
ansible 2.2.0 (devel 70e63ddf6c) last updated 2016/09/15 07:21:43 (GMT +000)
  lib/ansible/modules/core: (detached HEAD 683e5e4d1a) last updated 2016/09/15 07:22:56 (GMT +000)
  lib/ansible/modules/extras: (detached HEAD 8749c40fee) last updated 2016/09/15 07:23:49 (GMT +000)
  config file =
  configured module search path = Default w/o overrides

$ ansible all -i "localhost," -c local -m shell -a 'date;echo hello world'
localhost | SUCCESS | rc=0 >>
Thu Sep 15 07:25:32 UTC 2016
hello world

4. 结束

广告
将在 10 秒后关闭
bannerAds