标签:停止 任务 hosts 本地 module 脚本 疑问 /tmp sbin
1.查看模块的命令:参数:creates=/etc 相当于条件测试 [ -d /etc ]||pwd
[root@m01 ~]# ansible oldboy -m command -a "pwd creates=/etc"
参数:removes=/root 相当于条件测试 [ -d /root ]&&ls /root
ansible oldboy -m command -a "ls /root removes=/root"
ansible oldboy -m shell -a "[ -d /etc ]||pwd"
参数:warn=False 忽略警告
[root@m01 ~]# ansible oldboy -m command -a "chmod 000 /etc/hosts warn=False"
更多官方链接: http://docs.ansible.com/ansible/latest/command_module.html
或ansible-doc -s command
2.2
特殊:不支持的东西,例如 > < | &等 $HOME,替代方案用 shell模块
ansible oldboy -m shell -a "ps -ef|grep ssh"
ansible oldboy -m shell -a "echo oldboy >/tmp/a.log"
替代方案:
ansible oldboy -m command -a "chmod 777 /etc/hosts warn=false"
ansible oldboy -m command -a "chmod 644 /etc/hosts warn=false"
ansible oldboy -m command -a "chown oldboy /etc/hosts warn=false"
ansible oldboy -m command -a "chown root /etc/hosts warn=false"
创建目录:mkdir /tmp/oldboy_dir
ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory"
递归设置权限:
ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory mode=644 recurse=yes"
创建文件:touch /tmp/oldboy_file
ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch"
删除文件:rm -f /tmp/oldboy_file
ansible oldboy -m file -a "dest=/tmp/oldboy_file state=absent"
创建链接文件:ln -s /etc/hosts /tmp/link_file
ansible oldboy -m file -a "src=/etc/hosts dest=/tmp/link_file state=link"
ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=000"
ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=ugo=rwx"
3.6 yum模块功能说明:
功能说明:yum包管理模块
官方链接:http://docs.ansible.com/ansible/latest/yum_module.html
ansible oldboy -m yum -a "name=nginx state=installed"
[root@nfs01 oldboy_dir]# rpm -qa nginx
nginx-1.10.2-1.el6.x86_64
###不要用yum卸载,用rpm -e卸载。
rpm -e --nodeps 包名 卸载
3.7 service模块功能说明:
功能说明:启动停止服务
官方链接:http://docs.ansible.com/ansible/latest/service_module.html
#相当于
#service crond stop|/etc/init.d/crond stop
#chkconfig crond off
ansible oldboy -m service -a "name=crond state=stop enabled=no"
#相当于/etc/init.d/crond start
chkconfig crond on
ansible oldboy -m service -a "name=crond state=started enabled=yes"
3.8 cron模块功能说明:
功能说明:管理定时任务条目信息模块
官方链接:http://docs.ansible.com/ansible/latest/cron_module.html
定时任务格式:
ansible oldboy -m cron -a "name=‘sync time‘ minute=00 hour=00 job=‘/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1‘"
#Ansible: sync time
00 00 * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
标签:停止 任务 hosts 本地 module 脚本 疑问 /tmp sbin
原文地址:http://blog.51cto.com/13667268/2131001