0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ansible学習メモ③(NFSサーバの構築)

Posted at

業務で最近Ansibleを使い始めました。私自身はエンジニア職ではないので普段コーディングをほとんどしないです。少しAnsibleがわかるように学習し始めています。

AnsibleでCentOSマシンにNFSサーバの構築

本タスクには六つのモジュールを使用
ansible.builtin.yum パッケージインストール
ansible.builtin.file ディレクトリ作成
ansible.builtin.lineinfile ファイル編集
ansible.builtin.command ディレクトリのエクスポート
ansible.builtin.service サービスのリスタート
ansible.posix.firewalld ファイアウォール設定
※マニフェストを実行前、コレクションのインストールが必要

ansible-galaxy collection install ansible.posix

作成するマニフェストtest.yamlは以下のようです。

---
- name: export nfs share
  hosts: all
  become: true
  vars:
    share: "/nfs/share"
    option: "192.168.0.0/24(rw,sync,root_squash)"
    permission: "0777"
  tasks:
    - name: NFS share installed
      ansible.builtin.yum:
        name:
          - nfs-utils
          - nfs4-acl-tools
        state: present

    - name: share directory exists
      ansible.builtin.file:
        path: "{{ share }}"
        state: directory
        mode: "{{ permission }}"
        owner: root
        group: root

    - name: edit in /etc/exports file
      ansible.builtin.lineinfile:
        path: /etc/exports
        state: present
        line: '{{ share }} {{ option }}'
      notify: restart NFS Server

    - name: export share
      ansible.builtin.command: "exportfs -rav"

    - name: firewall enabled
      ansible.posix.firewalld:
        service: "{{ item }}"
        state: enabled
        permanent: true
        immediate: true
      with_items:
        - nfs
        - rpc-bind
        - mountd

  handlers:
    - name: restart NFS Server
      ansible.builtin.service:
        name: nfs-server
        state: restarted
        enabled: true

inventoryファイルを以下のように設定

XX.XX.XX.XX ansible_ssh_private_key_file=~/XXXXX.pem

(XX.XX.XX.XX: 対象マシンのIPアドレスまたはホスト名)

以下のコマンドを実行し、対象マシンにパッケージインストールを行う。

ansible-playbook -i inventory test.yaml
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?