【ansible】基础常用模块

修改/etc/ansible/hosts中添加组

[A]
192.168.122.60
192.168.122.68

查看模块用法

ansible-doc -s 【模块名】
ansible-doc -s copy
ansible-doc -s fetch

shell模块

file模块

在file模块中,path、name、dest三个参数等价
owner修改属主 group修改属组 mode修改权限
创建文件
ansible A -m file -a "path=/root/helo state=touch"
删除文件
ansible A -m file -a "path=/root/helo state=absent"
创建目录
ansible A -m file -a "path=/root/dir-helo state=directory"
删除目录
ansible A -m file -a "path=/root/dir-helo state=absent"
创建硬连接
ansible A -m file -a 'src=/etc/fstab dest=/root/hard-symbolic state=hard'
创建软连接
ansible A -m file -a 'src=/etc/fstab dest=/root/soft-symbolic state=link'

copy模块

ansible a -m copy -a "src=/tmp/test dest=/root mode=777 backup=yes"

fetch模块

ansible a -m fetch -a "src=/tmp/test.txt dest=/root"
                       远程的机子上文件    ansible命令安装机的目录

unarchive解包模块

archive打包模块

hostname模块

更改后重启后主机名也生效,/etc/hostname生效,但是/etc/hosts下的不更改仍未原来主机名
ansible A -m hostname -a "name=test"

cron计划任务模块

创建一个名为warning-wall的计划任务,内容为周1~5每分钟执行一次wall命令进行内容为helo:warning的广播警告,执行后目标机用crontab -e进行验证
ansible A -m cron -a 'minute=* weekday=1,2,3,4,5 job="/usr/sbin/wall helo:warning" name=warning-wall'
禁用计划任务,即将目标机的计划任务注释掉,job必须指定
ansible A -m cron -a 'disabled=true job="/usr/sbin/wall helo:warning" name=warning-wall'
启用计划任务
ansible A -m cron -a 'disabled=false job="/usr/sbin/wall helo:warning" name=warning-wall'

yum管理包模块

state(安装/卸载,present/absent)
disable_gpg_check=yes 忽略gpg检查
yum更新缓存问题,导致安装失败(主机一般用yum clean all解决),这里用参数update_cache=yes
安装vsftpd及httpd
####
ansible A -m yum -a 'name=vsftpd,httpd state=present update_cache=yes disable_gpg_check=yes'

service服务管理模块

将httpd服务启动并设为开机启动
ansible A -m service -a 'name=httpd state=started enabled=true'

user用户管理模块

创建用户为nginx,shell类型为nologin,系统账号,工作目录/var/nginx,附加组为root,bin
ansible A -m user -a 'name=nginx shell=/sbin/nologin system=yes home=/var/nginx groups=root,bin'
删除用户及其家目录
ansible A -m user -a 'name=nginx state=absent remove=yes'

group模块

ansible A -m group -a 'name=nginx system=yes gid=666'

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值