让我们使用Ansible快速检查管理服务器的状态吧

首先

kobito.1437097118.116827.png

用Ansible来管理多个服务器确实是一项繁琐的工作,但也很简单。
通过这个工具,我们可以一键确认目标服务器的状态,并在需要的情况下进行更新。
不管是一台服务器,还是十台或者一百台,都可以通过一个命令完成。

之前的准备工作

    MacにAnsibleインストール

参考文献:在 Mac OS X 上使用 Homebrew 安装 Ansible 的步骤。

设置 (SHE ZHI)

创建并切换到工作目录。

mkdir /usr/local/ansible
cd /usr/local/ansible

在当前目录下创建名为”hosts”的文件。

vi hosts
[bastion]
bastion ansible_connection=ssh

[dev]
dev ansible_connection=ssh

[stg]
stg-web-01 ansible_connection=ssh
stg-web-02 ansible_connection=ssh

请确保重要的一点,即正在参考~/.ssh/config文件!

vi ~/.ssh/config
Host bastion
 Hostname 192.168.1.100
 IdentityFile ~/.ssh/id_rsa
 User monlab
 TCPKeepAlive yes
 IdentitiesOnly yes
 ServerAliveInterval 60

Host dev
 Hostname 10.0.0.10
 IdentityFile ~/.ssh/id_rsa
 User monlab
 ProxyCommand ssh bastion -W %h:%p

Host stg-web-01
 Hostname 10.0.0.20
 IdentityFile ~/.ssh/id_rsa
 User monlab
 ProxyCommand ssh bastion -W %h:%p

Host stg-web-02
 Hostname 10.0.0.30
 IdentityFile ~/.ssh/id_rsa
 User monlab
 ProxyCommand ssh bastion -W %h:%p

首先尝试进行ping测试。

针对所有服务器执行。

ansible -i hosts all -m ping

只在dev上执行

ansible -i hosts dev -m ping

尝试确认openssl的版本

对于每台服务器执行。

ansible -i hosts all -m command -a "yum list openssl"

只在stg上执行

ansible -i hosts stg -m command -a "yum list openssl"

最后

非常简单呢。试着尝试更多不同的模式,如果整理好了再写在Qiita上。

先从这种感觉开始吧。

请参考下面的链接

在 Ansible 中,描述 ssh 连接信息的方式。

广告
将在 10 秒后关闭
bannerAds