Ansible的一些使用语法命令查看之前写的一篇文章,传送门->ansible运维自动化。
这边主要写的是我最近通过ansible批量部署zabbix_agent的一些脚本和遇到的问题。
一、脚本内容
1、main.yml #主控脚本,下面的4个脚本可以根据自己需求随意变化,主要是把脚本加到这下面才会运行
--- - include: common.yml - include: copy.yml - include: install.yml - include: start.yml
2、commom.yml #这个脚本主要有5个模块,每个-name即一个模块
- name: Change files attribute #更改文件属性 shell: chattr -i {{ item }} with_items: - /etc/passwd - /etc/group - /etc/shadow - /etc/gshadow - name: Create Zabbix Group #创建zabbix组 group: name={{ item }} state=present with_items: - zabbix - name: Create Zabbix User #创建zabbix用户 user: name={{ item }} shell=/sbin/nologin group={{ item }} with_items: - zabbix - name: Create Directory and Change Owner #创建zabbix log文件夹和更改文件所组 file: path={{ item }} state=directory owner=zabbix group=zabbix mode=0700 with_items: - /var/log/zabbix - name: Yum package #安装python和gcc shell: yum install {{ item }} -y with_items: - libselinux-python - gcc - python-simplejson
3、copy.yml
- name: Copy zabbix tar file to remote hosts #拷贝zabbix安装包 copy: src={{ item }} dest=/usr/local/src with_items: - zabbix-3.0.3.tar.gz - name: Copy zabbix_zgentd file to remote hosts #拷贝zabbix_agent管理脚本 copy: src={{ item }} dest=/etc/init.d/ with_items: - zabbix_agentd - name: Change files zabbix_agentd #赋予脚本可执行权限 shell: chmod +x {{ item }} with_items: - /etc/init.d/zabbix_agentd
4、install.yml
- name: Untar zabbix tar file #解压zabbix安装包 shell: tar xzf /usr/local/src/zabbix-{{ item }}.tar.gz -C /usr/local/src with_items: - 3.0.3 - name: Configure Zabbix and Install Zabbix Agent #安装zabbix_agent shell: cd /usr/local/src/zabbix-{{ item }} && ./configure --enable-agent && make && make install with_items: - 3.0.3 - name: Copy configure file #拷贝zabbix_agent配置文件 template: src=zabbix_agentd.conf dest=/usr/local/etc/zabbix_agentd.conf owner=zabbix group=zabbix - name: Configure rc.local #配置rc.local开机自启 shell: echo "/etc/init.d/zabbix_agentd restart" >>/etc/rc.d/rc.local - name: Set User Zabbix can read files #配置zabbix用户只读文件 shell: chattr -i {{ item }} && setfacl -m u:zabbix:rx {{ item }} && chattr +i {{ item }} with_items: - /etc/hosts.deny - /etc/hosts.allow - /etc/ssh/sshd_config - /root/.ssh/authorized_keys - /etc/passwd - /etc/group - /etc/shadow - /etc/gshadow
5、start.yml
- name: Start Zabbix Agent daemon #启动zabbix_agent shell: zabbix_agentd
以上5个脚本就是我批量安装zabbix_agent使用的,当然也许并不是很完善。有兴趣的朋友可以一起沟通。
二、遇到问题
1、stderr: configure: error: newly created file is older than distributed files! Check your system clock
很明显,提示我们检查下系统时间。是因为agent机器和server机器时间相差太多。同步下时间即可。
未完待续

聂扬帆博客
一个分享IT运维相关工作经验和实战技巧的个人博客
2016年7月13日 下午6:52 沙发
这个脚本写的不错
2016年7月22日 上午10:48 板凳
这个脚本写的不错