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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值