# 官方提供@yunTaoScripts 角色管理 🔥🔥
# 了解角色
如果自己写playbook涉及较多方面,比如变量,tasks,handlers,jinja2模版。一个角色就是完成一个功能
# 编写角色
- 指定角色目录,不指定默认ansible.cfg 所在目录的roles目录下
[root@server1 ansible]# cat /etc/ansible/ansible.cfg | grep roles
# additional paths to search for roles in, colon separated
roles_path = /etc/ansible/roles
# 创建角色
[root@server1 ansible]# vim ansible.cfg
[root@server1 ansible]# cd roles/
[root@server1 roles]# ansible-galaxy init apache
- Role apache was created successfully
[root@server1 roles]# ll
total 0
drwxr-xr-x. 10 root root 154 Aug 16 22:44 apache
- 角色目录结构
[root@server1 roles]# tree apache/
apache
├── defaults
│ └── main.yml ###变量文件,优先级最低
├── files
│ └── what ### 普通copy 文件
├── handlers ### notify 触发执行
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml ### 任务文件
├── templates ###jinja2 模版文件
│ └── httpd.conf.j2
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml ### 变量文件 优先级最高
变量优先级顺序
playbook中定义变量优先级介于vars 和 default,即 vars> playbook> defaults
- handlers
[root@server1 roles]# cat apache/handlers/main.yml
---
# handlers file for apache
- name: restart http1
service:
name: httpd
state: restarted
- name: restart http2
service:
name: httpd
state: restarted
- tasks
[root@server1 roles]# cat apache/tasks/main.yml
---
# tasks file for apache
- name: install httpd
yum:
name: httpd
state: installed
- name: copy j2config file
template:
src: httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: restart http1
- name: copy config file
copy:
src: what
dest: /etc/httpd/conf/what
notify: restart http2
- name: start firewall
firewalld:
port: "{{http_port}}/tcp"
immediate: yes
permanent: yes
state: enabled
- name: start httpd
service:
name: httpd
state: started
- templates
[root@server1 roles]# head -n 4 apache/templates/httpd.conf.j2
ServerRoot "/etc/httpd"
Listen {{http_port}}
ServerName {{ansible_fqdn}}:{{http_port}}
Include conf.modules.d/*.conf
- vars
[root@server1 roles]# cat apache/vars//main.yml
---
# vars file for apache
http_port: 8085
# 创建playbook,并引用角色
[root@server1 roles]# cat apache-role.yaml
---
- hosts: xyt
roles:
- role: apache
tags: ["aa","bb"]
# 使用系统自带角色
- 安装系统角色
[root@server1 chap8]# yum install rhel-system-roles.noarch -y
- 引用角色
[root@server1 roles]# cat chrony-role.yaml
---
- hosts: xyt
vars:
timesync_ntp_servers:
- hostname: 192.168.26.129
iburst: yes
pool: yes
roles:
- role: timesync
tags: ["xx","yy"]
# galaxy
第三方角色包,可以在里面找到想要的角色。
https://galaxy.ansible.com/ (opens new window)
- 直接安装
cd /var/ftp/roles/
tar -zcvf web.tar.gz web
cd /etc/ansible/roles/
ansible-galaxy install ftp://localhost/roles/web.tar.gz
- 重命名安装
[root@server1 roles]# cat web.yaml
---
- src: ftp://localhost/roles/web.tar.gz
name: newweb
[root@server1 roles]# ansible-galaxy install -r web.yaml -p ./
- downloading role from ftp://localhost/roles/web.tar.gz
- extracting newweb to /etc/ansible/roles/newweb
- newweb was installed successfully
[root@server1 roles]# ll
drwxr-xr-x. 10 root root 154 Aug 20 10:32 newweb
请注意安装路径
- 如果没修改roles_path,会安装到默认路径下面。
← 快速链接