一、ansible概述
1.1? ansible介紹
????????Ansible 是一個基于 Python 開發的配置管理和應用部署工具,近年來在自動化管理領域表現突出。它集成了許多傳統運維工具的優點,幾乎可以實現 Pubbet 和 Saltstack 所具備的功能。
1.2? ansible能做什么
????????批量處理。Ansible 可以對成千上萬的主機進行批量配置、部署和管理。例如,以前需要登錄到每一臺主機上執行一個或多個操作,而使用 Ansible 時,只需在一個指定的控制節點上操作,便可完成所有主機的任務。
????????基于模塊工作。Ansible 的工作原理基于模塊,它提供了一種執行框架,但本身并不執行任務。實際執行操作的是 Ansible 的模塊,例如,copy 模塊用于將文件復制到遠程主機,service 模塊用于管理服務的啟動、停止和重啟等。
1.3? ansible兩大特性
(1)無代理。Ansible 的一個顯著特點是 Agentless(無代理),它像普通命令一樣工作,并不是 C/S 軟件。只需在一個控制節點主機上安裝一次 Ansible,而遠程主機無需安裝 Ansible 或其他額外的服務,通常通過 SSH 連接來控制遠程主機。
(2)冪等性。Ansible 的另一個突出特性是大多數模塊都具備冪等性(idempotence)。冪等性指的是無論執行多少次相同操作,結果始終相同。舉例來說,執行 systemctl stop xxx 命令停止服務時,如果目標服務已處于停止狀態,命令不會再做任何操作,因此多次執行 stop 命令的結果始終是服務保持停止狀態,它是冪等的,而 systemctl restart xxx 則不是。
1.4? ansible架構
????????Ansible 管理節點和遠程主機節點間通過SSH協議進行通信。所以配置Ansible的時候,只需保證從Ansible 管理節點通過SSH協議能夠連接到被管理的遠程節點即可。注意,SSH必須配置為公鑰認證登錄方式,而非密碼認證。
????????Ansible可以同時管理Red Hat系的Linux、Debian系的Linux以及Windows 主機。Ansible的工作原理如圖所示:
1.5? 為什么選擇ansible
????????在使用時,用戶通過服務器終端輸入命令或 playbook,系統根據預定規則將 playbook 拆解成 play,再組織成 Ansible 可識別的任務,調用模塊和插件。
????????任務通過 SSH 連接發送到遠程主機執行,并返回結果,執行完畢后,臨時文件會自動刪除,可以有效地節省內存。
????????Ansible 的許多模塊在執行時會先檢查目標節點是否需要執行某項任務,因此,用戶可以放心地讓 Ansible 執行任務。大多數情況下,重復執行同一任務不會帶來副作用。
二、Ansible部署
服務器類型 | IP地址 | 安裝軟件 |
Ansible管理服務器 | 192.168.166.11 | Ansible |
被管理客戶端 | 192.168.166.12 | ...... |
被管理客戶端 | 192.168.166.13 | ...... |
2.1? 服務器安裝ansible服務
[root@localhost ~]#yum install -y epel-release
[root@localhost ~]#yum install -y ansible[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# ls
ansible.cfg hosts roles
----------------------------------------------------------------
ansible.cfg # ansible的配置文件,一般無需修改
hosts # ansible的主機清單,用于存儲需要管理的遠程主機的相關信息
roles # 公共角色目錄
2.2? 配置主機清單
vim /etc/ansible/hosts
[webservers] #配置組名
192.168.166.12 #組里包含的被管理主機IP地址或主機名(主機名需要先修改/etc/hosts文件)
[dbservers]
192.168.166.13
2.3? 配置密鑰對驗證
#生成密鑰對(一路回車)
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
#導入對方主機
ssh-copy-id root@192.168.166.12
ssh-copy-id root@192.168.166.13
?三、Ansible命令模塊
命令格式:ansible? <組名>? -m? <模塊>? -a? <參數列表>
ansible-doc -l #查詢所有已安裝的模塊,按q退出
#總共有三千多個模塊,我們只需要學習我們常用的就好了
3.1? command模塊
????????在遠程主機執行命令,不支持管道,重定向等shell的特性。
ansible-doc -s command?? ??? ?#-s 列出指定模塊的描述信息和操作動作
ansible 192.168.80.11 -m command -a 'date' #指定 ip 執行 date
ansible webservers -m command -a 'date' #指定組執行 date
ansible dbservers -m command -a 'date'
ansible all -m command -a 'date' #all 代表所有 hosts 主機
ansible all -a 'date' #如省略 -m 模塊,則默認運行 command 模塊
3.1.1? 示例:chdir
#在遠程主機上運行命令前提前進入目錄
[root@localhost opt]# ansible dbservers -m command -a 'chdir=/opt ls ./'
192.168.166.13 | CHANGED | rc=0 >>
ceph-release-1-1.el7.noarch.rpm
rh
3.1.2? 示例:creates
#判斷指定文件是否存在,如果存在,不執行后面的操作
[root@localhost opt]# ansible dbservers -m command -a 'creates=/opt/123.txt echo helloworld >/opt/123.txt '
192.168.166.13 | CHANGED | rc=0 >>
helloworld >/opt/123.txt#切換dbservers查看
[root@dbservers opt]# ls
123.txt rh
3.1.3? 示例:removes
#判斷指定文件是否存在,如果存在,執行后而的操作
[root@localhost opt]# ansible dbservers -m command -a 'removes=/opt/123.txt touch /opt/123.txt'
[WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.166.13 | CHANGED | rc=0 >>[root@localhost opt]# ansible dbservers -m command -a 'removes=/opt/123.txt rm -f /opt/123.txt'
[WARNING]: Consider using the file module with state=absent rather than running 'rm'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.166.13 | CHANGED | rc=0 >>[root@localhost opt]# ansible dbservers -m command -a 'removes=/opt/123.txt touch /opt/123.txt'
192.168.166.13 | SUCCESS | rc=0 >>
skipped, since /opt/123.txt does not exist
3.2 shell模塊
????????在遠程主機執行命令,相當于調用遠程主機的shell進程,然后在該shell 下打開一個子shell運行命令(支持管道符號等功能)
ansible-doc -s shell
#寫入helloworld到123.txt
[root@localhost opt]# ansible dbservers -m shell -a 'echo helloworld >/opt/123.txt '
192.168.166.13 | CHANGED | rc=0 >>
#過濾IP地址
[root@localhost ~]# ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2{print $2}") | cut -d " " -f2'
192.168.166.13 | CHANGED | rc=0 >>
192.168.166.13


3.3? cron模塊
????????在遠程主機定義任務計劃,其中有兩種狀態(state):present表示添加(可以省略),absent表示移除。
ansible-doc -s cron #查看相關說明,按q退出常用參數:
minute/hour/day/month/weekday:分/時/日/月 /周
job:任務計劃要執行的命令
name :任務計劃的名稱#每兩個月的10號的早上和晚上十點的第十分鐘執行一次復制系統內核日志到/opt/
linux:10 10,22 10 */2 * /usr/bin/cp /var/log/messages /opt
ansible:
[root@localhost opt]# ansible dbservers -m cron -a 'minute="10" hour="10,20" day="10" month="*/2" job="/usr/bin/cp /var/log/messages /opt" name="test crontab"'
192.168.166.13 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "envs": [], "jobs": ["test crontab"]
}
#查看任務列表
[root@localhost opt]# ansible dbservers -a 'crontab -l'
192.168.166.13 | CHANGED | rc=0 >>
#Ansible: test crontab
10 10,20 10 */2 * /usr/bin/cp /var/log/messages /opt
[root@localhost opt]#
#切換到dbservers上傳查看
[root@localhost opt]# crontab -l
#Ansible: test crontab
10 10,20 10 */2 * /usr/bin/cp /var/log/messages /opt


3.4 user模塊
? ? ? ? 用戶管理模塊
ansible-doc -s user常用的參數:
name :用戶名,必選參數
state=present|absent:創建賬號或者刪除賬號,present表示創建,absent 表示刪除
system=yes|no:是否為系統賬號
uid: 用戶uid
group:用戶基本組
groups:附加組
shell:默認使用的shell
move_home=yse|no:如果設置的家日錄已經存在,是否將已經存在的家日錄進行移動
password:用戶的密碼,建議使用加密后的字符串
comment:用戶的注釋信息
remove=yes|no:當state=absent時, 是否刪除用戶的家目錄ansible webservers -m user -a 'name="test001"' #創建
ansible webservers -m command -a 'tail -1 /etc/passwd' #查看確認
ansible webservers -m user -a 'name="test001" state=absent' #刪除
ansible webservers -m command -a 'tail -1 /etc/passwd' #查看確認
3.5 group模塊
用戶組管理的模塊
ansible-doc -s group #查看相關文檔ansible dbservers -m group -a 'name=mysql gid=300 system=yes'
ansible dbservers -m command -a 'tail -1 /etc/group'
ansible dbservers -m user -a 'name="test002" uid=300 system=yes group=mysql'
ansible dbservers -m command -a 'tail -2 /etc/passwd'
ansible dbservers -a 'id test002'
#創建mysql組
[root@localhost ~]# ansible dbservers -m group -a 'name=mysql gid=306 system=yes'
192.168.166.13 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "gid": 306, "name": "mysql", "state": "present", "system": true
}
#查看已創建的組
[root@localhost ~]# ansible dbservers -a 'tail /etc/group'
192.168.166.13 | CHANGED | rc=0 >>
postfix:x:89:
sshd:x:74:
tcpdump:x:72:
zyz:x:1000:zyz
cephadm:x:1001:
nscd:x:28:
screen:x:84:
ldap:x:55:
ceph:x:167:
mysql:x:306:
#將test02用戶添加到mysql組中
[root@localhost ~]# ansible dbservers -m user -a 'name=test02 uid=306 system=yes group=mysql'
192.168.166.13 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "comment": "", "create_home": true, "group": 306, "home": "/home/test02", "name": "test02", "shell": "/bin/bash", "state": "present", "system": true, "uid": 306
}
#查看用戶所屬組
[root@localhost ~]# ansible dbservers -a 'tail /etc/passwd'
192.168.166.13 | CHANGED | rc=0 >>
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zyz:x:1000:1000:zyz:/home/zyz:/bin/bash
cephadm:x:1001:1001::/home/cephadm:/bin/bash
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
nslcd:x:65:55:LDAP Client User:/:/sbin/nologin
ldap:x:55:55:OpenLDAP server:/var/lib/ldap:/sbin/nologin
ceph:x:167:167:Ceph daemons:/var/lib/ceph:/sbin/nologin
test02:x:306:306::/home/test02:/bin/bash
[root@localhost ~]# ansible dbservers -a 'id test02'
192.168.166.13 | CHANGED | rc=0 >>
uid=306(test02) gid=306(mysql) 組=306(mysql)
[root@localhost ~]#
3.6 copy模塊
????????用于復制指定主機文件到遠程主機上
ansible-doc -s copy #查看相關文檔##常用參數
dest:指出復制文件的日標及位置,使用絕對路徑,如果是源目錄,指目標也要是目錄,如果目標文件已經存在會覆蓋原有的內容
src:指出源文件的路徑,可以使用相對路徑或絕對路徑,支持直接指定目錄,如果源是目錄則目標也要是目錄
mode:指出復制時,目標文件的權限
owner:指出復制時,目標文件的屬主
group:指出復制時,目標文件的屬組
content:指出復制到目標主機上的內容,不能與src一起使用##測試創建文件并修改權限
ansible dbservers -a 'mkdir /test'
ansible dbservers -m copy -a 'src=/etc/passwd dest=/test/passwd.bak owner=root mode=640'
ansible dbservers -a 'ls -l /test'##測試創建文件并寫入內容
ansible dbservers -m copy -a 'content="this is test txt" dest=/test/test.txt'
ansible dbservers -a 'ls -l /test'
ansible dbservers -a 'cat /test/test.txt'
3.7 file模塊
????????設置文件屬性
ansible-doc -s file#修改文件的屬主屬組權限等
ansible dbservers -m file -a 'owner=zhangsan group=mysql mode=777 path=/opt/123.txt'
ansible dbservers -a 'ls -l /opt'##設置/opt/123.txt.bak 為 /opt/123.txt 的鏈接文件
ansible dbservers -m file -a 'path=/opt/123.txt.link src=/opt/123.txt state=link'ansible dbservers -m file -a 'path=/opt/abc.txt state=touch' #創建一個文件
ansible dbservers -m file -a 'path=/opt/abc.txt state=absent' #刪除一個文件
?3.7.1 修改屬主和屬組及權限
3.7.2 創建軟鏈接
3.7.3 創建文件,并刪除文件
3.8 hostname 模塊
????????用于管理遠程主機上的主機名
ansible dbservers -m hostname -a 'name=testhost'
?3.9 ping 模塊
ansible all -m ping
3.10 yum 模塊
????????在遠程主機上安裝與卸載軟件包
ansible-doc -s yumansible webservers -m yum -a 'name=httpd' #安裝服務
ansible webservers -m yum -a 'name=httpd state=absent' #卸載服務
3.11 service/systemd?模塊
????????用于在遠程主機上管理服務的運行狀態
ansible-doc -s service##常用的參數
name:被管理的服務名稱。
state=started | stopped | restarted:動作包含啟動關閉或者重啟。
enabled=yes | no:表示是否設置該服務開機自啟。
runlevel:如果設定了enabled開機自啟去,則要定義在哪些運行目標下自啟動。ansible webservers -m service -a 'name=httpd enabled=true state=started' #安裝服務并設為開機自啟
systemctl is-enabled httpd.service #被控制端查看是否設為開機自啟
3.12 script 模塊
????????實現遠程批量運行本地 shell 腳本
ansible-doc -s scriptvim test.sh #編寫一個腳本
#!/bin/bash
echo "hello ansible from script" > /opt/script.txt #在script.txt中寫入指定內容chmod +x test.sh #賦予權限
ansible dbservers -m script -a 'test.sh' #實現遠程運行本地的腳本
ansible dbservers -a 'cat /opt/script.txt' #查看生成的文檔內容
3.13 setup 模塊
????????facts組件是用來收集被管理節點信息的,使用 setup 模塊可以獲取這些信息
ansible-doc -s setupansible webservers -m setup #獲取webservers組主機的facts信息
ansible webservers -m setup -a 'filter=*ipv4' #使用filter可以篩選指定的facts信息