首页 > 代码库 > ansible自动化部署zabbix客户端
ansible自动化部署zabbix客户端
本文主要介绍使用ansible playbook中roles,在不同os版本下批量部署zabbix客户端。
一、facts介绍
playbook的部分fetch信息
ansible版本2.2.1.0
使用setup模块获取
# ansible 192.168.1.12 -m setup "ansible_distribution": "CentOS", "ansible_distribution_major_version": "6", "ansible_distribution_release": "Final", "ansible_distribution_version": "6.6", "ansible_processor_cores": 2, "ansible_processor_count": 1, "ansible_processor_threads_per_core": 2, "ansible_processor_vcpus": 4, "ansible_default_ipv4": { "address": "192.168.1.12", "alias": "eth0", "broadcast": "192.168.1.255", "gateway": "192.168.1.1", "interface": "eth0", "macaddress": "10:bf:48:b7:98:96", "mtu": 1500, "netmask": "255.255.255.0", "network": "192.168.1.0", "type": "ether" } "ansible_all_ipv4_addresses": [ "192.168.1.12" ], # ansible 192.168.1.17 -m setup "ansible_distribution": "CentOS", "ansible_distribution_major_version": "7", "ansible_distribution_release": "Core", "ansible_distribution_version": "7.0.1406", "ansible_processor_cores": 2, "ansible_processor_count": 1, "ansible_processor_threads_per_core": 2, "ansible_processor_vcpus": 4, "ansible_default_ipv4": { "address": "192.168.1.17", "alias": "enp3s0", "broadcast": "192.168.1.255", "gateway": "192.168.1.1", "interface": "enp3s0", "macaddress": "74:d0:2b:31:c6:a1", "mtu": 1500, "netmask": "255.255.255.0", "network": "192.168.1.0", "type": "ether" }, "ansible_all_ipv4_addresses": [ "172.17.0.1", "192.168.1.17", "192.168.2.116", "192.168.1.116" ],
当需要用到主机ip地址时,使用"ansible_default_ipv4.address"变量获取主机ip地址,而使用ansible_all_ipv4_addresses[x]获取时,主机有多个ip地址时,不好判读使用哪个。
二、roles介绍
roles用于层次性、结构化地组织playbook。roles能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量(vars)、文件(file)、任务(tasks)、模块(modules)及处理器(handlers)放置于单独的目录中,并可以便捷地include它们的一种机制。
三、创建roles的步骤
1.创建以roles命名的目录;
2.在roles目录中分别创建以各角色名称命名的目录,如webservers等;
3.在每个角色命名的目录中分别创建files、handlers、meta、tasks、templates和vars目录;用不到的目录可以创建为空目录,也可以不创建;
4.在playbook文件中,调用各角色;
四、roles内各目录中可用的文件
tasks目录:至少应该包含一个名为main.yml的文件,其定义了此角色的任务列表;此文件可以使用include包含其它的位于此目录中的task文件;
files目录:存放由copy或script等模块调用的文件;
templates目录:template模块会自动在此目录中寻找Jinja2模板文件;
handlers目录:此目录中应当包含一个main.yml文件,用于定义此角色用到的各handler;在handler中使用include包含的其它的handler文件也应该位于此目录中;
vars目录:应当包含一个main.yml文件,用于定义此角色用到的变量;
meta目录:应当包含一个main.yml文件,用于定义此角色的特殊设定及其依赖关系;ansible1.3及其以后的版本才支持;
default目录:为当前角色设定默认变量时使用此目录;应当包含一个main.yml文件。
五、自动安装zabbix客户端
1.环境介绍
zabbix server:192.168.1.49
zabbix agent1:192.168.1.12 centos6.6
zabbix agent2:192.168.1.17 centos7.0
zabbix版本3.0.4
2.定义hosts
# cat /etc/ansible/hosts [pltest] 192.168.1.17 ansible_ssh_user=root ansible_ssh_pass=12345678 192.168.1.12 ansible_ssh_user=root ansible_ssh_pass=l0jzt7yhudai12
3.目录结构
# cd /etc/ansible/ # mkdir zabbix_client_install # mkdir zabbix_client_install/{files,handlers,meta,tasks,templates,vars} # tree roles roles |-- zabbix_client_install | |-- files | | |-- zabbix_agent_centos6.tgz | | `-- zabbix_agent_centos7.tgz | |-- handlers | | `-- main.yml | |-- meta | |-- tasks | | |-- copy.yml | | |-- delete.yml | | |-- install.yml | | `-- main.yml | |-- templates | | |-- zabbix_agentd_centos6 | | |-- zabbix_agentd_centos6.conf | | |-- zabbix_agentd_centos7 | | `-- zabbix_agentd_centos7.conf | `-- vars | `-- main.yml `-- zabbix_client_install.yml 7 directories, 13 files
4.文件内容
zabbix_client_install.yml内容 # cat /etc/ansible/roles/zabbix_client_install.yml --- - hosts: pltest remote_user: root gather_facts: True roles: - zabbix_client_install files包含在不同平台上已经编译好的zabbix客户端程序 handlers目录下定义了配置文件发生变化后触发重启进程(notify) # cat /etc/ansible/roles/zabbix_client_install/handlers/main.yml - name: restart zabbix_agentd service: name=zabbix_agentd state=restarted tasks下角色的任务列表 # cat /etc/ansible/roles/zabbix_client_install/tasks/main.yml - include: copy.yml - include: install.yml - include: delete.yml copy.yml文件中使用tags定义标签,when语句判断os版本,复制对应版本的客户端程序。 tags用于让用户选择运行或路过playbook中的部分代码。ansible具有幂等性,因此会自动跳过没有变化的部分,即便如此,有些代码为测试其确实没有发生变化的时间依然会非常地长。此时,如果确信其没有变化,就可以通过tags跳过此些代码片断。 在task后添加when子句即可使用条件测试;when语句支持Jinja2表达式语法。 when语句中还可以使用facts或playbook中定义的变量。 # cat /etc/ansible/roles/zabbix_client_install/tasks/copy.yml - name: Create zabbix user in centos client tags: - create_user user: name={{ zabbix_user }} state=present createhome=no shell=/sbin/nologin when: ansible_distribution == "CentOS" and ansible_distribution_major_version|int == 7 or ansible_distribution_major_version|int == 6 - name: Copy zabbix client software to centos client tags: - copy_files copy: src=http://www.mamicode.com/files/zabbix_agent_centos7.tgz dest=/tmp/zabbix_agent_centos7.tgz owner=root group=root"CentOS" and ansible_distribution_major_version|int == 7 - name: Copy zabbix client software to centos client tags: - copy_files copy: src=http://www.mamicode.com/files/zabbix_agent_centos6.tgz dest=/tmp/zabbix_agent_centos6.tgz owner=root group=root"CentOS" and ansible_distribution_major_version|int == 6 - name: Uncompression zabbix client software to centos client tags: - un_files shell: tar xf /tmp/zabbix_agent_centos7.tgz -C {{ zabbix_dir }}/ when: ansible_distribution == "CentOS" and ansible_distribution_major_version|int == 7 - name: Uncompression zabbix client software to centos client tags: - un_files shell: tar xf /tmp/zabbix_agent_centos6.tgz -C {{ zabbix_dir }}/ when: ansible_distribution == "CentOS" and ansible_distribution_major_version|int == 6 - name: Copy zabbix start script to centos client tags: - copy_script template: src=http://www.mamicode.com/zabbix_agentd_centos7 dest=/etc/init.d/zabbix_agentd owner=root group=root mode=0755"CentOS" and ansible_distribution_major_version|int == 7 - name: Copy zabbix start script to centos client tags: - copy_script template: src=http://www.mamicode.com/zabbix_agentd_centos6 dest=/etc/init.d/zabbix_agentd owner=root group=root mode=0755"CentOS" and ansible_distribution_major_version|int == 6 - name: Copy zabbix config to centos client tags: - copy_config template: src=http://www.mamicode.com/zabbix_agentd_centos7.conf dest={{ zabbix_dir }}/zabbix/etc/zabbix_agentd.conf owner={{ zabbix_user }} group={{ zabbix_user }} mode=0644"CentOS" and ansible_distribution_major_version|int == 7 - name: Copy zabbix config to centos client tags: - copy_config template: src=http://www.mamicode.com/zabbix_agentd_centos6.conf dest={{ zabbix_dir }}/zabbix/etc/zabbix_agentd.conf owner={{ zabbix_user }} group={{ zabbix_user }} mode=0644"CentOS" and ansible_distribution_major_version|int == 6 install.yml修改目录权限,启动服务 # cat /etc/ansible/roles/zabbix_client_install/tasks/install.yml - name: Modify Zabbix Dir Permission In Centos Client tags: - change_perm file: path={{ zabbix_dir }}/zabbix owner={{ zabbix_user }} group={{ zabbix_user }} mode=0755 - name: Add Start Zabbix Service In Centos Client tags: - add_start_script shell: chkconfig --add zabbix_agentd - name: Start Zabbix Service In Centos Client tags: - start_server service: name=zabbix_agentd state=started delete.yml安装完删除软件包 # cat /etc/ansible/roles/zabbix_client_install/tasks/delete.yml - name: Delete zabbix compression software in centos client tags: - delete_files file: path=/tmp/zabbix_agent_centos7.tgz state=absent when: ansible_distribution == "CentOS" and ansible_distribution_major_version|int == 7 - name: Delete zabbix compression software in centos client tags: - delete_files file: path=/tmp/zabbix_agent_centos6.tgz state=absent when: ansible_distribution == "CentOS" and ansible_distribution_major_version|int == 6 templates目录下的模板文件 zabbix_agentd_centos{6|7}服务启动脚本 # cat /etc/ansible/roles/zabbix_client_install/templates/zabbix_agentd_centos6 BASEDIR={{ zabbix_dir }}/zabbix zabbix_agentd_centos{6|7}.conf配置文件 # cat /etc/ansible/roles/zabbix_client_install/templates/zabbix_agentd_centos6.conf Server={{ zabbix_server_ip }} ServerActive={{ zabbix_server_ip }} Hostname={{ ansible_default_ipv4.address }} vars目录下定义变量文件 # cat /etc/ansible/roles/zabbix_client_install/vars/main.yml zabbix_dir: /usr/local zabbix_user: zabbix zabbix_server_ip: 192.168.1.49
六、执行任务
1.语法检查 ansible-playbook zabbix_client_install.yml --syntax-check 2.执行命令 ansible-playbook zabbix_client_install.yml 3.查看任务数 ansible-playbook zabbix_client_install.yml --list-tasks 4.查看标签 ansible-playbook zabbix_client_install.yml --list-tags 5.执行某个tag ansible-playbook zabbix_client_install.yml -t create_user 6.执行多个tags ansible-playbook zabbix_client_install.yml -t create_user,copy_files,delete_files
本文出自 “linux之路” 博客,请务必保留此出处http://hnr520.blog.51cto.com/4484939/1922287
ansible自动化部署zabbix客户端