ansible-day07-20201122-roles详解-很厉害这个功能很实用

roles

多角色
不同角色互相调用任务

roles/
├── httpd
│   ├── files
│   │   └── httpd.conf
│   └── tasks
│       ├── copyfile.yml
│       ├── main.yml
│       ├── user.yml
│       └── yum.yml
├── memcache
├── mysql
└── nginx
    ├── tasks
    │   ├── group.yml
    │   ├── main.yml
    │   ├── restart.yml
    │   ├── start.yml
    │   ├── templ.yml
    │   ├── user.yml
    │   └── yum.yml
    └── template
        └── nginx.conf.j2

8 directories, 13 files

some_role.yml
- hosts: web
  remote_user: root
  roles:
    - httpd
    - nginx
   
# 多个角色一起生效
在nginx角色调用http角色的任务
roles/nginx/tasks/main.yml

- include: group.yml
- include: user.yml
- include: yum.yml
- include: templ.yml
- include: start.yml
- include: roles/httpd/tasks/copyfile.yml

roles/httpd/tasks/copyfile.yml

- name: copy file
  copy: src=/etc/httpd/conf/httpd.conf dest=/data/ owner=apache

多角色 标签 只执行固定的内容

roles
├── app
│   ├── tasks
│   │   ├── group.yml
│   │   ├── main.yml
│   │   ├── restart.yml
│   │   ├── start.yml
│   │   ├── templ.yml
│   │   ├── user.yml
│   │   └── yum.yml
│   └── template
│       └── nginx.conf.j2
├── httpd
│   ├── files
│   │   └── httpd.conf
│   └── tasks
│       ├── copyfile.yml
│       ├── main.yml
│       ├── user.yml
│       └── yum.yml
├── memcache
├── mysql
└── nginx
    ├── tasks
    │   ├── group.yml
    │   ├── main.yml
    │   ├── restart.yml
    │   ├── start.yml
    │   ├── templ.yml
    │   ├── user.yml
    │   └── yum.yml
    └── template
        └── nginx.conf.j2

11 directories, 21 files

some_role.yml

- hosts: web
  remote_user: root
  roles:
    - { role: httpd, tags: ['web','httpd'] }
    - { role: nginx, tags: ['web','nginx'] }
    - { role: app, tags: 'app' }

ansible-playbook -t web some_role.yml
# 只执行了带web标签的前2个 而没有第三个app的

当满足条件才执行

针对不同的服务器 采用不同的角色

- hosts: web
  remote_user: root
  roles:
    - { role: httpd, tags: ['web','httpd'],when: ansible_distribution_version ==
 "6.4" }
    - { role: nginx, tags: ['web','nginx'],when: ansible_distribution_version ==
 "7.4"}
    - { role: app, tags: 'app' }
# 系统版本为centos 6.4 执行httpd centos7.4执行nginx

roles 小集合

roles/app
├── files
│   └── vhosts.conf
├── handlers
│   └── main.yml
├── tasks
│   ├── copyfile.yml
│   ├── group.yml
│   ├── main.yml
│   ├── start.yml
│   ├── templ.yml
│   ├── user.yml
│   └── yum.yml
├── templates
│   └── httpd.conf.j2
└── vars
    └── main.yml

handlers/main.yml
- name: restart service
  service: name=httpd state=restarted

vars/main.yml
username: app
groupname: app

cp /etc/httpd/conf/httpd.conf ../templates/httpd.conf.j2

cat templates/httpd.conf.j2 | grep 'name }}'
User {{ username }}
Group {{ groupname }}

cat templates/httpd.conf.j2 | grep vcpu
Listen {{ ansible_processor_vcpus*10 }}

copyfile.yml
- name: copy config
  copy: src=vhosts.conf dest=/etc/httpd/conf.d/ owner=app
  
group.yml
- name: create group
  group: name=app system=yes gid=123
  
main.yml
- include: group.yml
- include: user.yml
- include: yum.yml
- include: templ.yml
- include: copyfile.yml
- include: start.yml

start.yml
- name: start service
  service: name=httpd state=started enabled=yes
  
templ.yml
- name: copy conf
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  notify: restart service
  
user.yml
- name: create user
  user: name=app group=app system=yes uid=123 shell=/sbin/nologin
  
yum.yml
- name: instal package
  yum: name=httpd

cat app_role.yml
- hosts: web
  remote_user: root
  roles:
    - app

ansible-playbook app_role.yml
# 就是没用到 when if for 

memcached

../memcached
├── tasks
│   ├── main.yml
│   ├── start.yml
│   ├── templ.yml
│   └── yum.yml
└── templates
    └── memcached.j2

main.yml
- include: yum.yml
- include: templ.yml
- include: start.yml

start.yml
- name: strat ervice
  service: name=memcached state=started enabled=yes
  
templ.yml
- name: copy conf
  template: src=memcached.j2 dest=/etc/sysconfig/memcached
  
yum.yml
- name: intall package
  yum: name=memcached

cp /etc/sysconfig/memcached templates/memcached.j2

memcached_role.yml
- hosts: web
  remote_user: root
  roles:
    - memcached

ansible-playbook memcached_role.yml


版权声明:本文为weixin_43862755原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。