一、实验准备
192.168.115.148:ansbile
192.168.115.149
192.168.115.151
设置防火墙、selinux
systemctl stop firewalld
setenforce 0
二、设置ansbile的hosts文件
[group] 192.168.115.148 192.168.115.149 192.168.115.151
三、在192.168.115.148上完成相关准备
上传Discuz_X3.3_SC_UTF8.zip
配置本地源、确保我们的sr0挂载后可以使用
vim /etc/local.repo [local] name=local baseurl=file:///mnt enabled=1 gpgcheck=0
四、编写roles
创建目录
cd /etc/ansible/roles/ mkdir -p {httpd,mysql,php,Discuz}/{files,tasks,handlers,templates,vars,meta} touch {httpd,mysql,php,Discuz}/{tasks,handlers,vars,meta}/main.yml tree . ├── Discuz │ ├── files │ ├── handlers │ │ └── main.yml │ ├── meta │ ├── tasks │ │ └── main.yml │ ├── templates │ └── vars ├── http │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ │ └── main.yml │ ├── templates │ └── vars ├── mysql │ ├── files │ ├── handlers │ ├── meta │ ├── tasks │ │ └── main.yml │ ├── templates │ └── vars └── php ├── files ├── handlers ├── meta ├── tasks │ └── main.yml ├── templates └── vars
编写http的main.yml
vim /ect/ansbile/roles/http/tasks/main.yml --- - name: 删除原来的yum源 shell: rm -rf /etc/yum.repos.d/* - name: 分发本地源文件 copy: src=/etc/local.repo dest=/etc/yum.repos.d/ - name: 挂载本地sr0光盘 shell: mount /dev/sr0 /mnt - name: 安装http服务 yum: name=httpd state=present - name: 启动httpd服务,并设为开机自启 service: name=httpd state=started enabled=yes - name: 删除httpd默认的访问网页 shell: rm -rf /var/www/html/*
编写mysql的main.yml
vim /etc/ansible/roles/mysql/tasks/main.yml --- - name: 安装mysql shell: yum -y install mariadb mariadb-server - name: 启动mariadb设为开机自启 shell: systemctl start mariadb && systemctl enable mariadb - name: 创建数据库hy shell: mysql -e 'create database hy;' - name: 创建用户qzh shell: mysql -e 'create user"qzh"@"localhosr" identified by "123.com";' - name: 为用户qzh授权hy库中的所有权限 shell: mysql -e 'grant all privileges on hy.* to "qzh"@"localhost";'
编写phpmain.yml
vim /etc/ansible/roles/php/tasks/main.yml --- - name: 安装php环境/依赖 yum: name=php,php-gd,php-ldap,php-odbc,php-pear,php-xml,php-xmlrpc,php-mbstring,php-snmp,php-soap,curl,curl-devel,php-bcmath,php-mysql state=present
编写Discuz的main.yml
vim /etc/ansible/roles/Discuz/tasks/main.yml --- - name: 分发论坛文件 copy: src=/Discuz_X3.3_SC_UTF8.zip dest=/opt - name: 安装zip解压工具 yum: name=unzip state=present - name: 解压论坛 shell: cd /opt && unzip Discuz_X3.3_SC_UTF8.zip - name: 移动论坛文件 shell: mv /opt/upload/* /var/www/html - name: 修改属主为Apache shell: chown -R apache /var/www/html/* notify: restart httpd
编写playbook
vim /etc/ansible/lamp.yml --- - hosts: group remote_user: root roles: - http - mysql - php - Discuz
五、执行
ansible-playbook /etc/ansible/lamp.yml
。
。
。
。