Ansible
Ansible 配置Apache(加粗的字體是要執行的命令)
配置免密登錄
[root@localhost ansible]# vim /etc/ansible/hosts //用來配置目標主機 加入以下內容
[httpd] //目標主機組
192.168.18.130 //目標主機的ip地址
192.168.18.134 //目標主機的ip地址
[root@localhost ~]# ansible httpd -m ping // 測試是否目標主機是否可以連通 如果顯示一下內容就是成功
編輯ansible的腳本
檢查是否有語法錯誤,執行以下命令成功則是沒有語法錯
如果沒有語法錯誤,就開始自動部署
Ansible自動部署nginx
1.配置免密登錄
[root@localhost ansible]# vim /etc/ansible/hosts //用來配置目標主機 加入以下內容
[nginx] //目標主機組
192.168.18.130 //目標主機的ip地址
192.168.18.134 //目標主機的ip地址
[root@localhost ~]# ansible nginx -m ping // 測試是否目標主機是否可以連通 如果顯示一下內容就是成功
編寫nginx的自動部署的文件nginx.yml
檢測是否有語法錯誤
運行文件nginx.yml
訪問目標主機組的ip地址
Ansible自動部署lnmp架構
配置免密登錄
[root@localhost ansible]# vim /etc/ansible/hosts //用來配置目標主機 加入以下內容
lnmp] //目標主機組
192.168.18.130 //目標主機的ip地址
192.168.18.134 //目標主機的ip地址
[root@localhost ~]# ansible nginx -m ping // 測試是否目標主機是否可以連通 如果顯示一下內容就是成功
編寫運行腳本lnmp.yml
運行結果
Ansible自動部署tomcat
- hosts: tomcat
tasks:- name: 關閉防火牆
service: name=iptables state=stopped - name: 關閉selinux
shell: setenforce 0 - name: 安裝所需的軟件
yum: name=tar,libselinux-python state=latest - name: 推送jdk java環境
copy: src=jdk-8u131-linux-x64_.rpm dest=/root - name: 創建文件夾
file: path=/opt/tomcat state=directory - name: 推送tomcat的壓縮包
unarchive: src=apache-tomcat-8.5.35.tar.gz dest=/opt/tomcat - name: 安裝jdk
yum: name=jdk-8u131-linux-x64_.rpm state=installed - name: 啓動tomcat
shell: nohup /opt/tomcat/apache-tomcat-8.5.35/bin/startup.sh &
Ansible自動部署lnmp架構+上線電商
編寫的lnmp.yml 文件
- name: 關閉防火牆
- hosts: lnmp
tasks:- name: 關閉防火牆
service: name=iptables state=stopped - name: 關閉selinux
shell: setenforce 0 - name: 配置班級yum源
script: repo.sh - name: 配置相關組件以及MySQL
yum: name=zlib-devel,pcre-devel,gcc,gcc-c++,php,php-mysql,php-gd,php-fpm,libselinux-python,tar,vim,unzip state=latest - name: 推送nginx安裝包
unarchive: src=nginx-1.16.1.tar.gz dest=/root - name: 安裝nginx
shell: cd /root/nginx-1.16.1 && ./configure && make && make install - name: 啓動nginx
shell: netstat -ntlp | grep -q nginx || /usr/local/nginx/sbin/nginx - name: 推送ningx的配置文件
template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
tags: config-nginx
notify: restart-nginx - name: 啓動php-fpm
service: name=php-fpm state=started - name: 安裝mysql
yum: name=mysql,mysql-server state=latest - name: 啓動mysql
service: name=mysqld state=started - name: 刪除數據庫如果存在
shell: mysql -e “drop database if exists ds;” - name: 創建數據庫
shell: mysql -e “create database ds charset=utf8;” - name: 給用戶授權
shell: mysql -e “grant all on . to ‘tom’@’%’ identified by ‘123’;” - name: 刷新權限
shell: mysql -e “flush privileges;” - name: 推送電商的安裝包並給予權限
unarchive: src=tinyshopV2.5_data.zip dest=/usr/local/nginx/html/ mode=777
handlers: - name: restart-nginx
shell: /usr/local/nginx/sbin/nginx -s reload
Ansible自動部署Zabbix
1.在Centos7系統上的操作(查找rpm源可以在repo.zabbix.com網站查找)
- name: 關閉防火牆
[root@localhost zabbix]# rpm -ivh http://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm
獲取http://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm
警告:/var/tmp/rpm-tmp.MEfeuA: 頭V4 RSA/SHA512 Signature, 密鑰 ID a14fe591: NOKEY
準備中… ################################# [100%]
正在升級/安裝…
1:zabbix-release-4.4-1.el7 ################################# [100%]
[root@localhost ~]# yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-agent
[root@localhost ~]# yum -y install mariadb mariadb-server
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl enable mariadb
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-MariaDB MariaDB Server
Copyright © 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
MariaDB [(none)]> create database zabbix charset=utf8;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on . to ‘zabbix’@‘localhost’ identified by ‘123’;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@localhost ~]# cd /usr/share/doc/zabbix-server-mysql-4.4.1/
[root@localhost zabbix-server-mysql-4.4.1]# ls
AUTHORS ChangeLog COPYING create.sql.gz NEWS README
[root@localhost zabbix-server-mysql-4.4.1]# zcat create.sql.gz | mysql -uzabbix -p123 zabbix
[root@localhost ~]# vim /etc/zabbix/zabbix_server.conf
DBName=zabbix //創建的數據庫的名字
DBUser=zabbix //用戶
DBPassword=123 //用戶的密碼
[root@localhost ~]# vim /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai
[root@localhost ~]# systemctl restart httpd zabbix-server zabbix-agent
可以訪問頁面查看是否成功
下面時ansible部署自動發現
然後點添加
下面的操作是要配置ansible-playbook自動部署zabbix的客戶端
1.配置免密登錄(可以參考上面操)
2.編寫ansible-playbook
- hosts: zabbix
vars:- ip: “192.168.18.140”
tasks: - name: 關閉防火牆
service: name=iptables state=stopped - name: 關閉selinux
shell: setenforce 0 - name: 配置rpm源
shell: rpm -q zabbix-release-4.4-1.el6.noarch || rpm -ivh http://repo.zabbix.com/zabbix/4.4/rhel/6/x86_64/zabbix-release-4.4-1.el6.noarch.rpm - name: 安裝zabbix-agent以及需要的插件
yum: name=libselinux-python,zabbix-agent,httpd state=latest - name: 分發配置文件
template: src=zabbix_agentd.conf dest=/etc/zabbix/zabbix_agentd.conf - name: 啓動zabbix以及相關軟件
service: name=zabbix-agent state=restarted - name: 啓動httpd
service: name=httpd state=restarted
3.檢測到你的目標主機就可以了
4.也可以在配置–>主機中查看
- ip: “192.168.18.140”