Ansible部署NFS服务

虽然很简单,写的比较low,不过还是记下吧,下次就可以直接拿着用了。很久以前写过一篇如何配置,这个安装基本是把原来的给改成playbook了,附原来手动配置的链接。
另附官方模块地址:

http://docs.ansible.com/ansible/latest/mount_module.html

Server

  • Role目录结构

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    nfs/
    ├── nfs-client
    │   ├── defaults
    │   │   └── main.yml
    │   └── tasks
    │   └── main.yml
    └── nfs-server
    ├── defaults
    │   └── main.yml
    ├── tasks
    │   └── main.yml
    └── templates
    └── exports.j2
  • export
    cat nfs/nfs-server/templates/exports.j2

    1
    2
    #/data/nfsd/deploy 192.168.1.2/32(rw,root_squash,all_squash)
    /data/ops_nfs/deploy {{ip_range}}(rw,root_squash,all_squash)
  • NFS-server
    cat nfs/nfs-server/tasks/main.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    ---
    - name: install nfs pkg
    yum: name=nfs-utils state=present
    - name: set starting up with service rpcbind and nfs
    command: chkconfig rpcbind on
    - command: chkconfig nfs on
    - name: create /data/ops_nfs dir
    file:
    path: /data/ops_nfs/deploy
    state: directory
    owner: nfsnobody
    group: nfsnobody
    mode: 0755
    - name: modify exports config
    template: src=exports.j2 dest=/etc/exports
    - name: start nfs service
    service: name=rpcbind state=restarted
    - service: name=nfs state=restarted
    #- name: debug rpc result
    # command: rpcinfo -p
    # command: exportfs

Client

  • NFS-client
    cat nfs/nfs-client/tasks/main.yml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    ---
    - name: set starting up with service rpcbind and nfs
    command: chkconfig rpcbind on
    - name: start rpcbind service
    service: name=rpcbind state=restarted
    - name: mount nfs
    mount:
    name: /data/deploy
    src: "{{ nfs_server_address }}"
    fstype: nfs4
    opts: rw
    fstab: /etc/fstab
    state: mounted

Other

  • hosts
    1
    2
    3
    4
    5
    6
    7
    8
    9
    [nfs-client]
    192.168.0.1
    192.168.0.2
    192.168.0.3
    [nfs-server]
    192.168.0.3
    [nfs:chidren]
    nfs-client
    nfs-server

以上