目录
role 动态变更
通过一个功能完整的Roles来整体了解Vars,Files,Handlers,Meta,Templates,然后逐步深入Roles Handlers用法
示例example.yaml
site.yaml webservers.yaml fooservers.yaml roles/ common/ files/ templates/ tasks/ handlers/ vars/ defaults/ meta/ webservers/ files/ templates/ tasks/ handlers/ vars/ defaults/ meta/
Playbooks中的调用方式如下
---
- hosts: webservers
roles:
- common
- webservers
roles对应功能的含义
roles/x/tasks/main.yaml 主函数,在其中任务都会执行
roles/x/handlers/main.yaml 所有包含其中的handlers都会执行
roles/x/vars/main.yaml 所有包含在其中的变量都会生效
roles/x/meta/main.yaml roles所有依赖都会写明
roles/x/{files,templates,tasks} 所有文件,模板都可放在这里
当Apache的配置文件发生变化时重启Apache进程
编排roles目录结构
roles/apache/ ├── handlers │ └── main.yaml ├── files │ └── httpd.conf └── tasks ├── restart.yaml └── main.yaml
目录结构需按要求编排,不得随意变更名称
mkdir roles/apache/{handlers,tasks,files}
编辑roles/apache/handlers/main.yaml
重启Apache进程
---
- name: restart apache
service:
name: apache2
state: restarted
编辑roles/apache/files/httpd.conf
apache2 配置文件,这里略过
....
编辑roles/apache/tasks/restart.yaml
更新Apache配置文件,如配置文件有变化则重启Apache
---
- name: transfer apache config
copy:
src: httpd.conf
dest: /opt/apache/httpd.conf
notify:
- restart apache
编辑roles/apache/tasks/main.yaml
---
- import_tasks: restart.yaml
编辑roles/apache.yaml
该yaml为总调度文件,完成Apache配置文件的变更和Apache的重启工作
---
- hosts: ubuntu
remote_user: root
roles:
- role: apache
ansible-playbook apache.yaml
PLAY [ubuntu] *****
TASK [Gathering Facts] *****
ok: [192.168.255.110]
TASK [apache : transfer apache config] *****
changed: [192.168.255.110]
RUNNING HANDLER [apache : restart apache] *****
changed: [192.168.255.110]
PLAY RECAP *****
192.168.255.110 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
roles 文件传输
files和templates均用于Ansible文件处理,files(不是file模块)目录下的文件无需写绝对路径即可将文件传输至远程主机,templates目录下的文件以Jinja2渲染,且传输文件至远程主机的同时支持预定义变量替换
编排目录结构如下
file.yaml roles/file/ ├── files │ ├── MAGEDU.PPT │ └── STANLEY.PPT ├── tasks │ ├── file.yaml │ └── main.yaml
mkdir -p roles/file/{files,tasks}
依次编辑 roles/file/files/MAGEDU.PPT,roles/file/files/STANLEY.PPT
roles/file/files/MAGEDU.PPT
This is magedu.ppt file.
roles/file/files/STANLEY.PPT
This is stanley.ppt file.
依次编辑role/file.yaml,roles/file/tasks/file.yaml,roles/file/tasks/main.yaml
file.yaml
---
# 该playbook是整个项目的调度入口
- hosts: ubuntu
gather_facts: yes
roles:
- role: file
roles/file/tasks/file.yaml
---
- name: file change file
copy:
src: "{{ item.src }}"
dest: "/opt/{{ item.dest }}"
owner: root
group: root
with_items:
- { src: 'MAGEDU.PPT', dest: 'magedu.ppt' }
- { src: 'STANLEY.PPT', dest: 'stanley.ppt' }
roles/file/tasks/main.yaml
---
- import_tasks: file.yaml
ansible-playbook file.yaml
PLAY [ubuntu] *******
TASK [Gathering Facts] *******
ok: [192.168.255.110]
TASK [file : file change file] *******
changed: [192.168.255.110] => (item={'src': 'MAGEDU.PPT', 'dest': 'magedu.ppt'})
changed: [192.168.255.110] => (item={'src': 'STANLEY.PPT', 'dest': 'stanley.ppt'})
PLAY RECAP *******
192.168.255.110 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
role 模板替换
Jinja2是一个Python的功能齐全的模板引擎
Jinja2读取变量的方式,通常两个花括号中间加变量名且花括号和变量名间需有空格分隔,
{{variable}}
更详尽内容请参考 Jinja2官网
将order.j2分发至远程主机/data/{{PROJECT}}/目录下,并改名为order.conf,且替换配置文件中变量为对应的值
编排目录如下
template.yaml roles/template/ ├── tasks │ ├── main.yaml │ └── template.yaml ├── templates │ └── order.j2 └── vars └── main.yaml
mkdir -p roles/template/{tasks,templates,vars}
tempates.yaml
---
- hosts: ubuntu
gather_facts: true
roles:
- role: template
roles/template/tasks/main.yaml
---
- import_tasks: template.yaml
roles/template/tasks/template.yaml
---
- name: tempalte transfer example
template:
src: order.j2
dest: /data/{{ PROJECT }}/order.conf
roles/template/templates/order.j2
project: {{ PROJECT }}
switch: {{ SWITCH }}
dbport: {{ DBPORT }}
roles/template/vars/main.yaml
---
PROJECT: "JAVA"
SWITCH: "ON"
DBPORT: "3306"
ansible-playbook tempates.yaml
PLAY [ubuntu] *****
TASK [Gathering Facts] *****
ok: [192.168.255.110]
TASK [template : tempalte transfer example] *****
changed: [192.168.255.110]
PLAY RECAP *****
192.168.255.110 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
cat /data/JAVA/order.conf
project: JAVA
switch: ON
dbport: 3306
跨平台Roles
编辑Inventory文件/etc/ansible/hosts
cat /etc/ansible/hosts
[ubuntu]
192.168.255.110
[centos]
192.168.255.101
针对不同的系统平台分别编辑httpd_db,httpd_rh的Role
mkdir -p roles/{httpd_db,httpd_rh}/tasks
roles/httpd_db/tasks/httpd.yaml
---
- name: ubuntu install httpd
apt:
name: mini-httpd
state: present
roles/httpd_db/tasks/main.yaml
---
- import_tasks: httpd.yaml
roles/httpd_rh/tasks/httpd.yaml
---
- name: centos install httpd
yum:
name: httpd
state: latest
roles/httpd_rh/tasks/main.yaml
---
- import_tasks: httpd.yaml
httpd.yaml
---
- name: all install httpd
hosts: all
roles:
- { role: httpd_db, when: ansible_os_family == 'Debian' }
- { role: httpd_rh, when: ansible_os_family == 'RedHat' }
ansible-playbook httpd.yaml
PLAY [all install httpd] ******
TASK [Gathering Facts] ******
ok: [192.168.255.110]
ok: [192.168.255.101]
TASK [httpd_db : ubuntu install httpd] ******
skipping: [192.168.255.101]
ok: [192.168.255.110]
TASK [httpd_rh : centos install httpd] ******
skipping: [192.168.255.110]
changed: [192.168.255.101]
PLAY RECAP ******
192.168.255.101 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
192.168.255.110 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
标签:httpd,tasks,roles,yaml,Ansible,file,apache,变更
From: https://www.cnblogs.com/anyux/p/18372007