- name: 部署高可用 WordPress 集群
  hosts: webservers
  become: yes
  vars:
    # WordPress 配置
    wp_db_name: "wordpress"
    wp_db_user: "wpuser"
    wp_db_password: "wppass"
    wp_db_host: "web1"
    
    # PHP 配置
    php_modules:
      - php-fpm
      - php-mysqlnd
      - php-curl
      - php-gd
      - php-mbstring
      - php-xml
      - php-zip

    wordpress_url: "https://wordpress.org/latest.tar.gz"

  tasks:
    # ================== 初始化阶段 ==================
    - name: 初始化本地存储
      file:
        path: /var/www/html
        state: directory
        mode: '0755'
        owner: nginx
        group: nginx

    # ================== 软件部署阶段 ==================
    - name: 安装 PHP 扩展
      yum:
        name: "{{ php_modules }}"
        state: present
        update_cache: yes
      notify: restart php-fpm

    - name: 创建Nginx配置目录
      file:
        path: "/etc/nginx/conf.d"
        state: directory
        mode: '0755'

    # ================== WordPress部署阶段 ==================
    - name: 下载 WordPress 到目标服务器
      get_url:
        url: "{{ wordpress_url }}"
        dest: /tmp/wordpress.tar.gz
        timeout: 30
        validate_certs: no

    - name: 清空目标目录（首次部署）
      file:
        path: /var/www/html
        state: directory
        owner: nginx
        group: nginx
        mode: '0755'
      when: ansible_local.initial_deploy | default(true)

    - name: 解压 WordPress 文件
      unarchive:
        src: /tmp/wordpress.tar.gz
        dest: /var/www/html
        remote_src: yes
        extra_opts: 
          - --strip-components=1
          - --no-same-owner
        owner: nginx
        group: nginx

    # ================== 配置阶段 ==================
    - name: 部署 Nginx 配置
      template:
        src: wordpress.conf.j2
        dest: /etc/nginx/conf.d/wordpress.conf
      notify: reload nginx

    - name: 动态生成wp-config.php
      template:
        src: wp-config.php.j2
        dest: /var/www/html/wp-config.php
        mode: '0644'
      register: wp_config
      notify: 
        - restart php-fpm
        - reload nginx

    # ================== 权限管理阶段 ==================
    #    - name: 递归设置目录权限
    #    file:
    #     path: /var/www/html
    #    state: directory
    #    owner: nginx
    #        group: nginx
    #    mode: '0755'
    #    recurse: yes
    #
    #- name: 递归设置文件权限
    #  shell: |
    #    find /var/www/html -type f -exec chmod 644 {} \;
    #    find /var/www/html -type f -exec chown nginx:nginx {} \;
    #  args:
    #    warn: false  # 禁用 Shell 警告
    #    changed_when: false  # 强制标记为 changed
    # ================== 数据库初始化 ==================
    - name: 创建 WordPress 数据库（仅主库）
      mysql_db:
        name: "{{ wp_db_name }}"
        state: present
        login_unix_socket: /var/lib/mysql/mysql.sock
        login_user: root
        login_password: "123456Ok"
      when: inventory_hostname == "web1"

    - name: 创建数据库用户并授权（仅主库）
      mysql_user:
        name: "{{ wp_db_user }}"
        host: "%"
        password: "{{ wp_db_password }}"
        priv: "{{ wp_db_name }}.*:ALL"
        state: present
        login_unix_socket: /var/lib/mysql/mysql.sock
        login_user: root
        login_password: "123456Ok"
      when: inventory_hostname == "web1"

  handlers:
    - name: restart php-fpm
      service:
        name: php-fpm
        state: restarted
        
    - name: reload nginx
      service:
        name: nginx
        state: reloaded

    - name: clean wordpress package
      file:
        path: /tmp/wordpress.tar.gz
        state: absent
      listen: "cleanup tasks"
