Ansible模块

模块
ansible-doc和ping模块
command模块
raw模块
shell模块
script模块
copy模块
lineinfile-replace模块
yum模块
service模块
setup模块

模块

ansible-doc和ping模块

  • ansible-doc

模块的手册相当与shell的man,很重要

[root@web1 ~]# ansible-doc -l			# 列出所有模块
[root@web1 ~]# ansible-doc -l | wc -l
1378
[root@web1 ~]# ansible-doc yum			# 查看帮助

在这里插入图片描述
在这里插入图片描述

  • ping模块

测试网络连通性,ping模块没有参数
注:测试ssh的连通性

[root@web1 ~]# ansible web -m ping -k
# ping检查的是ssh
# 如果把22端口关闭,就会报错

command模块

  • command模块

默认模块,远程执行命令
用法

[root@web1 ~]# ps -ef | grep ssh

在这里插入图片描述

[root@web1 ~]# ansible xl -m command -a 'ps -ef'

在这里插入图片描述

[root@web1 ~]# ansible xl -m command -a 'ps -ef | grep ssh'

在这里插入图片描述

[root@web1 ~]# ansible xl -m shell -a 'ps -ef | grep ssh'

在这里插入图片描述
查看所有机器负载

[root@web1 ~]# ansible all -m command -a 'uptime'

在这里插入图片描述
查看日期和时间

[root@web1 ~]# ansible all -m command -a 'date'

在这里插入图片描述

command模块( 续1 )

  • command模块注意事项:

该模块通过-a跟上要执行的命令可以直接执行,若命令里有如下字符则执行不成功
"<", “>”, “|”, "&"
该模块不启动shell直接在ssh进程中执行, 所有使用到shell的命令执行都会失败
下列命令执行会失败

[root@web1 ~]# ansible all -m command -a 'ps auxlgrep ssh'
[root@web1 ~]# ansible all -m command -a 'set'

shell 模块

  • shell 模块

shell模块用法基本和command一样,区别是shell模块是通过/bin/sh进行执行命令,可以执行任意命令
这里有个坑!

[root@web1 ~]# pstree -p
[root@web1 ~]# echo ${HOSTNAME}
[root@web1 ~]# ansible xl -m command -a 'echo ${HOSTNAME}'
[root@web1 ~]# ansible xl -m command -a "echo ${HOSTNAME}"

在这里插入图片描述

[root@web1 ~]# ansible all -m shell -a "echo ${HOSTNAME}"

在这里插入图片描述
这就是坑的所在

[root@web1 ~]# ansible all -m shell -a 'echo ${HOSTNAME}'

在这里插入图片描述

raw 模块

  • raw模块

raw模块,用法和shell模块一样,可以执行任意命令
区别是raw没有chdir、creates、 removes参数
执行以下命令查看结果

ansible t1 -m command -a 'chdir=/tmp touch f1'
ansible t1 -m shell -a 'chdir=/tmp touch f2'
ansible t1 -m raw -a 'chdir=/tmp touch f3'

在 web1, mysql2创建用户 oo
修改 oo 的密码为123456

[root@web1 ~]# ansible web1,mysql2 -m shell -a 'useradd oo'
[root@web1 ~]# ansible web1,mysql2 -m shell -a 'echo 123456 | passwd --stdin oo'

在这里插入图片描述

script模块

  • script模块

思考:
在所有web主机上创建用户 xx
修改xx 的密码为123123
要求:xx与oo不能同时出现在同一个主机上

[root@web1 ~]# vim ooxx.sh
id oo
if [ $? != 0 ]; then
	useradd xx
	echo 123123 | passwd --stdin xx
fi
[root@web1 ~]# ansible web -m script -a './ooxx.sh'

在这里插入图片描述

[root@web1 ~]# id oo
[root@web1 ~]# id xx

在这里插入图片描述

copy模块

  • copy模块

复制文件到远程主机
src :复制远程主机的文件到本地,绝对路径和相对路径都可,路径为目录时会递归复制。若路径以"/“结尾,只复制目录里的内容,若不以”/"结尾,则复制包含目录在内的整个内容,类似于rsync
dest :必选项。远程主机的绝对路径,如果源文件是一个目录,那该路径必须是目录

copy模块( 续1 )

  • copy模块

backup :覆盖前先备份原文件,备份文件包含时间信息。有两个选项: yes | no
force :若目标主机包含该文件,但内容不同,如果设置为yes ,则强制覆盖,设为no ,则只有当目标主机的目标位置不存在该文件时才复制。默认为yes
复制文件
ansible t1 -m copy -a 'src=/root/alog dest=/root/a.log
复制目录
ansible t1 -m copy -a 'src=urdir dest=/root/'

[root@web1 ~]# vim /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.199.2
[root@web1 ~]# ansible all -m copy -a 'src=/etc/resolv.conf dest=/etc/resolv.conf'

在这里插入图片描述

[root@web1 ~]# ansible all -m shell -a 'cat /etc/resolv.conf'

在这里插入图片描述

lineinfile | replace模块

  • lineinfile | replace模块
ansible all -m lineinfile -a 
	'path    # 指定要修改的文件
	regexp   # 匹配要修改的行
	line     # 最终修改后的样子'
[root@xl ~]# vim ifconfig
DEVICE=lo
IPADDR=127.0.0.1
NETMASK=255.0.0.0
NETWORK=127.0.0.0
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback
[root@web1 ~]# ansible xl -m shell -a 'cat /root/ifconfig'

在这里插入图片描述

[root@web1 ~]# ansible xl -m lineinfile -a 'path=/root/ifconfig regexp="^BROADCAST" line="BROADCAST=\"192.168.199.255\""'
[root@web1 ~]# ansible xl -m shell -a 'cat /root/ifconfig'

在这里插入图片描述

ansible all -m replace -a '
	path    	# 指定要修改的文件
	regexp  	# 匹配要修改的字符串
	replace		# 最终的字符串
[root@web1 ~]# ansible xl -m replace -a 'path=/root/ifconfig regexp="\.199\.255" replace=".199.1"'
[root@web1 ~]# ansible xl -m shell -a 'cat /root/ifconfig'

在这里插入图片描述

yum模块

  • yum模块

使用yum包管理器来管理软件包
config_file : yum的配置文件
disable_gpg_check :关闭gpg_check
disablerepo : 不启用某个源
enablerepo :启用某个源
name : 要进行操作的软件包名字,也可传递一个url或个本地的rpm包的路径
state :状态( present , absent , latest )

yum模块( 续1 )

  • yum模块

删除软件包
ansible xl -m yum -a name= “Irzsz” state = absent’

删除多个软件包
ansible xl -m yum -a name= “Irzsz,lftp” state = absent’

安装软件包
ansible xl -m yum -a name= "Irzsz"

安装多个软件包
ansible xl -m yum -a name= "Irzsz,Iftp"

ansible mysql -m yum -a 'state=动作 name=包名'
					  installed,removed
[root@web1 ~]# ansible mysql -m yum -a 'state=installed name=mariadb-server'

在这里插入图片描述

service模块

  • service模块

name:必选项,服务名称
enabled :是否开机启动yes|no
sleep :执行restarted ,会在stop和start之间沉睡几秒钟
state :对当前服务执行启动,停止、重启、重新加载等操作( started , stopped , restarted , reloaded )

ansible mysql -m yum -a 'state=动作 name=服务名 enabled=yes|no'
						started,stopped
[root@web1 ~]# ansible mysql -m service -a 'state=started name=mariadb enabled=yes'

在这里插入图片描述

[root@web1 ~]# ansible mysql -m shell -a 'ss -untlp | grep 3306'

在这里插入图片描述

setup模块

  • setup模块

主要用于获取主机信息, playbooks里经常会用的另一个参数gather_ facts与该模块相关, setup模块下经常用的是filter参数
filter过滤所需信息

[root@web1 ~]# ansible xl -m setup

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值