三宝祭和ansible

今天我期待已久的本地的“秋刀魚節”活動开始了。

因为下雨,因为台风,很遗憾地取消了。

1000条秋刀鱼被大火烤熟的盛会,对于秋刀鱼来说真是一场令人恐惧的节日。

因为孩子在下雨天被迫做公文,所以我也无法开电视,于是尝试了一下ansible。
在那样的一个星期天早晨;

首先,在两台CentOS 6.5虚拟机上创建测试服务器。

由于Ansible的推送方式,可以在服务器上配置密钥,实现服务器直接与客户端进行双向通信。

由于貌似可以使用yum从EPEL安装,所以可以快速方便地使用它。


rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/epel.repo
yum --enablerepo=epel update epel-release
yum --enablerepo=epel install ansible

版本似乎是最新的1.7版本。


[root@ansiblesvr ansible]# ansible --version
ansible 1.7


[root@ansiblesvr ansible]# python --version
Python 2.6.6

●试着进行ping测试

由于没有问题,所以尝试对连接畅通的节点进行ping测试。
嗯,我忘了写配置。
Ansible服务器
Ansible客户端
就像名字所示的。

据说可以从ansiblesvr执行以下命令进行ping,但是失败了。


[root@ansiblesvr ansible]# ansible ansibletest -m ping
No hosts matched

需要对主机进行”清单文件”的定义。

这看起来像一个模板。


[root@ansiblesvr ansible]# head -25 /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

green.example.com
blue.example.com
192.168.100.1
192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

[webservers]
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110

这次我只会写下需要的东西来进行尝试。


[root@ansiblesvr ansible]# cat /etc/ansible/hosts
[testwebserver]
ansibleclient
192.168.1.2


[root@ansiblesvr ansible]# ansible ansibleclient -m ping
ansibleclient | success >> {
"changed": false,
"ping": "pong"
}

乒乒乓乓!好的,就这样!

任何命令都可以


[root@ansiblesvr ansible]# ansible ansibleclient -a 'cat /etc/redhat-release'
ansibleclient | success | rc=0 >>
CentOS release 6.5 (Final)

 

▲学习:可将目标主机写入清单文件(/etc/ansible/hosts)中

似乎需要一本名为“play book”的东西,但我是一个试错派,所以试了一次,结果成功安装了Apache哈哈。看来play book里已经有Apache了。


[root@ansiblesvr ansible]# ansible ansibleclient -m yum -s -a name=httpd
ansibleclient | success >> {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: www.ftp.ne.jp\n * extras: www.ftp.ne.jp\n * updates: ftp.tsukuba.wide.ad.jp\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-31.el6.centos will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.2.15-31.el6.centos updates 824 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package(s)\n\nTotal download size: 824 k\nInstalled size: 2.9 M\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Installing : httpd-2.2.15-31.el6.centos.x86_64 1/1 \n\r Verifying : httpd-2.2.15-31.el6.centos.x86_64 1/1 \n\nInstalled:\n httpd.x86_64 0:2.2.15-31.el6.centos \n\nComplete!\n"
]
}

[root@ansibleclient ~]# /etc/init.d/httpd status

Apache 服务已经停止。


[root@ansibleclient ~]# tail /var/log/yum.log
Aug 31 16:25:04 Installed: ack-2.12-2.el6.noarch
Aug 31 16:29:51 Installed: python-psutil-0.6.1-1.el6.x86_64
Aug 31 16:29:51 Installed: python-setuptools-0.6.10-3.el6.noarch
Aug 31 16:29:51 Installed: glances-1.7.4-1.el6.noarch
Oct 05 11:05:42 Installed: yum-utils-1.1.30-17.el6_5.noarch
Oct 05 11:05:46 Installed: httpd-2.2.15-31.el6.centos.x86_64

还是序章,但到这里还没什么进展。
之后我会在play book上写点东西,试试看感觉如何。
妈妈叫我去购物,我就先去了><
结果只能玩了一个小时左右。下次继续。

●获取主机信息

测试ansible -m setup

bannerAds