【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
相关推荐
cui_win4 分钟前
每日一令:Linux 极简通关指南 - 汇总
linux·运维·服务器
知星小度S32 分钟前
Linux权限探秘:驾驭权限模型,筑牢系统安全
linux·运维·服务器
黄交大彭于晏37 分钟前
发送文件脚本源码版本
java·linux·windows
搞Linux的杰仔1 小时前
Ubuntu20.04基础配置安装——系统安装(一)
linux·嵌入式开发
Kaede63 小时前
如何应对Linux云服务器磁盘空间不足的情况
linux·运维·服务器
Zfox_5 小时前
Redis:Hash数据类型
服务器·数据库·redis·缓存·微服务·哈希算法
Kookoos6 小时前
Dynamics 365 Finance + Power Automate 自动化凭证审核
运维·自动化·dynamics 365·power automate
apocelipes9 小时前
Linux c 运行时获取动态库所在路径
linux·c语言·linux编程
ABB自动化10 小时前
for AC500 PLCs 3ADR025003M9903的安全说明
服务器·安全·机器人
努力学习的小廉10 小时前
深入了解linux系统—— 进程池
linux·运维·服务器