15 張圖精細化介紹 Ansible 與實戰演練

作者:liugp

出處:https://goo.gs/5q2v1

一、概述

Ansible是新出現的自動化運維工具,基於 Python 開發,集合了衆多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。

Ansible 特點:

官方文檔:https://docs.ansible.com/ansible/latest/
GitHub 地址:https://github.com/ansible/ansible

二、Ansible 架構

上圖爲 ansible 的基本架構,從上圖可以瞭解到其由以下部分組成:

三、Ansible 工作原理


從上面的圖上可以瞭解到:

四、Ansible 安裝與基礎配置

yum install epel-release
yum -y install ansible
ansible --version

1)開啓記錄日誌

配置文件:/etc/ansible/ansible.cfg

# 去掉前面的'#'號
#log_path = /var/log/ansible.log ==> log_path = /var/log/ansible.log

2)去掉第一次連接 ssh ask 確認

# 第一種(推薦)
vi /etc/ansible/ansible.cfg
# 其實就是把#去掉
# host_key_checking = False  ==> host_key_checking = False

# 第二種
vi /etc/ssh/ssh_config
StrictHostKeyChecking ask  ==> StrictHostKeyChecking no

五、Ansible 的七個命令

安裝完 ansible 後,發現 ansible 一共爲我們提供了七個指令:ansibleansible-docansible-galaxyansible-lintansible-playbookansible-pullansible-vault。這裏我們只查看 usage 部分,詳細部分可以通過 "指令 -h" 的方式獲取。

1)ansible

ansible 是指令核心部分,其主要用於執行 ad-hoc 命令,即單條命令。默認後面需要跟主機和選項部分,默認不指定模塊時,使用的是command模塊。不過默認使用的模塊是可以在/etc/ansible/ansible.cfg 中進行修改的#module_name = command

ansible 192.168.182.130 -a 'date'

2)ansible-doc

該指令用於查看模塊信息,常用參數有兩個 - l 和 -s

#列出所有已安裝的模塊ansible-doc  -l
ansible-doc  -l
#查看具體某模塊的用法,這裏如查看command模塊
ansible-doc  -s command

3)ansible-playbook

ansible-playbook 命令是使用最多的指令,其通過讀取 playbook 文件後,執行相應的動作,這個後面會做爲一個重點來講。

4)ansible-galaxy

ansible-galaxy 指令用於方便的從 https://galaxy.ansible.com/ 站點下載第三方擴展模塊,我們可以形象的理解其類似於 centos 下的 yum、python 下的 pip 或 easy_install 。如下示例:

ansible-galaxy install aeriscloud.docker

5)ansible-lint

ansible-lint 是對 playbook 的語法進行檢查的一個工具。用法如下:

ansible-lint playbook.yml

6)ansible-pull

該指令使用需要談到 ansible 的另一種模式,pull 模式,這和我們平常經常用的 push 模式剛好相反,其適用於以下場景:你有數量巨大的機器需要配置,即使使用非常高的線程還是要花費很多時間;你要在一個沒有網絡連接的機器上運行 Anisble,比如在啓動之後安裝。

7)ansible-vault

六、Ansible 主要組成部分

1)ansible 命令執行來源

2)ansible 管理方式

3)ansible 主要操作對象

注意事項:

七、Ansible 連接被控端方式

1)ssh 密鑰

# 生成祕鑰
ssh-keygen
# 將祕鑰拷貝到被管理服務器上
ssh-copy-id  -i ~/.ssh/id_rsa.pub -p 22 root@192.168.182.130

2)賬號密碼

1、命令行配置

# -k:交互式
ansible -uroot -k 192.168.182.130 -m ping

2、配置文件中配置

# 默認主機配置文件:/etc/ansible/hosts
192.168.182.130 ansible_ssh_user=root ansible_ssh_pass=123456

[web]
192.168.182.130 ansible_ssh_user=root ansible_ssh_pass=123456

常用的配置參數如下:

八、Host Inventory(主機清單)

主機清單配置(默認配置文件:/etc/ansible/hosts

1)添加被管控節點

192.168.182.110

示例:

# -m:指定模塊
# -a:指定參數
ansible 192.168.182.110 -m ping
ansible 192.168.182.110 -m shell -a "df -h"

2)配置主機組

# 定義webservers組
[webservers]
192.168.182.110
192.168.182.112

示例:

# -m:指定模塊
# -a:指定參數
ansible webservers -m ping
ansible webservers -m shell -a "df -h"

3)配置連接用戶名和密碼

[webservers]
192.168.182.130 ansible_ssh_user=root ansible_ssh_pass=123456

常用配置參數如下:

示例:

ansible 192.168.182.130 -m ping

4)子分組

[web]
192.168.182.130
192.168.182.110
[mysql]
192.168.182.111
# 子分組
[nfs:children]
web
mysql
# 對分組統一定義變量
[nfs:vars]
ansible_ssh_user=root
ansible_ssh_pass=123456
ansible_ssh_port=22

示例:

ansible nfs -m ping
# -o:一行顯示
ansible nfs -m ping -o

5)自定義主機列表文件

cat>hostlist<<EOF
[web]
192.168.182.130
192.168.182.110
[mysql]
192.168.182.111
# 子分組
[nfs:children]
web
mysql
# 對分組統一定義變量
[nfs:vars]
ansible_ssh_user=root
ansible_ssh_pass=123456
ansible_ssh_port=22
EOF

示例:

# -i:指定主機列表文件
ansible -i hostlist nfs -m ping

九、Ad-Hoc(點對點模式)

官方文檔:https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html

1)簡介

ad-hoc 命令是一種可以快速輸入的命令,而且不需要保存起來的命令,一般測試調試時用的多,ad-hoc 簡而言之,就是 " 臨時命令 "。

2)常用模塊

1、command 模塊(默認模塊)

默認模塊,沒有 shell 強大,基本上 shell 模塊都可以支持 command 模塊的功能。

【1】幫助

ansible-doc command
# 推薦使用下面這個
ansible-doc command -s

【2】參數解釋

【3】示例演示

# 上面命令表示在 web 主機上執行 ls 命令,因爲使用的是 root 用戶,所以默認情況下,ls 出的結果是 web 主機中 root 用戶家目錄中的文件列表。
ansible web -m command -a "ls"

# chdir 參數表示執行命令之前,會先進入到指定的目錄中,所以上面命令表示查看 web 主機上 /testdir 目錄中的文件列表,返回顯示有2個文件。
ansible web -m command -a "chdir=/testdir ls"

# 下面命令表示 /testdir/testfile1 文件存在於遠程主機中,則不執行對應命令。/testdir/testfile3 不存在,才執行”echo test”命令。
ansible web -m command -a "creates=/testdir/testfile1 echo test"

# 下面命令表示 /testdir/testfile3 文件不存在於遠程主機中,則不執行對應命令。/testdir/testfile1 存在,才執行”echo test”命令。
ansible web -m command -a "removes=/testdir/testfile1 echo test"

2、shell 模塊

shell 模塊 [執行遠程主機的 shell/python 等腳本]。

【1】查看幫助

ansible-doc shell -s

【2】示例演示

# -o:一行顯示
# 安裝httpd
ansible web -m shell -a 'yum -y install httpd' -o

# 查看時間
ansible web -m shell -a 'uptime' -o

3、script 模塊

script 模塊 [在遠程主機執行主控端的 shell/python 等腳本]。

【1】查看幫助

ansible-doc script -s

【2】參數解釋

【3】示例演示

# 下面命令表示 ansible 主機中的 /testdir/testscript.sh 腳本將在 web 主機中執行,執行此腳本之前,會先進入到 web 主機中的 /opt 目錄
ansible web -m script -a "chdir=/opt /testdir/testscript.sh"

# 下面命令表示,web主機中的 /testdir/testfile1文件已經存在,ansible 主機中的 /testdir/testscript.sh 腳本將不會在 web 主機中執行。
ansible web -m script -a "creates=/testdir/testfile1 /testdir/testscript.sh"

# 下面命令表示,web 主機中的 /testdir/testfile1 文件存在,ansible 主機中的 /testdir/testscript.sh 腳本則會在 web 主機中執行。
ansible ansible-demo3 -m script -a "removes=/testdir/testfile1 /testdir/testscript.sh"

4、raw 模塊

raw 模塊 [類似於 command 模塊、支持管道傳遞]。

【1】查看幫助

ansible-doc raw -s

【2】示例演示

ansible web -m raw -a "ifconfig eth0 |sed -n 2p |awk '{print \$2}' |awk -F: '{print \$2}'"

5、copy 模塊

copy 模塊 從主控端複製文件到被控端。

【1】查看幫助

ansible-doc copy -s

【2】示例演示

# -a,--args:後面接參數
ansible web -m copy -a 'src=/etc/ansible/hosts dest=/tmp/hosts owner=root group=bin mode=777'

# backup=yes/no:文件存在且文件內容不一樣是否備份,默認不備份
ansible web -m copy -a 'src=/etc/ansible/hosts dest=/tmp/hosts owner=root group=bin mode=777 backup=yes'

6、fetch 模塊

copy 模塊從被控端複製文件到主控端,正好跟 copy 相反。

【1】查看幫助

ansible-doc fetch -s

【2】示例演示

# 跟copy支持的參數差不多,src:遠端主機的目錄,dest:主控端目錄,其實真正存放的目錄在:/tmp/192.168.182.129/tmp/up.sh,會按每臺主機分組存放
#  This `must' be a file, not a directory:只支持單個文件獲取
ansible 192.168.182.129 -m fetch -a "src=/etc/fstab dest=/testdir/ansible/"

7、unarchive 模塊(解包模塊)

unarchive 模塊是解包模塊。

【1】查看幫助

ansible-doc unarchive -s

【2】參數解釋

【3】示例演示

ansible 192.168.182.129 -m unarchive -a 'src=/testdir/ansible/data.tar.gz dest=/tmp/tmp/'

8、archive 模塊(打包模塊)

unarchive 模塊是打包模塊。

【1】查看幫助

ansible-doc archive -s

【2】示例演示

# path:主控端目錄,format:壓縮格式,dest:被控端目錄文件'
ansible 192.168.182.129 -m archive -a 'path=/tmp/ format=gz dest=/tmp/tmp/t.tar.gz'

9、user 模塊

【1】查看幫助

ansible-doc user -s

【2】示例演示

# 創建用戶(present:默認,可以不寫)
ansible web -m user -a 'name=test state=present'

# 刪除用戶(absent)
ansible web -m user -a 'name=test state=absent'

# 修改密碼
# 步驟一、生成加密密碼
echo '777777'|openssl passwd -1 -stdin

# 步驟二、修改祕密
ansible web -m user -a ''

# 修改shell
ansible web -m user -a 'name=test shell=/sbin/noglogin append=yes'

10、group 模塊

【1】查看幫助

ansible-doc group -s

【2】示例演示

# 創建
ansible 192.168.182.129 -m group -a 'name=testgroup system=yes'
# 刪除
ansible 192.168.182.129 -m group -a 'name=testgroup state=absent'

11、yum 模塊

【1】查看幫助

ansible-doc yum -s

【2】示例演示

# 升級所有包
ansible web -m yum -a ' state=latest'

# 安裝apache
ansible web -m yum -a ' state=latest'

12、service 模塊

【1】查看幫助

ansible-doc service -s

【2】示例演示

ansible web -m service -a 'name=httpd state=started'

ansible web -m service -a 'name=httpd state=started enabled=yes'

ansible web -m service -a 'name=httpd state=stopped'

ansible web -m service -a 'name=httpd state=restarted'

ansible web -m service -a 'name=httpd state=started enabled=no'

13、file 模塊

【1】查看幫助

ansible-doc file -s

【2】示例演示

# 創建文件
ansible web -m file -a 'path=/tmp/88.txt mode=777 state=touch'

# 創建目錄
ansible web -m file -a 'path=/tmp/99 mode=777 state=directory'

# 刪除
ansible web -m file -a 'path=/tmp/99 state=absent'

14、setup 模塊

【1】查看幫助

ansible-doc setup -s

【2】示例演示

ansible web -m setup

ansible web -m setup -a 'filter=ansible_all_ipv4_addresses'

15、cron 模塊

【1】查看幫助

ansible-doc cron -s

【2】示例演示

# 創建定時任務
ansible 192.168.182.129 -m cron -a 'minute=* weekday=1,3,5,6,7 job="/usr/bin/wall FBI warning" name=warningcron'

# 關閉定時任務
ansible 192.168.182.129 -m cron -a 'disabled=true job="/usr/bin/wall FBI warning" name=warningcron'

# 刪除定時任務
ansible 192.168.182.129 -m cron -a ' job="/usr/bin/wall FBI warning" name=warningcron state=absent'

16、hostname 模塊

【1】查看幫助

ansible-doc hostname -s

【2】示例演示

ansible 192.168.182.129 -m hostname -a 'name=192.168.182.129'

Ansible 的介紹和簡單使用就先到這裏了,還有一個 ansible-playbook 是非常重要,內容也是比較多,就放到下篇文章介紹了!

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/YvRlCotdyjTSDRyPY_sPpQ