修改/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'