博客文章除注明转载外,均为原创。转载请注明出处。
本文链接地址:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=31396856&id=5778127
1.ansible配置文件和执行程序
/etc/ansible/ansible.cfg //配置文件
/etc/ansible/hosts //静态主机文件
/etc/ansible/roles //角色
/usr/bin/ansible //主程序
/usr/bin/ansible-console
/usr/bin/ansible-doc //文档命令
/usr/bin/ansible-galaxy
/usr/bin/ansible-playbook //剧本
/usr/bin/ansible-pull
/usr/bin/ansible-vault
2.ansible管理命令
语法:
ansible [options]
常见模块:command、shell、copy、cron、yum 、service
# 检查主机连接
# ansible db-server -m ping
# 执行远程命令
# ansible db-server -m command -a 'uptime'
# 执行脚本
# ansible db-server -m script -a '/etc/ansible/script/test.sh'
# 执行远程主机的脚本
# ansible db-server -m shell -a 'ps aux|grep zabbix'
# 类似shell,使用管道
# ansible db-server -m raw -a "ps aux|grep zabbix|awk '{print \$2}'"
# 远程执行脚本
ansible db-server -m script -a "/opt/app/target.sh"
# 创建用户/组
ansible all -m user -a "name=test password="
ansible all -m user -a "name=test state=absent"
# 服务管理
#确定服务都是开启的
ansible all -m service -a "name=mysql state=started"
#重启服务
ansibel all -m service -a "name=mysql state=restarted"
#关闭服务
ansible all -m service -a "name=mysql state=stoped"
#文件管理
# 创建软链接
# ansible db-server -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link"
# 删除软链接
# ansible db-server -m file -a "path=/tmp/resolv.conf state=absent"
# 复制文件到远程服务器
# ansible db-server -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"
#创建和删除计划任务
语法:ansible [group or ip] -m cron -a "name='....' job='.....' $time"
-m 跟模版,使用的是 cron 模版
name= 指定计划任务的名字,方便日后管理
job= 指定具体的任务
$time 指定具体的执行时间,minute分钟,hour小时,day 月份,month 月份。weekday 0 或者 7 代表周末。
state= 选项用来指定 name 并删除
增加计划任务
# ansible testhosts -m cron -a "name='test cron' job='/bin/bash /tmp/test.sh' weekday=6"
# ansible testhosts -m cron -a "name='test n' job='/bin/bash /tmp/test.sh' weekday=6 minute=30 hour='*/8'"
# ansible testhosts -m cron -a "name='test cron' job='/bin/bash /tmp/test.sh' minute=30 hour='*/8' day='1,10,20' weekday=6"
删除计划任务
# ansible testhosts -m cron -a "name='test n' state=absent"
# ansible testhosts -m cron -a "name='test cron' state=absent"
#yum
state= installed和removed(删除) ,不加state选项默认是 installed
例如:
# ansible testhost -m yum -a "name=vim-enhanced"
# ansible testhost -m yum -a "name=wget"
# ansible testhost -m yum -a "name=wget state=removed
ansible mysql-server -m yum -a "name=MySQL-python state=present"
3.playbook的维护
检查yaml文件的语法是否正确
$ ansible-playbook -i /etc/ansible/hosts mysql.yaml --syntax-check
检查yaml文件中的tasks任务
$ ansible-playbook -i /etc/ansible/hosts mysql.yaml --list-task
检查yaml文件中的生效主机
$ ansible-playbook -i /etc/ansible/hosts mysql.yaml --list-hosts
运行playbook
$ ansible-playbook -i /etc/ansible/hosts mysql.yaml
运行playbook里面特定的某个task,从某个task开始运行
$ ansible-playbook -i /etc/ansible/hosts mysql.yaml --start-at-task='Copy mysql.conf'
---The end
阅读(1739) | 评论(0) | 转发(0) |