Ansible ad-hoc
什么是ad-hoc?
臨時命令,執行完不會保存,類似於批量執行命令。
ansible的選項
-i # 指定主機清單
ansible rsync -m ping -i 1.txt
-m # 指定模塊
-a # 指定動作
[root@m01 ~]# ansible nfs_group -a 'df -h'
[root@m01 ~]# ansible nfs_group -m shell -a 'df -h'
ad-hoc返回的顏色
- 綠色:被管理的主機沒有發生修改
- 黃色:被控主機發生了變更
- 粉色:ansible發出的警告
- 紅色:報錯
查看幫助文檔
[root@m01 ~]# ansible-doc shell
# shell 是模塊的名稱,想查看什么模塊直接在后面接上即可。
ad-hoc模塊
command 模塊
不支持管道符等特殊字符,用於執行系統命令,不僅限於linux。和shell模塊差不多。
[root@m01 ~]# ansible web_group -m shell -a 'df -h'
[root@m01 ~]# ansible web_group -m command -a 'df -h'
shell 模塊
用於執行命令
[root@m01 ~]# ansible web_group -m shell -a 'df -h'
script 模塊
在本地執行的腳本,功能可以同步到其它機器上面去,被控制機器不需要腳本。
[root@m01 ~]# ansible web01 -m script -a '/root/a.sh'
yum 模塊
# 查看幫助
[root@m01 ~]# ansible-doc yum
# 安裝httpd
[root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=latest'
[root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=present'
# 一般使用present
# 卸載httpd
absent
[root@m01 ~]# ansible web_group -m yum -a 'name=httpd state=absent'
# 指定網絡的rpm包
[root@m01 ~]# ansible web_group -m yum -a 'name=https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.1-1.el7.x86_64.rpm state=present'
# 類似於
yum -y install https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/SRPMS/zabbix-4.4.1-1.el7.src.rpm
# 在db組安裝nginx,前提是db組上的主機有本地安裝的包。
[root@m01 ~]# ansible db_group -m yum -a 'name=/root/nginx-1.18.0-1.el7.ngx.x86_64.rpm state=present'
需要掌握的方法
name:
指定安裝的軟件。
可以是本地的也可以是遠程的鏈接包
state:
prsent 正常安裝,類似於yum -y install
absent 刪除、卸載
lastet 最新版本
yum_repository 模塊
[root@m01 ~]# ansible-doc yum_repository
[root@m01 ~]# ansible web_group -m yum_repository -a 'name=gong description=gong baseurl=zp.gong.com gpgcheck=0 enabled=no'
# web01生成的文件
[root@web01 ~]# cat /etc/yum.repos.d/gong.repo
[gong]
baseurl = zp.gong.com
enabled = 0
gpgcheck = 0
name = gong
# file指的是yum倉庫的文件名,gong是[ ] 里面的內容,des是name的內容
[root@m01 ~]# ansible web_group -m yum_repository -a 'file=nginx name=gong description=gong baseurl=zp.gong.com gpgcheck=0 enabled=no'
# web01生成的文件
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[gong]
baseurl = zp.gong.com
enabled = 0
gpgcheck = 0
name = gong
# 刪除yum倉庫文件,最好是把文件名和倉庫名都加上,防止誤刪。
[root@m01 ~]# ansible web_group -m yum_repository -a 'file=nginx name=gong state=absent'
# 直接在文件里面添加倉庫,file不變,其它參數改變就會加上
[root@m01 ~]# ansible web_group -m yum_repository -a 'file=nginx name=liao description=liao baseurl=tencent.gong.com gpgcheck=0 enabled=yes'
[root@web01 /etc/yum.repos.d]# cat nginx.repo
[xiao]
baseurl = qq.gong.com
enabled = 1
gpgcheck = 0
name = xiao
[liao]
baseurl = tencent.gong.com
enabled = 1
gpgcheck = 0
name = liao
name #指定倉庫名,如果沒有file,則倉庫名為文件名
baseurl #指定yum源
description # yum配置文件中的name
gpgcheck #指定檢查秘鑰,也可用數字
no
yes
enabled #是否啟用倉庫
no
yes
