ansible简单模块

ssh-copy-id -p22 172.16.1.41 将你的公钥拷贝到这台机器,登录的免密

ansible-doc -l是查看所有的模块

ansible 172.16.1.41

在ansible的配置文间中hosts中添加

[oldboy]

172.16.1.21 ansible_user=root ansible_password=123

172.16.1.31

172.16.1.61

ansible oldboy -m command -a “hostname”

-m 后接模块command

-a 后接参数

hostname是命令参数必须是linux的命令

还有 creates=/etc/rsyncd.conf 不存在则执行

ansible oldboy -m command -a “creates=/etc/rsyncd.conf hostname”

不存在/etc/rsyncd.conf则执行 hostname

removes=/etc/rsyncd.conf存在时执行

ansible oldboy -m command -a “creates=/etc/rsyncd.conf hostname”
在这里插入图片描述
ansible oldboy -m command -a " chdir=/tmp"切换目录

存在shell模块跟

script模块

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DmMqNIZQ-1589298789219)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\1589297190835.png)]

copy 模块

参数backup备份数据

owner属主权限

group 属组

mode 755

会覆盖的copy

ansible 172.16.1.7 -m copy -a “src=/tmp/1.txt dest=/tmp/2.txt”

备份不覆盖

ansible 172.16.1.7 -m copy -a “src=/tmp/1.txt dest=/tmp/2.txt backup=yes”

file模块

ansible 172.16.1.7 -m file -a “dest=/tmp/2.txt owner=oldboy group=oldboy mode=600”

创建 state=touch

ansible 172.16.1.7 -m file -a “dest=/tmp/2.txt state=touch owner=oldboy group=oldboy mode=600”

创建目录state=dir

ansible 172.16.1.7 -m file -a “dest=/tmp/oldboy state=dir owner=oldboy group=oldboy mode=600”

yum模块

state installed安装 absent卸载

ansible 10.0.0.7 -m yum -a ‘name=iftop state=installed skip_broken=yes’

查看是否安装

ansible 10.0.0.7 -m yum -a 'list=iftop ’

service 模块

state :stopped started restarted reloaded

enable:yes开启启动 no

ansible 10.0.0.7 -m service -a “name=crond state=stopped enable=no”

定时任务模块

cron 模块

分时日月周

minute=0-59 ** hour day month

就是同样的分时日月周 同样的格式minute=*/6 每6分钟执行一次

name 唯一性不会重复执行

ansible 10.0.07 -m cron -a " name = serv1 minute=0 hour=0 day=0 job=’/bin/sh /server/script/test.sh >/dev/null’ “ansible 10.0.07 -m cron -a " name = serv1 minute=0 hour=0 day=0 job=’/bin/sh /server/script/test.sh >/dev/null’ state=absent” 删除定时任务

ansible 10.0.07 -m cron -a " name = serv1 state=absent" 删除定时任务

ansible 10.0.07 -m cron -a " name = serv1 job =/bin/bash /script/test.sh disable=yes" 注释定时任务

你可能感兴趣的:(ansible简单模块)