【ansible】role流程实验

bash 复制代码
cd /server/scripts
bash 复制代码
mkdir roles-all
bash 复制代码
cd roles-all
bash 复制代码
mkdir role01
bash 复制代码
cd role01
bash 复制代码
touch top.yml
touch hosts
mkdir -p nfs-server/{files,templates,tasks,handlers} group_vars/all/
bash 复制代码
# 目录详情
[root@m01 /server/scripts/roles-all/role01]# tree F 
.
 ├── group_vars/
 │   └── all/
 ├── hosts
 ├── nfs-server/
 │   ├── files/
 │   ├── handlers/
 │   ├── tasks/
 │   └── templates/
 └── top.yml
 7 directories, 2 files

创建tasks

bash 复制代码
vim nfs-server/tasks/main.yml
bash 复制代码
- name: 01. Installing nfs-utils,rpcbind
  yum:
    name: nfs-utils,rpcbind
    state: present
  tags:
    - 01. Install

- name: 02. Modify the configuration file
  template:
    src: exports.j2
    dest: /etc/exports
    backup: yes
  notify:
    - restart_nfs
  tags:
    - 02. Modify the configuration file

- name: 03. Create a shared directory and set the owner
  file:
    path: "{{ nfs_server_dir }}"
    owner: "{{ nfs_user }}"
    group: "{{ nfs_user }}"
    state: directory
  tags:
    - 03. Create a shared directory and set the owner

- name: 04. Start the server
  systemd:
    name: "{{ item }}"
    enabled: yes
    state: started
  loop:
    - rpcbind
  tags:
    - 04. Start the server
bash 复制代码
vim nfs-server/templates/exports.j2
bash 复制代码
# author:  {{ author }}
{{ nfs_server_dir }} 172.16.1.0/24(rw,all_squash)
bash 复制代码
vim group_vars/all/main.yml
bash 复制代码
author: CJoy
nfs_server_dir: /backup-nfs
nfs_user: nfsnobody
bash 复制代码
vim nfs-server/handlers/main.yml
bash 复制代码
---
- name: restart_nfs
  systemd:
    name: nfs
    enabled: yes
    state: restarted
bash 复制代码
vim top.yml
bash 复制代码
- hosts: web
  roles:
  - role: nfs-server

目录成型

bash 复制代码
[root@m01 role01]# tree -F
.
├── group_vars/
│   └── all/
│       └── main.yml
├── hosts
├── nfs-server/
│   ├── files/
│   ├── handlers/
│   │   └── main.yml
│   ├── tasks/
│   │   └── main.yml
│   └── templates/
│       └── exports.j2
└── top.yml

7 directories, 6 files
相关推荐
蓝染k9z1 小时前
在Ubuntu上使用Docker部署DeepSeek
linux·人工智能·ubuntu·docker·deepseek+
苏-言2 小时前
Linux环境下的Java项目部署技巧:安装 Mysql
linux·运维·mysql
代码对我眨眼睛2 小时前
重回C语言之老兵重装上阵(十三)C 预处理器
linux·c语言
张文君3 小时前
ubuntu直接运行arm环境qemu-arm-static
linux·arm开发·ubuntu
lljss20203 小时前
在 WSL2 中重启 Ubuntu 实例
linux·运维·ubuntu
engchina3 小时前
在 Ubuntu 上安装 Node.js 23.x
linux·ubuntu·node.js
lingllllove3 小时前
使用 HTTP::Server::Simple 实现轻量级 HTTP 服务器
服务器·网络协议·http
Linux运维老纪4 小时前
K8s之Service详解(Detailed Explanation of K8s Service)
服务器·网络·云原生·容器·kubernetes·云计算·运维开发
程序猿编码4 小时前
自定义命令执行器:C++中命令封装的深度探索(C/C++实现)
linux·c语言·c++·网络安全·shell·命令行