Thank you for reading this post, don't forget to subscribe!
Следующая роль установит nfs сервер и настроит клиентов.
Нужно будет только поправить файл hosts и в /etc/ansible/playbooks/roles_play/nfs.yml указать пути до директорий на мастере и слейве.
cat /etc/ansible/hosts
[nfs:children]
nfsmaster
nfsclient
[nfsmaster]
192.168.1.112
[nfsclient]
192.168.1.111
192.168.1.110
cat /etc/ansible/roles/nfs/handlers/main.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 |
--- - name: start nfs service: name=nfs state=started enabled=yes - name: start rpcbind service: name=rpcbind state=restarted enabled=yes |
[/codesyntax]
cat /etc/ansible/roles/nfs/tasks/add-to-fstab-nfs-client.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- name: add {{ groups.nfsmaster }}:{{ dir_nfs_master }} {{ dir_nfs_client }} nfs defaults 1 2 to /etc/fstab lineinfile: dest: /etc/fstab line: "{{ hostvars[item].ansible_default_ipv4.address }}:{{ dir_nfs_master }} {{ dir_nfs_client }} nfs defaults 1 2" state: present become: yes with_items: "{{ groups.nfsmaster }}" when: inventory_hostname in groups['nfsclient'] - name: execute command "mount -a" on nfs clients "{{ groups.nfsclient }}" become: yes become_user: root shell: cmd: mount -a when: inventory_hostname in groups['nfsclient'] |
[/codesyntax]
cat /etc/ansible/roles/nfs/tasks/add-to-exports.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- name: add network to /etc/exports lineinfile: dest: "/etc/exports" line: "{{ dir_nfs_master }} {{ hostvars[item].ansible_default_ipv4.address }}/32(rw,sync,no_root_squash,no_subtree_check) " state: present become: yes with_items: "{{ groups.nfsclient }}" when: inventory_hostname in groups['nfsmaster'] - name: execute command "exportfs -rav" on nfs master "{{ groups.nfsmaster }}" become: yes become_user: root shell: cmd: exportfs -rav when: inventory_hostname in groups['nfsmaster'] |
[/codesyntax]
cat /etc/ansible/roles/nfs/tasks/create-nfs-dir.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
--- - name: Create directory "{{ dir_nfs_master }}" on nfsmaster file: path: "{{ dir_nfs_master }}" state: directory mode: 0755 owner: root group: root when: inventory_hostname in groups['nfsmaster'] - name: Create directory "{{ dir_nfs_client }}" on nfsclient file: path: "{{ dir_nfs_client }}" state: directory mode: 0755 owner: root group: root when: inventory_hostname in groups['nfsclient'] |
[/codesyntax]
cat /etc/ansible/roles/nfs/tasks/installepelrepo.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 |
- name: Import EPEL GPG key. rpm_key: key: /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-{{ ansible_distribution_major_version }} state: present - name: Install EPEL repo. yum: name: https://dl.fedoraproject.org/pub/epel/epel-release-latest-{{ ansible_distribution_major_version }}.noarch.rpm state: present |
[/codesyntax]
cat /etc/ansible/roles/nfs/tasks/install-packpages.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- name: install default packages for nfs yum: name: "{{item}}" state: present with_items: - nfs-utils - rpcbind notify: - start nfs - start rpcbind handlers: - name: start nfs service: name=nfs state=started enabled=yes - name: start rpcbind service: name=rpcbind state=started enabled=yes |
[/codesyntax]
cat /etc/ansible/roles/nfs/tasks/started-nfs.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 |
- name: Make sure NFS is started up service: name=nfs state=started enabled=yes - name: Make sure rpcbind is started up service: name=rpcbind state=restarted enabled=yes |
[/codesyntax]
cat /etc/ansible/roles/nfs/tasks/main.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
--- - import_tasks: installepelrepo.yml tags: epel - import_tasks: create-nfs-dir.yml tags: create-dir - import_tasks: install-packpages.yml tags: install-default-packpage - import_tasks: started-nfs.yml tags: start-nfs - import_tasks: add-to-exports.yml tags: add to exports - import_tasks: add-to-fstab-nfs-client.yml tags: add to fstab |
[/codesyntax]
cat /etc/ansible/playbooks/roles_play/nfs.yml
[codesyntax lang="php" blockstate="collapsed"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
--- - hosts: nfs become: true ignore_errors: yes become_method: sudo gather_facts: yes vars: - dir_nfs_master: /nfs - dir_nfs_client: /nfs-client roles: - nfs # tasks: # - include_role: # name: name1 # name: name2 |
[/codesyntax]
Здесь:
dir_nfs_master - директория на мастер сервере к которой будут подключаться клиенты
dir_nfs_client - директория на слейвах, в неё будет монтироваться директория с мастер сервера.