1. 程式人生 > 實用技巧 >Ansible批量管理與維護

Ansible批量管理與維護

  Ansible是2013年推出的一種通用自動化工具,可用於配置管理或工作流程自動化。配置管理是一種“基礎架構程式碼”實踐,它將事物編碼,例如應該在系統上安裝什麼包和版本,或者應該執行什麼守護程序。工作流自動化可能是從配置基礎架構到部署軟體的任何事情。Ansible在2015年時被Redhat公司收購。
  Ansible是用Python編寫的,它使用SSH在不同的機器上執行命令。Ansible是無代理的,這使得入手更容易。您只需要在相關機器上安裝SSH訪問和Python。Ansible使用宣告式YML"playbook"
將一組主機(從“hosts”)對映到定義明確的角色。宣告性用於指示Ansible如何設定或更改事物,Ansible才進行必要的更改。

  200-500臺伺服器,用ansible。更多的則使用saltstack

  ansible 無需安裝客戶端,依賴ssh服務。 -->ssh 認證

ansible 部署

安裝ansible

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
​
#ansible 管理端
yum install ansible -y
yum install libselinux-python -y
​
#backup nfs01  被管理端
yum install libselinux-python -y
[root@m01 ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg   # ansible的配置檔案
├── hosts       # ansible管理了 哪些伺服器 伺服器列表
└── roles      # 角色

[root@m01 ~]# cat /etc/ansible/hosts
[lewen]
172.16.1.31
172.16.1.41
​
ansible lewen -m command -a "hostname"
ansible lewen -m command -a "yum install cowsay -y"

-m 後邊是模組的名字 -m MODULE_NAME,--module-name=MODULE_NAME module name to execute(default=command). -a 後面是要執行的命令  -a MODULE_ARGS,-args=MODULE_ARGS. module arguments.

複製檔案

利用ansible遠端批量拷貝檔案或目錄。
語法:

 ansible lewen -m copy -a "sre=/etc/passwd dest=/tap/oldgirl.txt owner=lewen group=lewen sode=0755" 
注意:
1)如果指定的目標目錄不存在,系統會自動建立,否則源目錄會放到目標目錄下面去:
2)如果copy的是檔案,dest指定的名字和源如果不同,並且它不是已經存在的目錄,相當於copy過去後再重新命名: 3)若果dest是目標機器上已經存在的目錄,則會直接把檔案copy 到該目錄下面。 4)設定的使用者和組lewen在所有客戶端必須存在。

[root@m01 ~]# ansible lewen -m copy -a "src=/etc/hosts dest=/tmp owner=lewen mode=0755"  # backup=yes 已存在的檔案就複製備份
172.16.1.41 | SUCCESS => {
"changed": true,
"checksum": "bc07bb4d3a780f4fd8cae94ec7bff04edb1a5a4e",
"dest": "/tmp/hosts",
"gid": 0,
"group": "root",
"md5sum": "55ee21bf1168f9be70abd35bf29d8e4a",
"mode": "0755",
"owner": "lewen",
"size": 364,
"src": "/root/.ansible/tmp/ansible-tmp-1517744820.18-259504826638509/source",
"state": "file",
"uid": 500
}
172.16.1.31 | SUCCESS => {
"changed": true,
"checksum": "bc07bb4d3a780f4fd8cae94ec7bff04edb1a5a4e",
"dest": "/tmp/hosts",
"gid": 0,
"group": "root",
"md5sum": "55ee21bf1168f9be70abd35bf29d8e4a",
"mode": "0755",
"owner": "lewen",
"size": 364,
"src": "/root/.ansible/tmp/ansible-tmp-1517744820.17-14642605512978/source",
"state": "file",
"uid": 500
}
​
​
[root@m01 ~]# ansible lewen -m command -a "ls -l /tmp/hosts"
172.16.1.31 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 lewen root 364 Feb 4 19:47 /tmp/hosts
172.16.1.41 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 lewen root 364 Feb 4 19:47 /tmp/hosts

ansible lewen -m copy -a "src=/etc/hosts dest=/tmp backup=yes"

ansible-doc -l|wc -l
ansible-doc -s copy # 檢視文件

其他常用模組命令 ansible lewen -m copy -a "src=/server/scripts/yum-htop.sh dest=/server/scripts/ " ansible lewen -m shell -a "/bin/sh /server/scripts/yum-htop.sh" ansible lewen -m script -a "/server/scripts/yum.sh"

定時任務

linux 定時任務。
分,時,日,月,周 執行的命令。

# 建立定時任務
[root@m01 scripts]# ansible lewen -m cron -a "name='restart network' minute=00 hour=00 job=' /etc/init.d/network restart >/dev/null 2>&1'" 172.16.1.31 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "restart network" ] } 172.16.1.41 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "restart network" ] } ​# 檢視定時任務 [root@m01 scripts]# ansible lewen -a "crontab -l"
172.16.1.41 | SUCCESS | rc=0 >> #time sync by lidao at 2017-03-08 */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 #check & send result lee at 2017-01-01 03 * * * /bin/sh /server/scripts/check.sh >/dev/null 2>&1 #Ansible: restart network 00 * * * /etc/init.d/network restart >/dev/null 2>&1 172.16.1.31 | SUCCESS | rc=0 >> #time sync by lidao at 2017-03-08 */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 #Ansible: restart network 00 * * * /etc/init.d/network restart >/dev/null 2>&1​
​# 取消定時任務
[root@m01 ~]# ansible oldboy -m cron -a "name='restart network' state=absent "
172.16.1.31 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": []
}
172.16.1.41 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": []
}

常用模組

每個模組就是一個功能
command(預設的模組)  #執行命令模組****
shell          #執行shell 指令碼模組****。           # 支援shell 管道更多的功能
script         #把指令碼發到客戶端,然後執行。****。
copy          #把本地檔案傳送到遠端
file          # 設定檔案屬性模組。
service        #系統服務管理模組。
cron          #計劃任務管理模組
yum          #yum軟體包安裝管理模組
synchronize     #使用rsync同步檔案模組。

eg:
  ansible lewen -m service -a "name=crond state=started enabled=yes"
ssh 認證模組 authorized_key   #-Adds or removes an SSH authorized key

playbook

ansible 劇本

核心功能
1.PyYAML-劇本的語言。

2.paramiko-遠端連線與資料傳輸。
3.Jinjia2

mkdir -p /server/playbook
​
[root@m01 playbook]# cat ifconfig.yml
- hosts: lewen
  tasks:
    - command: ifconfig
    - shell: ifconfig >/tmp/ip.log
​
​
ansible-playbook -C ifconfig.yml       # 檢查劇本
ansible-playbook ifconfig.yml          


[root@m01 playbook]# cat print-ip.yml          
- hosts: all
  tasks:
    - name: get ip address
      shell: ifconfig eth0 |awk -F "[ :]+" 'NR==2{print $4}' >>/tmp/ip.log


ansible-playbook -C print-ip.yml
ansible-playbook print-ip.yml
ansible all -a "tail -1 /tmp/ip.log"
​

ansible oldboy -m cron -a 'name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present'
​
​# playbook新增定時任務
[root@m01 playbook]# cat add-cron.yml
- hosts: oldboy
  tasks:
    - name: add restart network cron
      cron: name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present
​
​

檢視
[root@m01 playbook]# ansible oldboy -a "crontab -l"
172.16.1.41 | SUCCESS | rc=0 >>
#time sync by lidao at 2017-03-08
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
#check & send result lee at 2017-01-01
03 * * * /bin/sh /server/scripts/check.sh >/dev/null 2>&1
172.16.1.31 | SUCCESS | rc=0 >>
#time sync by lidao at 2017-03-08
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1

playbook新增定時任務

兩種書寫格式
(1)
- hosts: oldboy tasks: - name: add restart network cron cron: name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present
(2) - hosts: oldboy tasks: - name: add restart network cron cron: name: restart network minute: 00 hour: 00 job: /etc/init.d/network restart >/dev/null 2>&1 state: present

例3:對同一臺機器配置多個任務

重啟網路 service

安裝軟體 yum

顯示時間資訊到檔案 date

[root@m01 playbook]# cat manage.yml
- hosts: all
  tasks:
    - name: restart network
      service:                    #服務
        name: network               #伺服器名
        state: restarted            #狀態
    - name: install tree nmap lrzsz iftop htop iotop nc
      shell: yum install -y tree nmap lrzsz iftop htop iotop nc
    - name: print date to file
      shell: date +%F >>/tmp/date.log

yml 轉化後的格式:
[ { hosts: 'all', tasks: [ { name: 'restart network', service: { name: 'network', state: 'restarted' } }, { name: 'install tree nmap lrzsz iftop htop iotop nc', shell: 'yum install -y tree nmap lrzsz iftop htop iotop nc' }, { name: 'print date to file', shell: 'date +%F >>/tmp/date.log' } ] } ]

-

view