天天看點

Ansible常用子產品

Ansible常用子產品

Ansible子產品 command 子產品 不支援管道符等特殊字元,用于執行系統指令,不僅限于linux。和shell子產品差不多。 [root@m01 ~]# ansible web_group -m shell -a 'df -h' [root@m01 ~]# ansible web_group -

Ansible子產品

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      

檔案管理子產品

copy 子產品

相當于scp

[root@m01 ~]# ansible-doc copy

[root@m01 ~]# ansible web_group -m copy -a 'src=/root/fenfa.sh dest=/root/fenfa_miyue.sh owner=root group=root mode=644'

src		# 指定原檔案
dest	# 指定目标位置及檔案名,可以改名
owner	# 屬主
group	# 指定屬組
mode	# 指定權限

[root@m01 ~]# ansible web_group -m copy -a 'src=/root/fenfa.sh dest=/root/fenfa_miyue.sh owner=root group=root mode=644 backup=yes'

backup 	# 如果原檔案變了去覆寫已經有的檔案,會把目标機的檔案備份一遍,預設不開啟。

[root@web03 ~]# ll
total 16136
-rw-------. 1 root root     1444 Apr 30 20:47 anaconda-ks.cfg
-rw-r--r--. 1 root root      287 May  1 00:05 change_ip.sh
-rw-r--r--  1 root root      149 Jun  9 09:51 fenfa_miyue.sh
-rw-r--r--  1 root root      152 Jun  9 09:34 fenfa_miyue.sh.11536.2020-06-09@09:51:56~

# 復原
[root@m01 ~]# ansible web_group -m copy -a 'src=/root/fenfa_miyue.sh.12398.2020-06-09@09:51:56~ dest=/root/fenfa_miyue.sh remote_src=yes'

remote_src=yes    # 是否在遠端主機操作

[root@m01 ~]# ansible web_group -m copy -a 'content=gong:123 dest=/root/rsync.pass owner=root group=root mode=600'

content=gong:123  # 把裡面的内容寫到,目标主機指定檔案中。      

file 子產品

作用:

  • 建立目錄
  • 建立檔案
  • 建立軟連結
  • 删除目錄、檔案,軟連結
# 建立目錄
[root@m01 ~]# ansible all -m file -a 'path=/opt/test owner=root group=root mode=755 state=directory'
path		# 指定檔案或者目錄
owner		# 指定屬主
group		# 指定屬組
mode		# 指定權限
src			# 做軟、硬連結的時候使用
recurse		# 遞歸授權
	yes
	no
state:	
	absent		# 
	touch		# 建立檔案
	directory	# 目錄
	link		# 軟連結
	hard		# 硬連接配接
	file		# 配合
# 遞歸授權
[root@m01 ~]# ansible web_group -m file -a 'path=/website owner=www group=www recurse=yes'      

get_url 子產品

[root@m01 ~]# ansible web_group -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-agent-3.4.0-1.el7.x86_64.rpm dest=/tmp mode=0644' -i ./hosts

url             #指定下載下傳位址
dest            #指定下載下傳的目錄
mode            #指定權限
checksum        #校驗加密算法
    md5
    sha256      

service 子產品

systemd

[root@m01 ~]# ansible web_group -m service -a 'name=crond state=started'

name		# 服務名
state
	stopped		# 停止服務
	started		# 開始服務
enabled
	yes
	no      

user 子產品

[root@m01 ~]# ansible all -m user -a 'name=john uid=1024 group=root shell=/sbin/nologin create_home=false'

name	# 使用者名
uid		# 指定uid
group	# 指定屬組
shell	# 指定解釋器
create_home		# 是否建立家目錄
	yes
	no
comment		# 指定注釋 -c
groups		# 指定附加組,配合append,如果不加就會覆寫-G
append		# 附加組 -a
remove		# 删除使用者的時候,是否同時删除家目錄
	true,yes	# 删除
	flase,no	# 不删除
state
	present		# 建立
	absent		# 删除
    
generate_ssh_key: 是否建立密鑰對
	yes		# 建立
	no		# 不建立
ssh_key_bits	# 指定密鑰的加密長度
ssh_key_file	# 指定私鑰檔案的位置

system			# 是否是系統使用者 -r
	yes			是
	no			不是      

group 子產品

# 添加組
[root@m01 ~]# ansible all -m group -a 'name=xxxx gid=897 state=present'      

cron 子產品

# 建立定時任務
[root@m01 ~]# ansible all -m cron -a "name='sync time' minute=*/5 job='ntpdate ntp1.aliyun.com &> /dev/null'"

# 效果
[root@web03 ~]# crontab -l
#Ansible: sync time
*/5 * * * * ntpdate ntp1.aliyun.com &> /dev/null

# 删除定時任務
[root@m01 ~]# ansible all -m cron -a "name='sync time' state=absent"

name		# 定時任務的名字,備注,删除的時候是用它來注釋的。
state
	present
	absent
minute	分0-59
hour	時0-23
day		日1-31
month		月1-12
weekday		周0-6      

mount 子產品

[root@m01 ~]# ansible web_group -m mount -a 'path=/website/wp/wp-content/uploads src=172.16.1.31:/data/wp_data fstype=nfs state=mounted'

path		# 挂載到本地的路徑
src			# 挂載源
fstype		# 挂載的檔案系統
	nfs
	ext4
	ext3
state
	present		# 隻寫檔案,挂載的東西寫到/etc/fstab
		推薦使用  :mounted		# 即寫入檔案,又挂載
		推薦使用  :	absent		# # 解除安裝裝置,并清理開機自動挂載檔案
	unmounted		# 隻會解除安裝,不會清除/etc/fstab      

slinux 子產品

# 關閉selinux
[root@m01 ~]# ansible all -m selinux -a 'state=disabled'

state
	enforcing
	permisive
	disabled      

firewalld 子產品

# 指定服務的方式去開放
[root@m01 ~]# ansible all -m firewalld -a 'service=httpd permanent=no state=enabled'
[root@m01 ~]# ansible nfs -m firewalld -a 'port=111/tcp permanent=no state=enabled'

service		# 需要控制的服務
port		# 需要設定的端口
permanent	# 是否臨時生效
state		# 讓這個服務通行,開啟或者關閉
	enabled
	disabled      

setup 子產品

[root@m01 ~]# ansible web01 -m setup      

繼續閱讀