---
- hosts: nfs
  tasks: 
    - name: 1. yum安装nfs-utils,rpcbind
      yum:
        name: 
          - nfs-utils
          - rpcbind
        state: present
      tags: "01"  
    - name: 2. template导入配置文件（文件中有变量）
      template: 
        src: /server/scripts/ansible/exports.j2
        dest: /etc/exports
        backup: yes
      tags: "02"  
      notify: 
        - 重启服务
      #- name: 2. lineinfile修改配置文件
      #lineinfile:
      #  path: /etc/exports
      #  regexp: "^/nfs/ans/"
      #  line: |
          # ansible-nfs     
          #    /nfs/ans/    172.16.1.0/24 (rw,all_squash,anonuid=2999,anongid=2999)
    - name: 3. 先创建www group
      group:
         name: "{{ nfs_user }}"
         gid: "{{ nfs_user_uid }}"
         state: present
      tags: "03"  
    - name: 3.5  将user添加www,uid 1999
      user:
         name: "{{ nfs_user }}"
         uid: "{{ nfs_user_uid }}"
         group: "{{ nfs_user }}"
         create_home: no
         shell: /sbin/nologin
         comment: "Ansible created service account"
         state: present
      tags: "04"  
    - name: 4. file创建共享目录并改所有者
      file:
        path: "{{ nfs_dir }}"
        owner: "{{ nfs_user }}"
        group: "{{ nfs_user }}"
        state: directory
      tags: "05"  
    - name: 5. systemd启动服务rpcbind,nfs(注意顺序，一次只能管一个)
      systemd:
        name: "{{ item }}"
        enabled: yes
        state: started
      loop: 
        - rpcbind
        - nfs 
      tags: "06"  
  handlers:     #设置踩雷后操作.
    - name: 重启服务
      systemd:
      name: nfs
      state: reloaded
      tags: "07"  
- hosts: web
  tasks:
    - name: 1. 安装nfs-utils
      yum:
        name: nfs-utils
        state: present
      tags: "01"  
    - name: 2. 创建挂载点
      file:
        path: "{{ web_upload }}"
        state: directory
      tags: "09"  
    - name: 3. 先创建www group
      group:
         name: "{{ web_user }}"
         gid: "{{ web_user_uid }}"
         state: present
      tags: "10"  
    - name: 3.5  将user添加www,uid 1999
      user:
         name: "{{ web_user }}"
         uid: "{{ web_user_uid }}"
         group: "{{ web_user }}"
         create_home: no
         shell: /sbin/nologin
         comment: "Ansible created service account"
         state: present
      tags: "11"  
    - name: 3.挂载与永久挂载
      mount:
        src: "{{ nfs_server_ip }}:{{ nfs_dir }}"
        path: "{{ web_upload }}" 
        fstype: nfs
        state: mounted
      tags: 
        -  "01"  
        -  "a1"  
