【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
相关推荐
weixin_307779133 小时前
Jenkins中的Jakarta Activation API插件:功能、使用与最佳实践
运维·开发语言·ci/cd·自动化·jenkins
Macbethad4 小时前
工业触摸屏技术指南:选型、难点与实战解决方案
服务器·前端·数据库
minihuabei5 小时前
跨域拉镜像
linux
王 富贵5 小时前
【Linux】防火墙常用命令(iptables/firewalld/ufw)
linux·运维·服务器
一条咸鱼¥¥¥5 小时前
【运维经济】思科交换机和路由器的密码重置方法
运维·网络·智能路由器
写代码的【黑咖啡】5 小时前
Linux系统简介及常用命令分类详解
linux·运维·服务器
❀͜͡傀儡师6 小时前
docker一键部署夜莺监控
运维·docker·容器
0思必得06 小时前
[Web自动化] HTML列表标签
运维·python·自动化·html·web自动化
lang201509286 小时前
Sentinel限流核心:ThrottlingController设计详解
服务器·网络·sentinel
YFLICKERH6 小时前
【Linux系统】ubuntu 25.04 虚拟机联网与DNS域名问题排查案例
linux·ubuntu25.04