Ansible快速瞭解

前言

本文依據於課堂《ansible入門到精通》,旨在快速瞭解ansible。文章篇幅較長,可通過目錄快速進入需要了解的部分。

目錄

 

是什麼?

工作原理?

部署?

常用模塊?

playbook?

roles


是什麼?

ansible是自動化運維工具,實現了批量系統配置、批量程序部署、批量運行命令等功能,從而提升運維的管理能力,解決工作中的管理難題。

工作原理?

運維自動化平臺是由管理機器和業務機器組成的。管理機器:任務定製及發佈;業務機器:接收任務並執行任務。

  • 用戶登錄管理機器:通過ansible劇本或單行命令針對業務機器組或者單個機器部署任務;
  • 管理機器讀取用戶的部署任務:根據自己hosts文件中定義的業務機器組查找對應的機器地址(ip或域名)
  • 管理機下發任務:管理機通過SSH免密連接業務機器,下發任務給業務機器;
  • 業務機器執行任務
  • 業務機器將執行結果發送給ansible管理機器

部署?

  • 服務器

主機名稱

ip地址

test-1.wn.com(管理機器)

192.168.52.21

test-2.wn.com(業務機器)

192.168.52.22

test-3.wn.com(業務機器)

192.168.52.23

  • 主機名互相綁定

    cat /etc/hosts

  • 關閉防火牆

   systemctl stop firewalld

   systemctl disable firewalld

   reboot

   systemctl status firewalld

  • 時間同步
  • ssh免密(實現管理機器可以免密登錄業務機器)

    ssh-keygen

    ssh-copy-id -i .ssh/id_rsa.pub [email protected]

    ssh [email protected] #測試是否可以免密登錄

    ssh-copy-id -i .ssh/id_rsa.pub [email protected]

    ssh [email protected] #測試是否可以免密登錄

  • 管理機器(192.168.52.21)安裝ansible並測試

    wget https://releases.ansible.com/ansible/ansible-2.9.0rc3.tar.gz

    tar xf ansible-2.9.0rc3.tar.gz

    mv ansible-2.9.0rc3 /opt/ansible

    cd /opt/ansible/

    pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/  #安裝依賴

    pip install --user ansible -i https://pypi.tuna.tsinghua.edu.cn/simple/  #安裝軟件

    ln -s /opt/ansible/bin/* /usr/bin/

    mkdir /etc/ansible

    cp /opt/ansible/examples/ansible.cfg /etc/ansible/

    部署主機列表,定義被監控機:

    執行ansible的時候會去讀取客戶端文件hosts,如果沒有把客戶端加入到hosts文件中,就說明無法被ansible管理。此hosts不等同於/etc/hosts

    cp /opt/ansible/examples/hosts /etc/ansible/

    cat /etc/ansible/hosts

    [group1]

   192.168.52.[22:23]

   test-[2:3].wn.com

   備註:當然ssh端口不是22也可以設置,未做免密機器也可以設置。示例如下:

   192.168.98.202:12121 ansible_ssh_user=sko ansible_ssh_pass='123'

   web1 ansible_ssh_host=192.168.98.203 ansible_ssh_port=12121

    測試管理機和業務機器的聯通性:

    ansible -m ping 192.168.52.22

    ansible -m ping group1

常用模塊?

ansible是基於模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運行的模塊,ansible只是提供一種框架。

具體用法:ansible 機器 -m 模塊名稱 -a ‘模塊參數’

官網模塊文檔地址:

https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html

查看所有的模塊:ansible-doc -l

查看具體模塊的用法:ansible-doc ping

    ##修改主機名

    示例:ansible -m hostname -a “name=test-2” 192.168.52.22

    注意:/etc/hosts中並不會改變,但是/etc/hostname會改變

    ##對文件或文件夾進行相關操作,創建、刪除、權限、軟硬鏈接、創建目錄

    文件創建:ansible -m file group1 -a “path=/opt/test state=touch”

    文件刪除:ansible -m file group1 -a “path=/opt/test state=absent”

    文件權限:ansible -m file group1 -a “path=/opt/test owner=wn group=nobody mode=0600”

    軟鏈接:ansible -m file group1 -a “src=/opt/test path=/opt/test_1 state=link”

    硬鏈接:ansible -m file group1 -a “src=/opt/test path=/opt/test_2 state=hard”

    創建目錄:ansible -m file group1 -a “path=/opt/test_dir state=directory”

    刪除目錄:ansible -m file group1 -a “path=/opt/test_dir state=absent”

    修改目錄及子文件權限:ansible -m file group1 -a “path=/opt/test_dir owner=wn mode=2755 recurse=yes”

    ##copy模塊用於對文件的遠程拷貝操作(從管理機器到業務機器)

    touch /opt/test

    cat /opt/test

    abcdef

    sha1sum /opt/test

    ansible -m copy group1 -a “src=/opt/test dest=/opt owner=wn group=nobody mode=0400 checksum=f8182e9ccdbe6efd13eb36a056a7db203fe66e40”

    注意:拷貝時目錄是否帶”/”符號

    ansible -m copy group1 -a “content=’hello world’ dest=/opt/test” ##管理機往遠程文件裏寫內容(會覆蓋原內容)

    注意:content的引號不能丟掉

    force:如果目標文件已存在,則是否覆蓋

    backup:如果拷貝的文件內容與原內容不一樣,則會備份一份;如果拷貝過來的文件本機存在,group1的機器上會將/opt/test備份一份,備份文件命名加上時間,再遠程拷貝新的文件爲/opt/test

    ##將遠程機器的文件拷貝到本地。

    注意:不管是拷貝多個機器還是一個機器的文件,在管理機本地目錄都會按照IP/路徑/文件名

    ansible -m fetch group1 -a “src=/opt/test dest=/opt”

    在業務機192.168.52.22執行:

    touch /opt/test_2

    vi /opt/test_2

    在管理機器上執行:

    ansible -m fetch 192.168.52.22 -a “src=/opt/test_2 dest=/opt”

    ##用於管理用戶賬號和用戶屬性

    創建用戶:

    echo “12345” | openssl passwd -1 -stdin

    ansible -m user group1 -a “name=wn password=’fsferr’”

    用戶刪除:

    ansible -m user group1 -a “name=wn state=absent remove=yes”

    注意:

    在刪除用戶的時候報錯:user wn is currently userd by process 5163

    那是因爲我在業務機上執行了su wn,su root操作,你只需要在那臺機器上執行ctrl+D操作就可以了。

    參考:https://www.cnblogs.com/raobenjun/p/8203397.html

    ##用於管理用戶組合用戶組屬性

    ansible -m group group1 -a “name=admin state=present” ##組創建

    ansible -m group group1 -a “name=admin state=absent” ##組刪除

    ##用於管理週期性時間任務

    ansible -m cron group1 -a “name=’cron test’ user=root job=’echo haha > /opt/test’ minue=’*/1’”  ##執行

    ansible -m cron group1 -a “name=’cron test’ state=absent”  ##刪除任務

  • yum模塊

    ##用於使用yum命令來實現軟件包的安裝於卸載。

    ansible -m yum group1 -a “list=repos”  ##列出包信息

    ansible -m yum group1 -a “name=vsftpd”  ##安裝一個軟件

    ansible -m yum 192.168.52.22 -a “state=absent name=vsftpd”  ##刪除軟件包

    ##用於控制服務的啓動、關閉、開機自啓動等

    ansible -m service 192.168.52.22 -a “name=vsftpd state=started enabled=on” ##啓動vsftpd服務,並設置爲開機自啓動

    ansible -m service 192.168.52.22 -a “name=vsftpd state=stopped enabled=false” ##關閉vsftpd服務,並設爲開機不自動啓動

    注意:在安裝過程中報錯unregistered authentication agent for unix-process:

    參考https://blog.csdn.net/qq_42618287/article/details/81265562解決

    ##用於在遠程機器上執行本地腳本

    在管理機上創建腳本,通過ansible將腳本分發到被管理端。

    注意:腳本一直存在於ansible管理機本地,不需要手動拷貝到遠程主機後再執行。

    在管理機創建腳本:

    touch /opt/test.sh

    chmod u+x /opt/test.sh

    cat /opt/test.sh

#!/bin/bash
for ((i=1;i<10;i++))    
do
        for ((n=1;n<=i;n++))
        do
          echo -n  " $i*$n " >> /opt/test.log
        done
        echo " " >> /opt/test.log  
done

    ansible -m script group1 -a “/opt/ansible_test.sh”

    ansible -m command group1 -a “ls /opt” #測試執行是否成功

    ##用於執行linux命令。

    兩個模塊類似,區別在於command模塊不能執行一些類似$HOME,>,<,|等符號,但shell可以。

    ansible -m shell 192.168.52.22 -a “ls /root”

    ansible -m shell 192.168.52.22 -a “echo ‘hello world’ > /opt/testShell”

    ansible -m shell 192.168.52.22 -a “cat /opt/testShell”

    ansible -m command 192.168.52.22 -a “ls /root”

    ansible -m command 192.168.52.22 -a “echo ‘hello world’ > /opt/testShell”

    ansible -m command 192.168.52.22 -a “cat /opt/testShell”  注意:你會發現上一條命令沒有起作用

  • setup模塊

    ##用於收集遠程主機的基本信息(如操作系統類型、主機名、ip、cpu信息,內存信息)

    ansible -m setup 192.168.52.22

    ansible -m setup 192.168.52.22 -a “filter=’ansible_processor’”  ##打印cpu信息

    ansible -m setup 192.168.52.22 -a “filter=’ansible_kernel’”  ##打印內核信息

    ansible -m setup 192.168.52.22 -a “filter=’ansible_hostname’” ##打印主機名

    ansible -m setup 192.168.52.22 -a “filter=’ansible_ens*’” ##打印網卡信息

    ##用於獲取文件的狀態信息

    ansible -m stat 192.168.52.22 -a “path=/opt/test” ## 獲取/opt/test文件的狀態信息

playbook

是ansible用於配置,部署和管理被控節點的劇本。ansible格式的腳本,將所有需要執行的操作按照ansible的編程語法,放到文件中執行。

YAML格式規則:

文件的第一行以”---“開始,表明yaml文件的開始;

以”#”開頭爲註釋;

列表中的所有成員都開始於相同的縮進級別,並且使用”- “作爲開頭(一個橫槓和一個空格);

一個字典是由一個簡單的鍵: 值的形式組成(這個冒號後面必須是一個空格)。

參數說明:

hosts:用於指定要執行任務的主機,其可以是一個或多個由冒號分割主機組;

remote_user:用於指定遠程主機上的執行任務的用戶;

tasks:任務列表,按順序執行任務(如果一個host執行task失敗,整個tasks都會回滾);

handlers:類似task,但需要使用notify通知調用,實現按需調用(不管有多少個通知者進行了notify,等到play中的所有task執行完成之後,handlers也只會被執行一次;handlers最佳的應用場景是用來重啓服務或觸發系統重啓操作);

variables:定義變量可以被多次方便調用;

with_items:迭代列表

示例(安裝vsftpd,並啓動該服務):

注意:修改vsftpd.conf文件,再次運行,會發現觸發了notify。

ansible-playbook /etc/ansible/vsftpd.yaml

roles

通過分別將variables,tasks及handlers等放置於單獨的目錄中,並可以便捷地調用它們的一種機制。主要使用場景代碼複用度較高的情況下。更多瞭解可查看《Ansible--Ansible之Roles

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