#参数 enabled #加入开机自启 noyes name #服务名 state started #启动服务 stopped #停止服务 restarted #重启服务 reloaded #重载服务 enabled #开机自启 #启动服务 ansible 192.168.8.32 -m service -a 'name=nginx state=started' #查看 ansible 192.168.8.32 -m shell -a 'netstat -tunpl' #停止服务 ansible 192.168.8.32 -m service -a 'name=nginx state=stopped' #将nginx服务加入开机自启 ansible 192.168.8.32 -m service -a 'name=nginx enabled=yes'
cron模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#参数 day #天 disabled #禁用 hour #小时 minute #分组 month #月份 job #任务 name #任务名 weekday #周 #创建计划任务 ansible 192.168.8.32 -m cron -a 'minute=49 job="touch /root/cron_test.txt" name=crontest' #删除计划任务 ansible 192.168.8.32 -m cron -a 'name=crontest state=absent' #禁用计划任务 # 表示禁用 ansible 192.168.8.32 -m cron -a 'minute=49 job="touch /root/cron_test.txt" name=crontest2 disabled=yes' #启动禁用任务 ansible 192.168.8.32 -m cron -a 'minute=49 job="touch /root/cron_test.txt" name=crontest2 disabled=no'
管理员 普通用户 系统用户 #参数 group #组 groups #附加组 home #家目录 name #用户名 password #密码 remove shell system state #状态 #创建用户mysql 指定家目录/root/mysql 指定附加组root 指定uid 指定shell不能登录 ansible db -m user -a 'name=mysql uid=4000 home=/root/mysql groups=root shell=/sbin/nologin' #删除mysql用户但不删除此用户家目录 ansible db -m user -a 'name=mysql state=absent' #删除mysql用户并删除此用户家目录 ansible db -m user -a 'name=mysql state=absent remove=yes'
group模块
1 2 3 4 5 6 7 8 9 10 11
#参数 gid #组id name #组名 system #系统组 state #创建mysql系统组 ansible db -m group -a 'name=mysql system=yes' #删除mysql系统组 ansible db -m group -a 'name=mysql state=absent'
# This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character 用#注释 # - Blank lines are ignored 空行可被忽略 # - Groups of hosts are delimited by [header] elements 组用[]命名且在此下面 # - You can enter hostnames or ip addresses # - A hostname/ip can be a member of multiple groups 一个主机可以在多个组中
ansible web -m command -a 'ls /' #默认是command ansible web -a 'ls /' # 可以简写 #参数 chdir #切换目录 ansible web -a 'chdir=/tmp pwd' #编译安装时
creates #如果文件或文件夹不存在就执行 否则跳过 ansible web -a 'creates=/tmp2 pwd'
removes #如果文件或文件夹存在就执行 否则跳过 ansible web -a 'removes=/tmp pwd'
例子
1 2 3 4 5 6 7
#批量创建用户 ansible web -a 'useradd ropon' #查看是否创建成功 ansible web -a 'id ropon' #批量设置密码 ansible web -a 'echo "west.cn"passwd --stdin ropon' #因command模块不支持特殊字符 < > ; & 所以批量设置密码不成功 需要使用shell模块
ansible db -m copy -a 'src=/root/test.sh dest=/root/test_copy.sh mode=744' #带权限复制
ansible web -m copy -a 'src=/root/test.sh dest=/root/test_copy.sh group=ropon' #带所需组复制 ansible web -m copy -a 'src=/root/test.sh dest=/root/test_copy.sh owner=ropon' #带所有者复制 #复制本地目录到远程主机 ansible web -m copy -a 'src=/etc/init.d dest=/tmp/' #复制本地目录下所有文件到远程主机 ansible web -m copy -a 'src=/etc/init.d/ dest=/tmp/' #将内容写入到远程主机 ansible web -m copy -a 'content="测试将内容写入远程主机\n" dest=/tmp/b.txt'