自動化運維工具——Ansible

Ansible基本概述

Ansible是新出現的自動化運維工具,它基於Python開發,集合了衆多運維工具(puppet、chef、func、fabric)的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。
它是一個配置管理系統,我們只需要通過ssh訪問服務器或設備就可以幫助我們完成一些批量任務或重複性的工作。

ansible軟件特點

  • 部署簡單。它不需要單獨安裝客戶端,只需要通過ssh協議就可以對設備進行管理
  • 配置簡單。安裝完成不需要啓動任何服務,僅需要安裝對應工具即可
  • 擴展性強。基於Python開發,可自定義Python模塊實現批量管理
  • 模塊化。所有的操作都基於模塊

ansible軟件服務意義

  • 提高工作效率
  • 提高工作準確度
  • 減少維護成本
  • 減少重複性工作

ansible基礎架構圖

圖片來自網絡
上圖模塊詳解:

  1. (connection plugins)連接插件。用於連接主機(被管理端)
  2. (core modules)核心模塊。連接主機實現操作,它依賴於具體的模塊來實現具體的事情
  3. (custom modules)自定義模塊。根據需求編寫模塊
  4. (plugins)插件。完成模塊功能的補充
  5. (playbooks)劇本。將多個任務定義在劇本中,由Ansible自動執行
  6. (host inventory)主機清單。定義Ansible需要操作主機的範圍
    環境準備:
IP OS 描述
192.168.8.41 CentOS7.8 Ansible管理端
192.168.8.51 CentOS7.8 被管理端
  1. 先升級epel源,否則可能找不到軟件包

    yum install -y epel-relase
    
  2. 安裝軟件

    yum install -y ansible
    
  3. ssh部署公鑰發至被管理端

    #!/bin/bash
     for i in `cat ip.list`
     do
      ssh-copy-id -i /root/.ssh/id_rsa.pub root@$i "-o StrictHostKeyChecking=no" &>/dev/null
     done 
    
  4. 清單管理
    配置/etc/ansible/hostsinventory文件

    //方法1:通過IP定義主機
    [jxj]
    192.168.8.51
     
    //方法2:iventory文件支持主機名和非標準的ssh端口以及正則表達式和
    [domainname]
    www.web1.com:11111
    www.web2.com
    www.web3.com
    www.web[1:3].com
    //方法3:支持指定變量,可對個別主機進行特別配置,如登錄用戶、密碼等
    [server_ip_list]
    192.168.8.100 ansible_ssh_user_=root ansible_ssh_pass="123456"
    

inventory內置變量

參數 用途 例子
ansible_ssh_host 定義 hosts ssh地址 ansible_ssh_host=192.168.8.100
ansible_ssh_port 定義shh端口 ansible_ssh_port=5201
ansible_ssh_user 定義hosts ssh認證用戶 ansible_ssh_user=user
ansible_ssh_pass 定義hosts ssh認證密碼 ansible_ssh_pass=pass
ansible_sudo 定義hosts sudo用戶 ansible_sudo=sssss
ansible_sudo_pass 定義hosts sudo密碼 ansible_sudo_pass=123
ansible_sudo_exe 定義hosts sudo路徑 ansible_sudo_exe=/usr/sbin/sudo
ansible_connection 定義hosts連接方式 ansible_connection=local
ansible_ssh_shell_type 定義hosts shell類型 ansible_ssh_shell_type=bash
ansible_python_interpreter 定義hosts任務執行python路徑 ansible_python_interpreter=/usr/bin/python3.7
ansible_*_interpreter 定義hosts其它語言解析路徑 ansible_*_interpreter=/usr/bin/java
ansible_ssh_private_key_file 定義hosts私鑰 ansible_ssh_private_key_file=/root/key

ansible常用模塊

  • command模塊(默認)

    ansible jxj -a "hostname"
    

    chdir:在執行命令之前切換相應目錄

    ansible jxj -m command -a "chdir=/tmp touch aa.txt"
    creates: 		//如果文件存在,不執行命令操作
    ansible jxj -m command -a "creates=c.txt touch bb.txt"
    removes: 		//如果文件存在,執行命令
    ansible jxj -m command -a "removes=c.txt touch bb.txt"
    

注意:command模塊不支持< > | ; &等符號

  • shell模塊(支持正則表達式以及一些特殊符號)

    ansible jxj -m shell -a "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config"
    
  • copy模塊: 將數據信息進行批量分發(默認覆蓋內容)

    ansible jxj -m copy -a "src=/etc/hosts dest=/etc/"
    

    copy模塊參數:

    src:本地路徑/文件
    dest:目標路徑/文件(如存在重命名)
    backup(=yes/no):額外生成一個備份文件
    group:指定組
    content:添加內容
    owner:指定主
    mode:修改權限
    
  • file模塊: 設置文件屬性信息

    ansible jxj -m file -a "dest=/etc/hosts owner=root group=root mode=777"	
    

    state參數:

    =absent    --- 缺席/刪除數據信息
    =directory --- 創建一個目錄信息
    =file      --- 檢查創建的數據信息是否存在 綠色存在 紅色不存在
    =hard      --- 創建一個硬鏈接文件
    =link      --- 創建一個軟鏈接文件
    =touch     --- 創建一個文件信息
    
  • yum模塊:

    ansible jxj -m yum -a "name=httpd state=installed"
    

    name:指定安裝軟件名稱
    state參數:

    =installed    //安裝軟件
    =present	   //安裝軟件
    =latest        //安裝最新版軟件
    =absent     //卸載軟件
    =removed   //卸載軟件
    
  • service模塊: 管理服務器的運行狀態
    name: 指定管理的服務名稱
    enabled(=yes/no): 指定服務是否開機自啓動

    ansible jxj -m service -a "name=sshd state=restarted"
    

    state參數:

    停止stopped
    開啓started
    重啓restarted
    
  • cron模塊: 批量設置多個主機的定時任務信息

    ansible jxj -m cron -a "minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'" 
    

    crontab -e

    minute:               //設置分鐘信息( 0-59,  */2)                       
    hour:                  //設置小時信息( 0-23, */2 )                
    day:                   //設置日期信息( 1-31, *, */2)                       
    month:              //設置月份信息 ( 1-12,*/2 )                       
    weekday:          // 設置周信息 ( 0-6 for Sunday-Saturday)                      
    job                 //用於定義定時任務需要乾的事情
    

    給計劃任務設置註釋信息(只能操作ansible設置的)

    ansible jxj -m cron -a "name='jxjcron' minute=3 hour=2 job='/usr/sbin/ntpdate mtp1.aliyum.com &>/dev/null' disabled=yes"
    

    刪除指定計劃任務(只能操作ansible設置的)

    ansible jxj -m cron -a "name='jxjcron' state=absent"
    
  • mount模塊: 批量進行掛載操作

    rc:  需要掛載的存儲設備或文件信息 
    path: 指定目標掛載點目錄
    fstype: 指定掛載時的文件系統類型
    state=
    present/mounted     //掛載
    present: 	不會立即掛載,修改fstab文件,實現開機自動掛載
    mounted:	//立即掛載, 並且會修改fstab文件,實現開機自動掛載 
    absent/unmounted    //卸載
    absent:     //立即卸載, 並且會刪除fstab文件信息,禁止開機自動掛載
    unmounted:  //立即卸載, 但是不會會刪除fstab文件信息
    
  • user模塊: 實現批量創建用戶

    ansible jxj -m user -a "name=beitai_one"
    

    user模塊參數:

    name:指定創建用戶的名稱
    gid:指定gid
    uid:指定uid
    group/groups:指定組信息
    create_home(yes/no):創建家目錄,默認yes
    shell:指定用戶登錄的shell類型
    

    給用戶生成密碼:

    ansible all -i localhost, -m debug -a "msg={{'123456' | password_hash('sha512','密碼口令')}}"
    

    使密碼生效:

    ansible jxj -m user -a 'name=jxj1 password=$6$jxj$lb4BpCo33QoczNvSLYD5DagSXHWBgYeNCYx38LxkrjZFWlYEgkVG4SzWgxACy3LEoOps3latRCqoTLQGRVFRL1'
    

更多模塊信息請參考Ansible官方文檔或使用ansible-doc -l命令查看。

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