天天看點

在 CentOS 7 中安裝并使用自動化工具 Ansible

ansible是一款為類unix系統開發的自由開源的配置和自動化工具。它用python寫成,類似于chef和puppet,但是有一個不同和優點是我們不需要在節點中安裝任何用戶端。它使用ssh來和節點進行通信。

在 CentOS 7 中安裝并使用自動化工具 Ansible

本篇中我們将在centos 7上安裝并配置ansible,并且嘗試管理兩個節點。

ansible 服務端 – ansible.linuxtechi.com ( 192.168.1.15 )

節點 – 192.168.1.9 , 192.168.1.10

<a target="_blank"></a>

ansible倉庫預設不在yum倉庫中,是以我們需要使用下面的指令啟用epel倉庫。

<code>[root@ansible ~]# rpm -iuvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm</code>

<code>[root@ansible ~]# yum install ansible</code>

安裝完成後,檢查ansible版本:

<code>[root@ansible ~]# ansible --version</code>

在 CentOS 7 中安裝并使用自動化工具 Ansible

ansible-version

在ansible服務端生成密鑰,并且複制公鑰到節點中。

<code>root@ansible ~]# ssh-keygen</code>

在 CentOS 7 中安裝并使用自動化工具 Ansible

ssh-keygen

使用ssh-copy-id指令來複制ansible公鑰到節點中。

在 CentOS 7 中安裝并使用自動化工具 Ansible

ssh-copy-id-command

檔案 <code>/etc/ansible/hosts</code> 維護着ansible中伺服器的清單。

<code>[root@ansible ~]# vi /etc/ansible/hosts</code>

<code>[test-servers]</code>

<code>192.168.1.9</code>

<code>192.168.1.10</code>

儲存并退出檔案。

主機檔案示例如下:

在 CentOS 7 中安裝并使用自動化工具 Ansible

ansible-host

使用ping檢查‘test-servers’或者ansible節點的連通性。

<code>[root@ansible ~]# ansible -m ping 'test-servers'</code>

在 CentOS 7 中安裝并使用自動化工具 Ansible

ansible-ping

例子1:檢查ansible節點的運作時間(uptime)

<code>[root@ansible ~]# ansible -m command -a "uptime" 'test-servers'</code>

在 CentOS 7 中安裝并使用自動化工具 Ansible

ansible-uptime

例子2:檢查節點的核心版本

<code>[root@ansible ~]# ansible -m command -a "uname -r" 'test-servers'</code>

在 CentOS 7 中安裝并使用自動化工具 Ansible

kernel-version-ansible

例子3:給節點增加使用者

<code>[root@ansible ~]# ansible -m command -a "useradd mark" 'test-servers'</code>

<code>[root@ansible ~]# ansible -m command -a "grep mark /etc/passwd" 'test-servers'</code>

在 CentOS 7 中安裝并使用自動化工具 Ansible

useradd-ansible

例子4:重定向輸出到檔案中

<code>[root@ansible ~]# ansible -m command -a "df -th" 'test-servers' &gt; /tmp/command-output.txt</code>

在 CentOS 7 中安裝并使用自動化工具 Ansible

本文來自雲栖社群合作夥伴“linux中國”,原文釋出日期:2015-10-04

繼續閱讀