---
- name: 针对 web2 执行任务
  hosts: web2
  vars:
    packages:
      - nginx      
      - php
      - php-bcmath
      - php-cli
      - php-common
      - php-devel
      - php-embedded
      - php-fpm
      - php-gd
      - php-intl
      - php-mbstring
      - php-mysqlnd
      - php-opcache
      - php-pdo
      - php-process
      - php-xml
      - php-json
  tasks:
    - name: 当前执行剧本的机器
      debug:
      msg: "这是在 {{ inventory_hostname }} 机器上执行的任务"
    - name: 1. 安装nginx，php
      yum: 
        name: "{{ packages }}"
        state: present
        # yum模块本身就支持直接传递一个包列表，这样可以避免循环，并且只需要一个任务即可安装所有包
        # （推荐，因为这样只需要一次yum事务）：
        # 这里使用列表方式安装的好处是，yum会一次性安装所有包，只需要一个事务，比循环安装每个包更高效。
    - name: 2.1创建www用户组
      group:
        name: www
        gid: 1999
        state: present
    - name: 2.2创建www用户
      user:
        name: www
        group: www
        create_home: no
        shell: /sbin/nologin
        state: present
    - name: 3.修改nginx站点目录配置文件/conf.d/blog.linuxjk.cn.conf #（直接copy，与web1保持一致即可）
      copy:
        src: files/blog.linuxjk.cn.conf
        dest: /etc/nginx/conf.d/blog.linuxjk.cn.conf
    - name: 4.1修改nginx用户为www，与nfs统一
      lineinfile: 
        path: /etc/nginx/nginx.conf
        regexp: '^user'
        line: "user www;"

    - name: 5.1检查nginx配置是否正确
      shell: nginx -t  &>/dev/null
      register: check_nginx    
    
    - name: 4.2 修改php用户为www，与nfs统一,修改php监听socket文件的所属主所属组为www，权限0660
      lineinfile:
        path: /etc/php-fpm.d/www.conf
    # 使用 {{ item.olduser }} 来正确引用变量
        regexp: "{{ item.olduser }}"
        line: "{{ item.newuser }}"
    # 确保修改后配置文件语法正确
        backrefs: yes
      loop:
        - { olduser: '^;?\s*user\s*=', newuser: 'user = www' }
        - { olduser: '^;?\s*group\s*=', newuser: 'group = www' }
        - { olduser: '^;?\s*listen.owner\s*=', newuser: 'listen.owner = www' }
        - { olduser: '^;?\s*listen.group\s*=', newuser: 'listen.group = www' }
        - { olduser: '^;?\s*listen.mode\s*=', newuser: 'listen.mode = 0660' }
    - name: 4.3 修改php.ini(上传文件大小,默认2M)
      lineinfile:
        path: /etc/php.ini
        regexp: "{{ item.oldsize }}"
        line: "{{ item.newsize }}"
        backrefs: yes
      loop:
        - { oldsize: '^;?\s*upload_max_filesize\s*=.*', newsize: 'upload_max_filesize = 500M'}
        - { oldsize: '^;?\s*post_max_size\s*=.*', newsize: 'post_max_size = 512M'} 
        - { oldsize: '^;?\s*memory_limit\s*=.*', newsize: 'memory_limit = 256M'}
        - { oldsize: '^;?\s*max_execution_time\s*=.*', newsize: 'max_execution_time = 300'}

    - name: 5.2检查php配置是否正确
      shell: php-fpm -t &>/dev/null
      register: check_php

    - name: 6.1设置开机自启动并启动nginx服务
      systemd: 
        name: nginx
        enabled: yes
        state: started
      when: check_nginx.rc == 0
    - name: 提示nginx配置错误
      debug: 
        msg: |
          nginx配置文件有误，请检查配置
      when: check_nginx.rc != 0
    - name: 6.2设置开机自启动并启动php服务    
      systemd: 
        name: php-fpm
        enabled: yes
        state: started     
      when:  check_php.rc == 0 
    - name: 提示php配置错误  
      debug: 
        msg: |
          php配置文件有误，请检查配置
      when: check_php.rc != 0 

    - name: 8.创建临时目录存放网页目录文件
      file: 
        path: /tmp/test/
        state: directory
    - name: 8.将upload目录下的内容转移出去
      shell: mv /app/code/blog/wp-content/uploads/* /tmp/test  &>/dev/null
      ignore_errors: yes  # 添加错误忽略
    - name: 9. web2 挂载 nfs并设置永久挂载
      mount: 
        src: 172.16.1.31:/ans_www/web1
        path: /app/code/blog/wp-content/uploads
        fstype: nfs
        state: mounted
      register: mount_nfs
    - name: 10. 显示挂载结果(成功)
      debug: 
        msg: mount is successful
      when:  not mount_nfs.failed
    - name: 10. 显示挂载结果(失败)
      debug: 
        msg: mount is failed
      when:   mount_nfs.failed
    - name: 11. 调整站点目录，nginx工作目录权限
      file: 
        path: "{{ item }}"
        owner: www
        group: www
      loop: 
        - /app/code/blog/
        - /var/log/nginx/
        - /var/lib/nginx/
