ansible使用技巧

2022-08-24 00:30:13 字數 1734 閱讀 9661

一、常用模組

ansible-doc -l   # 列出 ansible 支援的模組

ansible-doc ping   # 檢視該模組幫助資訊

1

、command 作為 ansible 的預設模組,可以執行遠端許可權範圍所有的 shell 命令,不支援管道符

ansible client -m command -a "

free -m

"# 檢視 client 分組主機記憶體使用情況

2、script 的功能是在遠端主機執行主控端儲存的 shell 指令碼檔案,相當於 scp +shell 組合

ansible client -m script -a "

/home/test.sh 12 34

"# 遠端執行本地指令碼

3、shell模組基本和command相同,但是shell支援管道符

ansible client -m shell -a "

/home/test.sh

"# 執行遠端指令碼

4、copy模組實現主控端向目標主機拷貝檔案,類似於 scp 功能

ansible client -m copy -a "

src=/home/test.sh dest=/tmp/ owner=root group=root mode=0755

" # 向 client 組中主機拷貝 test.sh 到 /tmp 下,屬主、組為 root ,許可權為 0755

5、stat模組

獲取遠端檔案狀態資訊,atime/ctime/mtime/md5/uid/gid 等資訊

ansible client -m stat -a "

path=/etc/syctl.conf"6

、yum模組軟體包管理

ansible client -m yum -a "

name=curl state=latest"7

、corn模組,遠端主機 crontab 配置

ansible client -m cron -a "

name='check dirs' hour='5,2' job='ls -alh > /dev/null'

"效果:* 5,2 * * * ls -alh > /dev/null

8、mount模組,遠端主機分割槽掛載

ansible client -m mount -a "

name=/mnt/data src=/dev/sd0 fstype=ext4 opts=ro state=present"9

、service模組,遠端主機系統服務管理

ansible client -m service -a "

name=nginx state=stoped

"ansible client -m service -a "

name=nginx state=restarted

"ansible client -m service -a "

name=nginx state=reloaded"10

、user模組

ansible client -m user -a "

name=wang comment='user wang'

"ansible client -m user -a "

name=wang state=absent remove=yes

" # 新增刪除使用者

更多詳細使用方法:

為何使用Ansible

b size large it 自動化 size b 現在市面上用一些實現it自動化的工具,例如 puppet,chef,salt。ansible 是乙個相對比較新的工具,但目前社群十分活躍。我用過puppet和ansible。這裡想討論一下我偏愛ansible的原因。b size large 架構...

ansible使用文件

假設a機器上安裝ansible yum install ansible vim etc ansible hosts 對每個主機加key認證 ssh copy id i ssh id rsa.pub root b ip ansible all m ping 乙個playbook檔案可以引入其他的yml...

Ansible 簡單使用

安裝 rpm uvh yum install y ansible 配置hosts vi etc ansible hosts aly ansible ssh port 10011 ansible ssh host 127.0.0.1 kaiping ansible ssh port 10013 ans...