【ansible】role流程实验
cd /server/scripts
mkdir roles-all
cd roles-all
mkdir role01
cd role01
touch top.yml
touch hosts
mkdir -p nfs-server/{files,templates,tasks,handlers} group_vars/all/
# 目录详情
[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
vim nfs-server/tasks/main.yml
- 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
vim nfs-server/templates/exports.j2
# author: {{ author }} {{ nfs_server_dir }} 172.16.1.0/24(rw,all_squash)
vim group_vars/all/main.yml
author: CJoy nfs_server_dir: /backup-nfs nfs_user: nfsnobody
vim nfs-server/handlers/main.yml
--- - name: restart_nfs systemd: name: nfs enabled: yes state: restarted
vim top.yml
- hosts: web roles: - role: nfs-server
目录成型
[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