主要完成經過playbook自動生成zabbix_server,agent,這裏沒有徹底實現自動化,這裏機器的獲取仍是須要人爲手工填寫,若是感興趣想經過自動獲取須要部署的機器能夠經過namp掃描工具awk命令截取,這裏不過多描述。我用兩臺機器作的測試,老版本的模塊用法會有差別,若是沒法執行能夠經過官網去查看https://ansible-tran.readthedocs.io/en/latest/,參考文檔https://www.cnblogs.com/LyShark/p/10886486.htmlphp
ansible 2.4.2
zabbix 3.4.15html
安裝nmap掃描工具mysql
yum install nmap -y #經過ping探測172.16.9.0網段中存活機器 nmap -sP 172.16.9.0/24
完成機器間的免密登陸linux
生成公鑰對web
ssh-keygen -t rsa
設置ansible hosts配置sql
[test] 172.16.9.141 ansible_ssh_user="root" ansible_ssh_pass="root" 172.16.9.142 ansible_ssh_user="root" ansible_ssh_pass="root" [zabbix_server] 172.16.9.141 [zabbix_client] 172.16.9.142
批量推送公鑰到集羣節點shell
- hosts: test user: root tasks: - name: ssh-copy authorized_key: user=root key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}" tags: - sshkey
報錯:數據庫
FAILED! => {"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."}
解決:apache
/etc/ansible/ansible.cfgssh
修改host_key_checking(默認是check的)
playbook構建zabbix_server(官方建議一個play寫一個獨立的任務,測試不過多計較了)
# 初始化,關閉防火牆和SELINUX - hosts: zabbix_server tasks: - name: off selinux shell: setenforce 0 - name: seline modify enforcing lineinfile: dest: /etc/selinux/config regexp: '^SELINUX=' line: 'SELINUX=disabled' - name: seline firealld shell: systemctl stop firewalld && systemctl disable firewalld # 安裝部署LAMP環境,經過YUM模塊快速安裝 - hosts: zabbix_server tasks: - name: install LAMP yum: name={{item}} state=installed with_items: - httpd - httpd-devel - mariadb - mariadb-server - php - php-mysql - name: start httpd shell: systemctl restart httpd - name: start mariadb shell: systemctl restart mariadb # 下載YUM源,更新EOEL源,安裝Zabbix - hosts: zabbix_server tasks: - name: clear YUM shell: rm -rf /etc/yum.repos.d/* - name: install YUM EPEL get_url: 'url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo' - name: yum install EPEL -y yum: name=epel-release state=installed - name: install zabbix.repo shell: rpm -i http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm - name: install zabbix yum: name={{item}} state=installed with_items: - zabbix-server-mysql - zabbix-web-mysql - zabbix-agent - name: start zabbix-server shell: systemctl restart zabbix-server - name: start zabbix-agent shell: systemctl restart zabbix-agent # 安裝配置數據庫權限,導入zabbix數據庫 - hosts: zabbix_server tasks: - name: set mariadb password shell: mysqladmin -u root password 'ansible' - name: create zabbix master databases shell: mysql -uroot -pansible -e 'create database zabbix character set utf8 collate utf8_bin;' - name: set zabbix master databases grant shell: mysql -uroot -pansible -e 'grant all privileges on zabbix.* to zabbix@localhost identified by "zabbix";' - name: import zabbix initial data SQL shell shell: zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -pzabbix zabbix # 修改並拷貝配置文件,給予權限 - hosts: zabbix_server tasks: - name: edit zabbix dbhost lineinfile: dest: /etc/zabbix/zabbix_server.conf regexp: '# DBHost=localhost' line: 'DBHost=localhost' - name: edit zabbix dbpasswd lineinfile: dest: /etc/zabbix/zabbix_server.conf regexp: '# DBPassword=' line: 'DBPassword=zabbix' - name: cp zabbix web shell: cp -a /usr/share/zabbix/* /var/www/html/ - name: chmod web shell: chmod 755 -R /var/www/html/* - name: chown web shell: chown apache.apache -R /var/www/html/* - name: set php shell: echo "date.timezone = Asia/Shanghai" >> /etc/php.ini - name: set php shell: echo "max_execution_time = 300" >> /etc/php.ini - name: set php shell: echo "max_input_time = 300" >> /etc/php.ini - name: set php shell: echo "post_max_size = 32M" >> /etc/php.ini - name: set php shell: echo "memory_limit = 128M" >> /etc/php.ini - name: set php shell: echo "mbstring.func_overload = 0" >> /etc/php.ini - name: start http mysql zabbix shell: systemctl restart httpd ; systemctl restart mariadb - name: start http mysql zabbix shell: systemctl restart zabbix-server ; systemctl restart zabbix-agent - name: enabled http mysql zabbix shell: systemctl enable httpd ; systemctl enable mariadb - name: start http mysql zabbix shell: systemctl enable zabbix-server ; systemctl enable zabbix-agent
--- # 初始化,關閉防火牆和SELINUX - hosts: zabbix_client tasks: - name: off selinux shell: setenforce 0 - name: seline modify enforcing lineinfile: dest: /etc/selinux/config regexp: '^SELINUX=' line: 'SELINUX=disabled' - name: seline firealld shell: systemctl stop firewalld && systemctl disable firewalld # 安裝zabbix_client - hosts: zabbix_client vars: zabbix_server_ip: 172.16.9.141 zabbix_agent_ip: 172.16.9.142 tasks: - name: install zabbix_client shell: rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-agent-3.4.15-1.el7.x86_64.rpm - name: Install zabbix agent shell: yum -y install zabbix-agent - name: modify zabbix server ip address shell: sed -i 's#Server=127.0.0.1#Server='{{zabbix_server_ip}}'#g' /etc/zabbix/zabbix_agentd.conf - name: modify zabbix server active ip addr shell: sed -i 's/ServerActive=127.0.0.1/ServerActive='{{zabbix_server_ip}}'/g' /etc/zabbix/zabbix_agentd.conf - name: Enable remote command execution shell: sed -i 's/# EnableRemoteCommands=0/EnableRemoteCommands=1'/g /etc/zabbix/zabbix_agentd.conf - name: Enable remote command logs shell: sed -i 's/# LogRemoteCommands=0/LogRemoteCommands=1'/g /etc/zabbix/zabbix_agentd.conf - name: modify zabbix agent hostname shell: sed -i 's/Hostname=Zabbix server/Hostname='{{zabbix_agent_ip}}'/g' /etc/zabbix/zabbix_agentd.conf - name: enable zabbix-agent shell: systemctl start zabbix-agent ;systemctl enable zabbix-agent
再來幾個檢查語法結構,主機是否生效的命令
ansible-playbook install_zabbix_server.yaml --syntax-check ansible-playbook install_zabbix_server.yaml --list-task ansible-playbook install_zabbix_server.yaml --list-hosts