ansible常用模块

常用模块命令详细介绍:

目录

常用模块命令详细介绍:

command

raw

script

shell

copy

fetch

file

yum

service

archive

unarchive

replace

parted模块

filesystem

mount--挂载

lvg—新建卷组

lvol—新建逻辑卷

sefcontext—修改context值

debug

cron--计划任务模块

get_url

selinux


command

执行远程命令,不支持管道,重定向,默认模块

ansible webservers -m command -a 'uname -n'

raw

 类似于command模块、支持管道传递

ansible webservers -m raw -a 'ip a |grep "192.168"'

script

在远程主机执行主控端的shell/python脚本

ansible webservers -m script -a '/etc/ansible/test.sh'

shell

 执行远程主机的shell/python脚本或命令

 ansible webservers -m shell -a 'bash /root/test.sh'

copy

拷贝文件---将本机文件拷贝到远程服务器上

ansible webservers  -m copy -a "src=/etc/hosts dest=/tmp/hosts"

fetch

拷贝文件---将远程服务器文件拷贝到本机上,注意:放置到本机上时为目录,为了防止相互覆盖。

ansible webservers -m fetch -a "src=/root/123 dest=/root"

file

 不可以创建文件

模块可以做到修改文件的属主和权限,(在这里可替换为 copy 模块,是等效的):

ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600" 

ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=root group=root"

使用 file 模块也可以创建目录,与执行 mkdir -p 效果类似:

ansible webservers -m file -a "dest=/path/to/c mode=755 owner=root group=root state=directory"

删除目录(递归的删除)和删除文件:

 ansible webservers -m file -a "dest=/path/to/c state=absent"

yum

 state:状态(present,absent,latest),表示是安装还卸载

   present:默认的,表示为安装

   latest: 安装为最新的版本

   absent:表示删除

确认一个软件包已经安装,但不去升级它:

 ansible webservers -m yum -a "name=lftp state=present"

确认一个软件包的安装版本:

ansible webservers -m yum -a "name=lftp -1.5 state=present"

确认一个软件包还没有安装:

ansible webservers -m yum -a "name=lftp  state=absent"

service

确认某个服务在所有的webservers上都已经启动:

ansible webservers -m service -a "name=httpd state=started"

在所有的webservers上重启某个服务

ansible webservers -m service -a "name=httpd state=restarted enabled=yes"

确认某个服务已经停止:

ansible webservers -m service -a "name=httpd state=stopped"

archive

块可以做到压缩文件

archive value of format must be one of: bz2, gz, tar, xz, zip

ansible test4 -m archive -a 'path=/etc dest=/tmp/newetc.tar.gz'

path=指定路径

dest=压缩存放到的路径下

unarchive

可以做到传送压缩文件的同时可以解压完成传送过去

[root@ansible ~]# ansible test4 -m unarchive -a 'src=nginx-1.24.0.tar.gz dest=/usr/local/src/'

src=指定文件

dest=存放后的目录下

replace

replace模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配的字符串都会被替换

参数:

path参数:2.3版本之前只能用dest、destfile、name指定操作文件,2.4版本中仍然可以用这些参数名,也可以用path

regexp参数:必须参数,指定一个python正则表达式,文件中与正则匹配的字符串将会被替换 replace参数:指定最终要替换成的字符串

backup参数:是否在修改文件之前对文件进行备份,最好设置为yes

将/tmp/hehe文件中的“abc”替换成“yyy”
ansible all -m replace -a 'path=/tmp/hehe regexp="abc" replace="yyy"'

将/tmp/hehe文件中的“yyy”替换成“iii”,且把替换前的/tmp/hehe文件备份
ansible all -m replace -a 'path=/tmp/hehe regexp="yyy" replace="iii" backup=yes'

parted模块

新建扩展分区

ansible node1 -m parted -a 'device=/dev/sda number=4 part_type=extended part_start=50GiB part_end=51GiB state=present'

新建逻辑分区

ansible node1 -m parted -a 'device=/dev/sda number=5 part_type=logical part_start=40GiB part_end=45GiB state=present'

filesystem

文件系统

ansible node1 -m filesystem -a 'fstype=xfs dev=/dev/sda5'

mount--挂载

新建挂载点/haha1
ansible node1 -m file -a 'path=/haha1 state=directory'
 
查看/dev/sda5的UUID
ansible node1 -m shell -a 'blkid /dev/sda5'
 
将分区/dev/sda5挂载到/haha1目录
ansible node1 -m mount -a 'path=/haha1 src="UUID=d162b8b9-4ee4-a559-80861461c4-2326f0" fstype=xfs state=mounted'
 
卸载
ansible node1 -m mount -a 'path=/haha1 src="UUID=d162b8b9-4ee4-a559-80861461c4-2326f0" fstype=xfs state=absent'

lvg—新建卷组

ansible node1 -m lvg -a 'vg=vg0 pesize=16M pvs=/dev/sda5'

lvol—新建逻辑卷

ansible node1 -m lvol -a 'lv=lv0 size=1000M vg=vg0'
 
在线扩容逻辑卷
ansible node1 -m lvol -a 'lv=lv0 size=1600M vg=vg0 resizefs=yes'

sefcontext—修改context值

ansible node1 -m file -a 'path=/hehe2 state=directory'
 
修改context值
ansible node1 -m sefcontext -a 'target="/hehe2(/.*)?" setype=samba_share_t state=present'
应用新的selinux 文件的context值
ansible node1 -m command -a 'restorecon -Rv /hehe2'

debug

用户输出自定义的信息,类似于echo、print等输出命令。ansible中的debug主要用于输出变量值、表达式值,以及用于when条件判断时。使用方式非常简单 msg 打印的自定义消息 var 要调试的变量名。与msg选项互斥。 verbosity 一个控制调试运行时间的数字,如果设置为3,则仅在-vvv或更高版本时运行调试

cron--计划任务模块

可以创建周期任务

执行任务计划

minute 分 minute= 每分钟

hour 时 special_time=hourly 每小时

day 天

month 月

weekday 周

job 计划任务的工作

name= 计算任务名称

disbaled=true 禁用某个计划任务

disabled=false 再次开启某个计划任务

state=absent 删除某个计划任务

ansible test4 -m cron -a 'name=ntpdate minute=5 hour=1 job=/root/crontsh.sh'
ansible node1 -m cron -a 'name="renwu" job="echo hello world"  user=root minute=0 hour=14 state=present'

关闭周期任

ansible test4 -m cron -a 'name=ntpdate minute=5 hour=1 job=/zlj.sh disabled=yes'

disabled决定着周期任务存活

get_url

语法:ansible node1 -m get_url -a 'url=需要下载的文件 dest=存放的位置'

selinux

selinux模块起到关闭开启作用

设置为selinux为disable

[root@ansible ~]# ansible test4 -m selinux -a 'state=disabled' 

state=指定开启关闭样子

设置为enforcing

[root@xuzhichao ~]# ansible 192.168.20.23 -m selinux -a 'state=enforcing policy=targeted'
[root@nginx03 ~]# grep "^SELINUX" /etc/selinux/config
 SELINUX=enforcing
 SELINUXTYPE=targeted

firewalld

模块开启端口开关防火墙

开启防火墙

[root@master ~]# ansible webservers -m service -a 'name=firewalld state=started enabled=true'

允许http服务

[root@master ~]# ansible webservers -m firewalld -a 'service=http permanent=true immediate=true state=enabled'

允许端口

[root@master ~]# ansible webservers -m firewalld -a 'port=80/tcp permanent=true immediate=true state=enabled'

富规则

[root@master ~]# ansible webservers -m firewalld -a 'rich_rule="rule family=ipv4 source address=192.168.150.0/24 service name=http accept" permanent=true immediate=true state=enabled'

端口转发

[root@master ~]# ansible webservers -m firewalld -a 'rich_rule="rule family=ipv4 forward-port port=443 protocol=tcp to-port=8443" permanent=true immediate=true state=enabled'

masquerade

[root@master ~]# ansible webservers -m firewalld -a 'mquerade=yes‘

group

参数:gid,name,system,state,state=,state=

选项/默认值: system yes/no ,state present(默认) ,state absent

说明: gid=创建的组id信息

name=创建组名称信息

system= system=yes表示创建系统组

state=组状态

state=创建指定的用户组

state=删除指定的用户组

创建一个指定用户组123 gid=1022

ansible test4 -m group -a 'name=123 gid=1022'

删除一个指定用户子

ansible test4 -m group -a 'name=123 state=absent'

user

参数 解释 comment 用户的备注,相当于useradd -C group 用户的主组 groups 用户的附加组 create_home 要不要创建home目录 home 指定用户的home目录 system 是否创建系统账号(shell=nologin) uid 指定账号的uid

password password: "{{ ‘密码’

添加用户

ansible test4 -m user -a 'name=www system=yes shell=/bin/bash/ create_home=no'

删除用户

ansible test4 -m user -a 'name=www state=absent'

临时命令使用user模块来确保haha用户存在于node1上,并且其UID为2000

[student@ansible ~]$ ansible node1 -m user -a 'name=haha uid=2000 state=present'

创建用户并指定密码,如果该用户存在,仍然修改密码

[student@ansible ~]$ openssl passwd -1 haha
........
[student@ansible ~]$ ansible all -m user -a 'name=hehe state=present password=".............." update_password=always'

创建用户并指定密码,但是如果改用户存在,则不修改密码

[student@ansible ~]$ openssl passwd -1 redhat
.........
[student@ansible ~]$ ansible all -m user -a 'name=hehe state=present password="............."  update_password=on_create'

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值