一、Ansible Roles 介绍
它允许管理员将他们复杂的PlayBook分解成一个个小的逻辑单元,以便于维护和管理。
二、Roles结构
1、Role是什么
先看一个示例
root@zsh-virtual-machine:~# tree nginx
nginx
├── files
│ └── alonzo.txt
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ └── config.j2
└── vars
└── main.yaml
5 directories, 5 files
从表面上看,它就是一个目录,目录的名字也就是role的名字,示例中role的名字就叫做nginx
在这个role名字的目录里,会发现好多子目录
使用时,每个目录必须包含一个main.yaml,这个文件应该包含如下目录名称对应的内容:
- tasks 包含角色要执行的主要列表
- handlers 包含处理程序,此角色甚至该角色之外的任何地方都可以使用这些处理程序
- defaults 校色的默认变量
- vars 角色的其他变量
- files 包含可以通过角色部署的文件
- templates 包含可以通过此角色部署的模板
- meta 为此角色定义一些元数据
注意:这些目录用不到时可以删除
三、制作一个Role
将之前笔记中的一个完整的PlayBook分解成一个Role,那么应该怎么做呢?
最终优化的PlayBook
- name: install nginx or httpd
hosts: all
remote_user: root
vars:
username:
- user1
- user2
- user3
tasks:
- name: create user
user:
name: "{{ item }}"
home: /home/{{ item }}
loop: "{{ username }}"
- name: install nginx ubuntu
apt:
name: nginx
state: present
update_cache: yes
when:
- ansible_pkg_mgr == 'apt'
notify: ubuntu
- name: install httpd centos
yum:
name: httpd
state: present
when:
- ansible_pkg_mgr == 'yum'
notify: centos
- name: copy file
copy:
src: /root/alonzo.txt
dest: /root/alonzo.txt
- name: update jinjia2 config
template:
src: config.j2
dest: /root/cfg.conf
- name: check nginx config
shell: /usr/sbin/nginx -t
register: nginx_check_config
tags: restart_nginx
handlers:
- name: ubuntu
service:
name: nginx
state: restarted
when:
- nginx_check_config.rc == 0
tags: restart_nginx
- name: centos
service:
name: httpd
state: restarted
分解这个PlayBook,命名role的名字为nginx
nginx
├── files
│ └── alonzo.txt
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ └── config.j2
└── vars
└── main.yaml
files文件夹存放需要copy的文件
templates文件夹需要存放Jinja2文件
{# check system #}
{{ ansible_date_time.date }}
hostname:{{ ansible_hostname }}
os:{{ansible_os_family}}
ip:{{ ansible_default_ipv4.address }}
{% for m in ansible_mounts if m['mount'] != "/" %}
mount:{{ m['mount'] }} total size:{{ m['size_total'] }} free_size:{{ 'size_available' }}
{% endfor %}
memory_total:{{ ansible_memory_mb.real.total }} memory_free:{{ ansible_memory_mb.real.free }}
{% if ansible_processor_vcpus > 1 %}
system cpu core more than one core
{% endif %}
handlers文件夹中的main.yaml文件
- name: ubuntu
service:
name: nginx
state: restarted
when:
- nginx_check_config.rc == 0
tags: restart_nginx
- name: centos
service:
name: httpd
state: restarted
vars文件夹中的main.yaml文件
username:
- user1
- user1
- user1
tasks文件夹中的main.yaml文件
- name: create user
user:
name: "{{ item }}"
home: /home/{{ item }}
loop: "{{ username }}"
- name: install nginx ubuntu
apt:
name: nginx
state: present
update_cache: yes
when:
- ansible_pkg_mgr == 'apt'
notify: ubuntu
- name: install httpd centos
yum:
name: httpd
state: present
when:
- ansible_pkg_mgr == 'yum'
notify: centos
- name: copy file
copy:
src: alonzo.txt
dest: /root/alonzo.txt
- name: update jinjia2 config
template:
src: config.j2
dest: /root/cfg.conf
- name: check nginx config
shell: /usr/sbin/nginx -t
register: nginx_check_config
tags: restart_nginx
四、执行方式
1、经典方式
目录结构
root@zsh-virtual-machine:~/server# pwd
/root/server
root@zsh-virtual-machine:~/server# tree
.
├── hosts
├── nginx
│ ├── files
│ │ └── alonzo.txt
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ │ └── config.j2
│ └── vars
│ └── main.yaml
└── nginx_test.yaml
nginx_test.yaml文件内容
- name: a playbook used role
hosts: all
roles:
- nginx
执行命令
root@zsh-virtual-machine:~/server# ansible-playbook -i hosts nginx_test.yaml
2、新方式
目录结构
root@zsh-virtual-machine:~/server# pwd
/root/server
root@zsh-virtual-machine:~/server# tree
.
├── hosts
├── nginx
│ ├── files
│ │ └── alonzo.txt
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ │ └── config.j2
│ └── vars
│ └── main.yaml
├── nginx_new.yaml
└── nginx_test.yaml
6 directories, 8 files
nginx_new.yaml文件内容
- name: new role
hosts: all
tasks:
- name: print start
debug:
msg: "roles start"
- import_role:
name: nginx
- name: print finsh
debug:
msg: "roles finsh"
执行命令
root@zsh-virtual-machine:~/server# ansible-playbook -i hosts nginx_new.yaml