查看目录结构
[root@master ~]# tree
.
├── enabled.yaml
├── file.yaml
├── firewalld.yaml
├── lnmp_install.yaml
├── mariadb.repo
├── nginx.conf
├── nginx.repo
├── user.yaml
└── vsftpd.yaml
0 directories, 9 files
编写剧本
- hosts: 192.168.169.209
remote_user: root
tasks:
- name: stop firewalld setenforce
shell: systemctl disable firewalld --now && setenforce 0; sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config;
- name: rm repo
shell: rm -rf /etc/yum.repos.d/*
- name: curl centos.repo
shell: curl -o /etc/yum.repos.d/centos.repo http://mirrors.aliyun.com/repo/Centos-7.repo warn=false
- name: curl epel.repo
shell: curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo warn=false
- name: copy repo
copy: src={{ item }} dest=/etc/yum.repos.d/
with_items:
- nginx.repo
- mariadb.repo
- name: install epel
shell: rpm -Uvh https://mirrors.cloud.tencent.com/epel/epel-release-latest-7.noarch.rpm
ignore_errors: yes
- name: install php
shell: rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
ignore_errors: yes
- name: yum php
yum: name={{ item }} state=installed
with_items:
- mod_php72w.x86_64
- php72w-cli.x86_64
- php72w-common.x86_64
- php72w-mysqlnd
- php72w-fpm.x86_64
- nginx
- mariadb-server
- mariadb
ignore_errors: yes
- name: start service
service: name={{ item }} state=started enabled=yes
with_items:
- nginx
- php-fpm
- mariadb
- name: copy nginx.conf
copy: src=nginx.conf dest=/etc/nginx/conf.d/default.conf backup=yes
- name: grant mariadb
shell: mysql -e "create database wordpress; grant all privileges on *.* to 'wordpress'@'%' identified by '000000'; delete from mysql.user where user = '' and host = 'localhost'; flush privileges;"
- name: restart service
service: name={{ item }} state=restarted
with_items:
- nginx
- php-fpm
- mariadb
- name: installed wget unzip vim
yum: name={{ item }} state=installed
with_items:
- wget
- unzip
- vim
- name: wordpress
shell: wget https://cn.wordpress.org/wordpress-4.7.3-zh_CN.zip; unzip wordpress-4.7.3-zh_CN.zip; rm -rf /usr/share/nginx/html/*; cd /root/wordpress; cp -rvf * /usr/share/nginx/html/; cp wp-config-sample.php wp-config.php; chmod -R 777 /usr/share/nginx/html/; sed -i s/define('DB_NAME', 'database_name_here');/define('DB_NAME', 'wordpress');/g; sed -i s/define('DB_USER', 'wordpress');/define('DB_USER', 'username_here');/g; sed -i s/define('DB_PASSWORD', 'password_here');/define('DB_PASSWORD', '000000');/g;
运行ansible剧本
[root@master ~]# ansible-playbook lnmp.yaml
PLAY [192.168.169.209] **************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.169.209]
TASK [stop firewalld setenforce] ****************************************************************************************************************************
changed: [192.168.169.209]
TASK [rm repo] **********************************************************************************************************************************************
changed: [192.168.169.209]
TASK [curl centos.repo] *************************************************************************************************************************************
changed: [192.168.169.209]
TASK [curl epel.repo] ***************************************************************************************************************************************
changed: [192.168.169.209]
TASK [copy repo] ********************************************************************************************************************************************
changed: [192.168.169.209] => (item=nginx.repo)
changed: [192.168.169.209] => (item=mariadb.repo)
TASK [install epel] *****************************************************************************************************************************************
changed: [192.168.169.209]
TASK [install php] ******************************************************************************************************************************************
changed: [192.168.169.209]
TASK [yum php] **********************************************************************************************************************************************
changed: [192.168.169.209] => (item=[u'mod_php72w.x86_64', u'php72w-cli.x86_64', u'php72w-common.x86_64', u'php72w-mysqlnd', u'php72w-fpm.x86_64', u'nginx', u'mariadb-server', u'mariadb'])
TASK [start service] ****************************************************************************************************************************************
changed: [192.168.169.209] => (item=nginx)
changed: [192.168.169.209] => (item=php-fpm)
changed: [192.168.169.209] => (item=mariadb)
TASK [copy nginx.conf] **************************************************************************************************************************************
changed: [192.168.169.209]
TASK [grant mariadb] ****************************************************************************************************************************************
changed: [192.168.169.209]
TASK [restart service] **************************************************************************************************************************************
changed: [192.168.169.209] => (item=nginx)
changed: [192.168.169.209] => (item=php-fpm)
changed: [192.168.169.209] => (item=mariadb)
TASK [installed wget unzip vim] *****************************************************************************************************************************
changed: [192.168.169.209] => (item=[u'wget', u'unzip', u'vim'])
TASK [wordpress] ********************************************************************************************************************************************
changed: [192.168.169.209]
PLAY RECAP **************************************************************************************************************************************************
192.168.169.209 : ok=15 changed=14 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0