Ansible快速上手

集中式的系統配置工具市面上有不少,比如SaltStack、Puppet、Cfengine、Ansible等。相比較其他三種配置工具,Ansible安裝方便,使用起來容易上手,而且它的語法也簡單易懂。

最近有個專案,給雲主機部署zabbit-agent,因數量比較大,選用了Ansible的部署方式。借這個機會,把Ansible的簡單使用方法介紹下。

一,安裝

Ansible不需要給目標主機安裝Agent,所以安裝比較簡單,直接在管理主機上操作(管理主機是中心配置伺服器,

配置對被管主機免密登入

,方便後續操作)

sudo yum install epel-releasesudo yum install ansible

(筆者使用的伺服器作業系統都是CentOS 7)

二,配置

安裝完成後,ansible的預設配置檔案放在目錄“/etc/ansible”下,其中ansible自身的配置檔案為“/etc/ansible/ansible。cfg”,該目錄底下的檔案可以保持不動。因為在生產環境中,一般分專案進行,比如批次安裝zabbix-agent這個專案,在家目錄下建立“zabbix-agent”目錄,結構如下:

templates\ #存放模板檔案,會透過jinja2渲染成具體的配置檔案 zabbix_agentd。conf。j2vars\ #存放模板檔案或playbook中使用的變數檔案 centos。yamlansible。cfg #ansible 配置檔案,優先順序比/etc/ansible/ansible。cfg高hosts #inventory檔案,配置要操作的目標主機install-zabbix-agent。yaml #playbook檔案,ansible按照該檔案來執行task

後面操作都以目錄“zabbix-agent”作為上下文,

ansible。cfg

[defaults]host_key_checking = Falseinventory = hosts #指示inventory檔案

hosts

[aliyun]p2 ansible_ssh_host=p2。example。com ansible_ssh_user=root ansible_ssh_port=22[tencentyun]p3 ansible_ssh_host=p3。example。com ansible_ssh_user=root ansible_ssh_port=22

1、3行定義主機組,2、4行定義組中的主機,第二行第一列是主機的別名,其他都是自解釋的,有些內容可以省略。

有這兩個檔案,就可以執行ansible的adhoc命令,如下:

[root@p1 zabbix-agent]# ansible p2,p3 -m command -a ‘hostname’p2 | CHANGED | rc=0 >>p2。example。comp3 | CHANGED | rc=0 >>p3。example。com

adhoc命令執行單個task,比如需要檢視若干臺伺服器的IP或主機名,用起來非常方便。

三,實際使用

現在以一個具體的專案為例:給幾百臺伺服器安裝zabbix-agent。首先需要定義操作的物件,就是哪些伺服器需要安裝zabbix-agent,修改hosts檔案如下:

[web]web-1 ansible_ssh_host=web-1。example。com ansible_ssh_user=root……web-n ansible_ssh_host=web-n。example。com ansible_ssh_user=root[db]db-1 ansible_ssh_host=db-1。example。com ansible_ssh_user=root……db-n ansible_ssh_host=db-n。example。com ansible_ssh_user=root[other-servers]……

定義“install-zabbix-agent。yaml”,這個檔案是核心,其他檔案都是服務於這個檔案的,內容如下:

—— name: install zabbix-agent hosts: all vars_files: - vars/centos。yaml tasks: - name: Install Zabbix repo package yum: name: “{{ zabbix_repo_url }}” state: present - name: Install zabbix-agent yum: name: zabbix-agent state: present - name: Remove zabbix-agent original config file file: path: /etc/zabbix/zabbix_agentd。conf state: absent - name: copy file template: > alt="Ansible快速上手" data-isLoading="0" src="/static/img/blank.gif" data-src=templates/zabbix_agentd。conf。j2 dest=/etc/zabbix/zabbix_agentd。conf - name: Restart zabbix-agent service ansible。builtin。service: name: zabbix-agent state: restarted enabled: yes

ansible playbook檔案的語法非常簡單,就算你是第一次使用,僅根據“install-zabbix-agent。yaml”檔案的內容也可以猜個大概。

在檔案“install-zabbix-agent。yaml”中使用了變數,其定義在“vars/centos。yaml”中,

zabbix_repo_url: https://repo。zabbix。com/zabbix/5。0/rhel/7/x86_64/zabbix-release-5。0-1。el7。noarch。rpm

playbook檔案中,使用鍵“vars_files”來定義變數檔案,這樣ansible-playbook就知道在哪裡找到變數的值。

安裝軟體,免不了要修改配置檔案,這裡直接刪掉原來的配置檔案,透過jinja2引擎渲染本地的模板檔案“templates/zabbix_agentd。conf。j2”,然後複製到目標伺服器。

templates/zabbix_agentd。conf。j2

PidFile=/var/run/zabbix/zabbix_agentd。pidLogFile=/var/log/zabbix/zabbix_agentd。logLogFileSize=0Server=192。168。9。128ServerActive=192。168。9。128Hostname={{ ansible_hostname }}Include=/etc/zabbix/zabbix_agentd。d/*。conf

注意在模板檔案中,有一個特殊變數“ansible_hostname”,它是ansible內建變數。在執行playbook時,ansible預設會收集目標伺服器的一些屬性比如IP、主機名、作業系統等資訊,這些資訊ansible稱其為facts,變數“ansible_hostname”會在收集facts後自動賦值。

執行命令“ansible-playbook install-zabbix-agent。yaml”即可完成“zabbix-agent”的批次部署,短短30行程式碼,就可以完成幾百臺伺服器的軟體安裝。

總結

Ansible有很多優點:配置管理指令碼playbook基於yaml,比較容易理解;被管理主機不用安裝agent;使用推送的工作模式,這種模式最大的優點就是釋出者可以控制變更在伺服器的生效時間;基於模組的擴充套件方式,Ansible有很多內建的模組,也可以安裝第三方模組或者乾脆自己書寫模組都是支援的。

希望這篇文章能幫到正在努力的你,歡迎點贊評論!