首页 > 代码库 > 试用ansible

试用ansible

环境

  • Windows8
  • Virtual box
  • vagrant

试用 ansible

安装虚拟机

  • vagrant box add chef/centos-6.5
  • vagrant init chef/centos-6.5

修改Vagrantfile

config.vm.define :node1 do |node|
node.vm.box = “chef/centos-6.5”
node.vm.network :forwarded_port, guest: 22, host: 2001, id: “ssh”
node.vm.network :private_network, ip: “192.168.33.11”
end

config.vm.define :node2 do |node|
node.vm.box = “chef/centos-6.5”
node.vm.network :forwarded_port, guest: 22 , host: 2002, id: “ssh”
node.vm.network :forwarded_port, guest: 80, host: 8000, id: “http”
node.vm.network :private_network, ip: “192.168.33.12”
end

启动虚拟机

% vagrant up
% vagrant ssh node1
Last login: Mon Jun 30 06:23:19 2014 from 10.0.2.2
[vagrant@vagrant-centos65 ~]$

添加 SSH 访问 Node1到Node2

% vagrant ssh-config node1 > ssh_config
% vagrant ssh-config node2 >> ssh_config
% scp -F ssh_config ~/.vagrant.d/insecure_private_key node1:.ssh/id_rsa

安装ansible

% vagrant ssh node1
[vagrant@vagrant-centos65 ~]$ sudo yum install ansible -y
这里需要你自行百度了。

添加测试服务器

新建一个hosts然后添加下面的代码
[test-server]
192.168.33.12

执行Ansible命令

$ ansible test-server -i hosts -m ping

192.168.33.12 | success ? {
“changed”: false,
“ping”: “pong”
}

执行任意命令

$ ansible test-server -i hosts -a "cat /etc/redhat-release"

192.168.33.12 | success | rc=0 ? {
CentOS release 6.5 (Final)
}

PlayBook

新建一个simple.yml


  • hosts: test-server
    sudo: yes
    tasks:

  • name: be sure httpd is installed
    yum: name=httpd state=installed

  • name: be sure httpd is running and enabled
    service: name=httpd state=running enabled=yes

检查Playbook

$ ansible-playbook -i hosts simple.yml --list-tasks

运行Playbook

$ ansible-playbook -i hosts simple.yml

确认是否成功

% vagrant ssh node2
[vagrant@vagrant-centos65 ~]$ sudo service httpd status
httpd (pid 8552) is running...

全部了,比较简单,很多地方需要大家自行百度。
有兴趣可以研究一下。
最后吐槽一下,OSC这么流弊的码畜网站,为毛没有支持Github的markdown标准呢。。。
写起来好麻烦。

试用ansible