Ansible自动化部署WordPress

本文详细描述了通过Ansibleplaybook在一台远程Linux服务器上执行一系列任务,包括停止firewalld、更改SELinux设置、安装软件包、配置Nginx和MariaDB,以及初始化WordPress环境。
摘要由CSDN通过智能技术生成

查看目录结构

[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  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值