1、模拟安装httpd
1 [root@linux-node1 ansible]# tree roles/ 2 roles/ 3 ├── app 4 │ ├── files 5 │ │ └── vhosts.conf 6 │ ├── handlers 7 │ │ └── main.yml 8 │ ├── tasks 9 │ │ ├── copyfile.yml 10 │ │ ├── group.yml 11 │ │ ├── main.yml 12 │ │ ├── start.yml 13 │ │ ├── templ.yml 14 │ │ ├── user.yml 15 │ │ └── yum.yml 16 │ ├── templates 17 │ │ └── httpd.conf.j2 18 │ └── vars 19 │ └── main.yml
1 [root@linux-node1 tasks]# cat main.yml 2 - include: group.yml 3 - include: user.yml 4 - include: yum.yml 5 - include: templ.yml 6 - include: copyfile.yml 7 - include: start.yml 8 [root@linux-node1 tasks]# cat group.yml 9 - name: create group 10 group: name=app 11 [root@linux-node1 tasks]# cat user.yml 12 - name: create user 13 user: name=app group=app 14 [root@linux-node1 tasks]# cat yum.yml 15 - name: install package 16 yum: name=httpd 17 [root@linux-node1 tasks]# cat templ.yml 18 - name: copy conf 19 template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf 20 notify: restart service 21 22 [root@linux-node1 tasks]# cat copyfile.yml 23 - name: copy cofig 24 copy: src=vhosts.conf dest=/etc/httpd/conf.d/ owner=app 25 [root@linux-node1 tasks]# cat start.yml 26 - name: start service 27 service: name=httpd state=started enabled=yes
1 #这里碰到一个坑,如果把main.yml 给成其他名字报错如下 2 ERROR! The requested handler 'restart service' was not found in either the main handlers list nor in the listening handlers list 3 #抗我了2个小时尴尬 4 [root@linux-node1 app]# cat handlers/main.yml 5 - name: restart service 6 service: name=httpd state=restarted
[root@linux-node1 ansible]# cat roles/app/vars/main.yml username: app groupname: app
[root@linux-node1 ansible]# cat app_role.yml - hosts: date remote_user: root roles: - app
templates、files为测试目录自己改吧改吧,测试成功哈哈