自動化運維工具Ansible的用法

一、Ansible簡介

Ansible是新出現的自動化運維工具,基於Python開發,集合了衆多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。ansible是基於模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運行的模塊,ansible只是提供一種框架。

2015年RedHat收購了Ansible,並且將其多個平臺自動化部署方案切換到Ansible。
Ansible官網
https://www.ansible.com/
Ansible幫助文檔:http://docs.ansible.com/
RedHat 中國的Ansible支持頁面:https://www.redhat.com/zh/technologies/management/ansible

Ansible的優勢及特性
Ansible作爲一個自動化運維工具,優勢總共有一下幾點:

* 輕量級:無需在客戶端安裝 **agent** ,並且也不需要啓動服務。
* 配置靈活:Ansible基於SSH工作,可使用系統或自定義模塊滿足更爲靈活的需求。
* 語法簡潔:配置語言採用 **yaml** ,用來定義多條任務,語法更爲簡潔,Ansible簡單易懂的自動化語言允許使用人員在很短的時間內完成自動化項目的部署。

二、Ansible的基本結構

* Ansible的核心管理主機:亦可以稱爲 **管理節點** ,用來管理其他的受控節點。
* 系統模塊:Ansible本身自帶一些模塊,提供一些常用的功能。
* 擴展模塊:若Ansible的系統模塊無法滿足批量化管理的特定需求,則可以添加一些擴展模塊。
* 插件:完成模塊功能的補充。
* 主機組:用來定義實際管理維護的主機服務器。
* 劇本(playbooks):用來定義多條任務,由Ansible統一去執行。
下載地址:
http://releases.ansible.com/https://github.com/ansible/

下載最新版軟件包
安裝:
[root@servera ~]# yum -y localinstall ansible-1.9.2-1.el7.noarch ansible-inventory-grapher-1.0.1-2.el7.noarch ansible-lint-2.0.1-1.el7.noarch


三、配置Ansible

1) ssh的基本配置
Ansible是基於ssh來實現批量化的配置,Ansible1.2.1及其之後的版本都會默認啓用公鑰認證。於是我們可以先來配置下ssh基於公鑰的認證。
servera依舊作爲管理節點,serverb作爲受控節點。在這裏我們使用密鑰方式來保證servera和serverb的連接。

              ---> serverb
servera
              ---> serverc
[root@servera ~]# ssh-keygen
[root@servera ~]# ssh-copy-id [email protected]
[root@servera ~]# ssh-copy-id [email protected]

* ansible的配置,配置主目錄/etc/ansible
ansible.cfg爲主配置文件。hosts定義了主機組相關的內容。
[root@servera ~]# cd /etc/ansible/
[root@servera ansible]# vim /etc/ansible/ansible.cfg
private_key_file = /root/.ssh/id_rsa

2)定義inventory文件(定義主機組)

[root@servera ansible]# vim hosts 
[webserver]
node1
node2
[root@servera ansible]# vim /etc/hosts
172.25.1.11 node1
172.25.1.12 node2

測試(可以通過以下指令做簡單的測試,具體操作後續分析):
[root@servera ansible]# ansible webserver -m ping
node2 | success >> {
    "changed": false, 
    "ping": "pong"
}
node1 | success >> {
    "changed": false, 
    "ping": "pong"
}
[root@servera ansible]# ansible webserver -m command -a "uptime"
node1 | success | rc=0 >>
 10:25:59 up 12 min,  2 users,  load average: 0.00, 0.01, 0.01

node2 | success | rc=0 >>
 10:25:59 up 12 min,  2 users,  load average: 0.08, 0.03, 0.03


有些時候我們可能會遇到一個問題,如果有個主機重新安裝並在“known_hosts”中有了不同的key值記錄,這會提示一個錯誤信息直到被糾正爲止。如果有個主機沒有在“known_hosts”中被初始化將會導致在交互使用Ansible或定時執行Ansible時對key信息的確認提示。如果你想禁用此項行爲並明白其含義,你能夠通過編輯 /etc/ansible/ansible.cfg來實現。
[root@servera ansible]# ls
ansible.cfg  hosts  roles
[root@servera ansible]# vim ansible.cfg
[defaults]
host_key_checking = False
[root@servera ansible]# pwd
/etc/ansible

四、ansible命令語法介紹

ansible HOST-PATTERN [-f FORKS] [-M MOD_NAME] [-a MOD_ARGS]
-f FORKS:表示一批處理幾臺主機,也就是當被管控主機很多時,ansible不是對所有主機同時發起管理操作,而是一批處理幾臺,然後再換一批,直到所有主機被處理完成,如果不指定,則默認是5臺
-m MOD_NAME:指明調用哪個模塊執行操作,各個模塊所能實現的功能不同,如果不指定,默認是用-m command模塊
-a MOD_ARGS:指明使用該模塊的執行操作時的參數

列出所有模塊:
[root@ansiable ~]# ansible-doc -l
[root@ansiable ~]# ansible-doc yum

五、常用Ansible模塊
Ansible可以通過模塊的方式來完成一些原理的管理工作,可以通過ansible-doc -l查看到所有自帶的模塊。 ansible-doc -s 模塊名 可以用來查看具體模塊對應的用法。

1.ping模塊 探測助主機是否在線
[root@ansiable ~]# ansible webserver -m ping

2.command模塊 在遠程主機執行名,不支持管道,重定向等shell特性
[root@ansiable ~]# ansible webserver -m command -a "uptime"
--無效命令:
[root@ansiable ~]# ansible webserver -m command -a "echo redhat | passwd --stdin root"
常用參數:
chdir=   表示指明命令在遠程主機上哪個目錄下運行,也就是在命令執行前切換到哪個目錄下
creates=   在命令運行時創建一個文件,如果文件已存在,則不會執行創建任務
removes=   在命令運行時移除一個文件,如果文件不存在,則不會執行移除任務
executeble=   指明運行命令的shell程序
[root@ansiable ~]# ansible webserver -m command -a 'chdir=/tmp ls ./'
node1 | success | rc=0 >>
zabbix_server.log
node2 | success | rc=0 >>
systemd-private-gFvMoV

不支持管道和重定向:
[root@ansiable ~]# ansible webserver -m command -a 'echo  "hello" > /tmp/aa.txt'

判斷是否能夠新建aa.txt(不會新建文件)

[root@serverb tmp]# touch aa.txt
[root@ansiable ~]# ansible webserver -m command -a 'chdir=/tmp ls ./ creates=aa.txt'
node2 | success | rc=0 >>
systemd-private-gFvMoV

node1 | success | rc=0 >>
skipped, since aa.txt exists
[root@ansiable ~]# ansible webserver -m command -a 'chdir=/tmp ls ./ removes=aa.txt'


3.shell模塊
在遠程主機執行命令,相當於調用遠程主機的shell進程,然後在該shell下打開一個子shell運行命令
支持shell特性,如管道,重定向等

[root@ansiable ~]# ansible webserver -m shell -a "echo hello > /tmp/aa.txt"
[root@ansiable ~]# ansible webserver -m shell -a "cat /tmp/aa.txt"
node2 | success | rc=0 >>
hello
node1 | success | rc=0 >>
hello


4.copy模塊
拷貝ansible管理端的文件到遠程主機的指定位置
常見參數有:
        dest=   指明拷貝文件的目標目錄位置,使用絕對路徑,如果源是目錄,則目標也要是目錄,如果目標文件已存在,會覆蓋原有內容
        src=   指明本地路徑下的某個文件,可以使用相對路徑和絕對路徑,支持直接指定目錄,如果源是目錄,則目標也要是目錄
        mode=   指明覆制時,目標文件的權限
        owner=   指明覆制時,目標文件的屬主
        group=   指明覆制時,目標文件的屬組
        content=  指明覆制到目標主機上的內容,不能與src一起使用,相當於複製content指明的數據,到目標文件中

[root@ansiable ~]# ansible webserver -m copy -a "src=/root/anaconda-ks.cfg dest=/tmp/ks.cfg mode=600 owner=root group=nobody"
[root@ansiable ~]# ansible webserver -m command -a 'chdir=/tmp ls -l ./ks.cfg'
node1 | success | rc=0 >>
-rw-------. 1 root nobody 9462 Oct 17 11:08 ./ks.cfg
node2 | success | rc=0 >>
-rw-------. 1 root nobody 9462 Oct 17 11:08 ./ks.cfg


記住: 不能與src一起使用
[root@ansiable ~]# ansible webserver -m copy -a 'content="hello hugo" dest=/tmp/abc.txt'

5.cron模塊
管理計劃任務的模塊
        常見參數有:
        minute=  指明計劃任務的分鐘,支持格式:0-59,*,*/2等,與正常cron任務定義的一樣的語法,省略時,默認爲*,也就是每分鐘都執行
        hour=  指明計劃任務的小時,支持的語法:0-23,*,*/2等,省略時,默認爲*,也就是每小時都執行
        day=  指明計劃任務的天,支持的語法:1-31,*,*/2等,省略時,默認爲*,也就是每天都執行
        month=  指明計劃任務的月,支持的語法爲:1-12,*,*/2等,省略時,默認爲*,也就是每月都執行
        weekday=  指明計劃任務的星期幾,支持的語法爲:0-6,*等,省略時,默認爲*,也就是每星期幾都執行
        reboot  指明計劃任務執行的時間爲每次重啓之後
      name=   給該計劃任務取個名稱,必須要給明。每個任務的名稱不能一樣。刪除任務時,只需要給明任務的名稱即可
        job=  執行的任務是什麼,當state=present時纔有意義
        state=present|absent   表示這個任務是創建還是刪除,present表示創建,absent表示刪除,默認是present

注意: 給該計劃任務取個名稱,必須要給明

[root@ansiable ~]# ansible webserver -m 
cron -a 'minute=*/5 hour=* day=17 month=10 weekday=* name="test crontab"
 job="touch /opt/test.out" state=present'
[root@ansiable ~]# ansible webserver -m shell -a "crontab -l"
node1 | success | rc=0 >>
#Ansible: test crontab
*/5 * 17 10 * touch /opt/test.out
node2 | success | rc=0 >>
#Ansible: test crontab
*/5 * 17 10 * touch /opt/test.out


刪除

[root@ansiable ~]# ansible webserver -m cron -a 'name="test crontab" state=absent'
node2 | success >> {
    "changed": true, 
    "jobs": []
}
node1 | success >> {
    "changed": true, 
    "jobs": []
}
[root@ansiable ~]# ansible webserver -m shell -a "crontab -l"
node1 | success | rc=0 >>

node2 | success | rc=0 >>


6.fetch模塊
從遠程主機拉取文件到本地
        一般情況下,只會從一個遠程節點拉取數據
        常見參數有:
        dest=  從遠程主機上拉取的文件存放在本地的位置,一般只能是目錄
        src=   指明遠程主機上要拉取的文件,只能是文件,不能是目錄
[root@ansiable ~]# ansible node1 -m fetch -a "src=/tmp/a1.txt dest=/opt/"
[root@ansiable ~]# find /opt/node1/
/opt/node1/
/opt/node1/tmp
/opt/node1/tmp/a1.txt

7.file模塊
用於設定遠程主機上的文件屬性
        常見參數有:
        path=   指明對哪個文件修改其屬性
        src=   指明path=指明的文件是軟鏈接文件,其對應的源文件是誰,必須要在state=link時纔有用
        state=directory|link|absent   表示創建的文件是目錄還是軟鏈接
        owner=   指明文件的屬主
        group=   指明文件的屬組
        mode=   指明文件的權限

        創建軟鏈接的用法:
            src=  path=  state=link
        修改文件屬性的用法:
            path=  owner=  mode=  group=
        創建目錄的用法:
            path=  state=directory
        刪除文件:
            path= state=absent

新建軟連接:
[root@ansiable ~]#  ansible webserver -m file -a 'src=/tmp/aa.txt path=/home/test.link state=link'
刪除:
[root@ansiable ~]#  ansible webserver -m file -a 'path=/home/test.link state=absent'
修改文件屬性:
[root@ansiable ~]# ansible webserver -m file -a 'path=/tmp/aa.txt owner=nobody group=nobody mode=000'
創建目錄:
[root@ansiable ~]# ansible webserver -m file -a 'path=/tmp/dir state=directory'


8.hostname模塊
       管理遠程主機上的主機名
       常用參數有
       name=  指明主機名

[root@ansiable ~]# ansible node1 -m hostname -a "name=node1.ansible.com"
node1 | success >> {
    "changed": true, 
    "name": "node1.ansiable.com"
}
[root@ansiable ~]# ansible node2 -m hostname -a "name=node2.ansible.com"
node2 | success >> {
    "changed": true, 
    "name": "node2.ansiable.com"
}


9.yum模塊
基於yum機制,對遠程主機管理程序包
        常用參數有:
        name=   指明程序包的名稱,可以帶上版本號,不指明版本,就是默認最新版本。
        state=present|latest|absent   指明對程序包執行的操作,present表示安裝程序包,latest表示安裝最新版本的程序包,absent表示卸載程序包
        disablerepo=    在用yum安裝時,臨時禁用某個倉庫,倉庫的ID
        enablerepo=    在用yum安裝時,臨時啓用某個倉庫,倉庫的ID
        conf_file=   指明yum運行時採用哪個配置文件,而不是使用默認的配置文件
        diable_gpg_check=yes|no  是否啓用gpg-check
注意區別:
[root@ansiable ~]# ansible webserver -m yum -a "name=nmap disable_gpg_check=yes disablerepo=yum  state=present"   #yum指向配置文件裏的標籤[yum]
[root@ansiable ~]# ansible webserver -m yum -a "name=nmap disable_gpg_check=yes   state=present"
[root@ansiable ~]# ansible webserver -m yum -a "name=nmap disable_gpg_check=yes   state=absent"

10.service模塊
用來管理遠程主機上的服務的模塊
        常見參數有:
        name=   被管理的服務名稱
        state=started|stopped|restarted   表示啓動或關閉或重啓
        enabled=yes|no  表示要不要設定該服務開機自啓動
        runlevel=   如果設定了enabled開機自動啓動,則要定義在哪些運行級別下自動啓動

[root@ansiable ~]# ansible webserver -m shell -a "yum -y install vsftpd"
[root@ansiable ~]# ansible webserver -m service  -a "name=vsftpd state=started"
node2 | success >> {
    "changed": true, 
    "name": "vsftpd", 
    "state": "started"
}

node1 | success >> {
    "changed": true, 
    "name": "vsftpd", 
    "state": "started"
}
[root@ansiable ~]# ansible webserver -m service  -a "name=vsftpd enabled=yes"


11. uri模塊
        如果遠端是web服務器,可以利用ansible直接請求某個網頁
        常見參數有:
        url=  指明請求的url的路徑,如:
http://10.1.32.68/test.jpg
        user=  如果請求的url需要認證,則認證的用戶名是什麼
        password=  如果請求的url需要認證,則認證的密碼是什麼
        method=  指明請求的方法,如GET、POST…
        body=   指明報文中實體部分的內容,一般是POST方法或PUT方法時用到
        HEADER_   自定義請求報文中的添加的首部
注意: 安裝模塊
[root@node1 ~]# yum localinstall python-
httplib2-0.7.7-3.el7.noarch.rpm
[root@ansiable pkg]#  ansible webserver -m uri -a 'url=http://172.25.1.11/test.html'

12 模塊 group
13 模塊 user

[root@ansiable ~]# ansible webserver -m group -a "name=tomcat gid=888 state=present"
[root@ansiable ~]# ansible webserver -m shell -a "tail -1 /etc/group"
node2 | success | rc=0 >>
tomcat:x:888:
node1 | success | rc=0 >>
tomcat:x:888:
[root@ansiable
 ~]# ansible webserver -m user -a "name=tomcat uid=888 group=tomcat 
comment="apache-tomcat" shell=/bin/bash state=present"
--直接覆蓋修改:
[root@ansiable
 ~]# ansible webserver -m user -a "name=tomcat uid=888 group=tomcat 
comment="apache-tomcat" shell=/sbin/nologin state=present"
[root@ansiable ~]# ansible webserver -m group -a "name=testgroup gid=2000 state=present"
 --設置明文密碼:
[root@ansiable
 ~]# ansible webserver -m user -a 'name=testuser group=testgroup 
uid=2000 home=/home/testuser shell=/sbin/nologin system=yes 
comment=testtest password=123'
[root@ansiable ~]# ansible webserver 
-m user -a 'name=testuser group=testgroup uid=2000 home=/home/testuser 
shell=/sbin/nologin system=yes comment=testtest 
password=$1$ty46Y/$YZBMaOM9qGUkETZtA/M4e1'


14 模塊: script
[root@ansiable ~]# ansible webserver -m script -a "/root/test.sh"

15 模塊: setup  --獲取主機信息(變量)
[root@ansiable ~]# ansible webserver -m setup

16 模塊: template
模板
[root@ansiable ~]# vim temp.txt
my hostname ` ansible_hostname `

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章