role综合测试

1、模拟安装httpd

[ ansible]# tree 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

tree目录层级

[ tasks]# cat main.yml 
- include: group.yml
- include: user.yml
- include: yum.yml
- include: templ.yml
- include: copyfile.yml
- include: start.yml
[ tasks]# cat group.yml 
- name: create group
  group: name=app
[ tasks]# cat user.yml 
- name: create user
  user: name=app group=app
[ tasks]# cat yum.yml 
- name: install package
  yum: name=httpd
[ tasks]# cat templ.yml 
- name: copy conf
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  notify: restart service

[ tasks]# cat copyfile.yml 
- name: copy cofig
  copy: src=vhosts.conf dest=/etc/httpd/conf.d/ owner=app
[ tasks]# cat start.yml 
- name: start service
  service: name=httpd state=started enabled=yes

tasks

#这里碰到一个坑,如果把main.yml 给成其他名字报错如下
ERROR! The requested handler ‘restart service‘ was not found in either the main handlers list nor in the listening handlers list
#抗我了2个小时尴尬
[ app]# cat handlers/main.yml 
- name: restart service
  service: name=httpd state=restarted

handlers/main.yml

[ ansible]# cat roles/app/vars/main.yml 
username: app
groupname: app
[ ansible]# cat app_role.yml 
- hosts: date
  remote_user: root

  roles:
    - app

templates、files为测试目录自己改吧改吧,测试成功哈哈

role综合测试

role综合测试

相关推荐