ansible deploy nfs

Preface

Use a virtual machine to create two cloud hosts with centos7.9 system. One of them serves as the mother machine of ansible and is named ansible. The other cloud host is named node1. Use this mother machine to write an ansible script.

Host IP
ansible 192.168.200.50
node1 192.168.200.51

Configure basic environment

[root@localhost ~]# hostnamectl set-hostname ansible
[root@localhost ~]# bash
[root@ansible ~]# 

[root@localhost ~]# hostnamectl set-hostname node1
[root@localhost ~]# bash
[root@node1 ~]# 

[root@ansible ~]# systemctl stop firewalld
[root@ansible ~]# setenforce 0
[root@ansible ~]# 

[root@node1 ~]# systemctl stop firewalld
[root@node1 ~]# setenforce 0
[root@node1 ~]# 

[root@ansible ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.50 ansible
192.168.200.51 node1
[root@ansible ~]# 

[root@node1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.50 ansible
192.168.200.51 node1
[root@node1 ~]# 
[root@ansible ~]# ssh-keygen
[root@ansible ~]# ssh-copy-id root@node1

Configure ansible and centos warehouses

[root@ansible ~]# ls
anaconda-ks.cfg  ansible.tar.gz
[root@ansible ~]# tar -zxvf ansible.tar.gz -C /opt/
[root@ansible ~]# mkdir /opt/centos
[root@ansible ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@ansible ~]# cp -rfv /mnt/* /opt/centos/
[root@ansible ~]# umount /mnt/
[root@ansible ~]# mv /etc/yum.repos.d/* /mnt/
[root@ansible ~]# vi /etc/yum.repos.d/ftp.repo
[root@ansible ~]# cat /etc/yum.repos.d/ftp.repo 
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[ansible]
name=ansible
baseurl=file:///opt/ansible
gpgcheck=0
enabled=1
[root@ansible ~]# 

Configure ansible file

[root@ansible ~]# yum install -y vsftpd ansible
[root@ansible ~]# echo "anon_root=/opt" >> /etc/vsftpd/vsftpd.conf
[root@ansible ~]# systemctl restart vsftpd
[root@ansible ~]# cat >> /etc/ansible/hosts <<eof
[node1]
192.168.200.51
eof
[root@ansible ~]# mkdir ansible_nfs
[root@ansible ~]# cd ansible_nfs/
[root@ansible ansible_nfs]# cat local.repo 
[centos]
name=centos
baseurl=ftp://ansible/centos
gpgcheck=0
enabled=1
[ansible]
name=ansible
baseurl=ftp://ansible/ansible
gpgcheck=0
enabled=1
[root@ansible ansible_nfs]# 
[root@ansible ansible_nfs]# cat install_nfs.yaml 
---
- name: Install and configure NFS server
  hosts: node1
  become: true
  vars:
    nfs_export_dir: "data"
  tasks:
    - name: Copy local.repo file to node1
      copy:
        src: "/root/ansible_nfs/local.repo"
        dest: "/etc/yum.repos.d/local.repo"
      become: true
    - name: Install NFS
      yum:
        name: nfs-utils
        state: latest
      become: true
    - name: Configure NFS exports
      template:
        src: "/root/ansible_nfs/exports.j2"
        dest: "/etc/exports"
      become: true
    - name: Create NFS share directory
      file:
        path: "{
    
    { nfs_export_dir }}"
        state: directory
        mode: "0777"
      become: true
    - name: exportfs
      shell: exportfs -r
    - name: Start NFS service
      service:
        name: nfs
        state: started
        enabled: true
      become: true
[root@ansible ansible_nfs]# 
[root@ansible ansible_nfs]# cat exports.j2 
data 192.168.200.0/24(rw,all_squash)
[root@ansible ansible_nfs]# 

executable file

[root@ansible ansible_nfs]# ansible-playbook install_nfs.yaml

verify

[root@node1 ~]# cat /etc/exports
data 192.168.200.0/24(rw,all_squash)
[root@node1 ~]# ls
anaconda-ks.cfg  data
[root@node1 ~]# showmount -e 192.168.200.51
Export list for 192.168.200.51:
/root/data 192.168.200.0/24
[root@node1 ~]#

Guess you like

Origin blog.csdn.net/m0_56363537/article/details/130297555