学习目标:
能够编写简单的role角色
学习内容:
[root@zabbix_server ansible]# tree roles/app/
roles/app/
├── files
│ └── testfile.conf
├── handlers
│ └── main.yml
├── tasks
│ ├── file.yml
│ ├── group.yml
│ ├── main.yml
│ ├── start.yml
│ ├── templ.yml
│ ├── user.yml
│ └── yum.yml
├── templates
│ └── httpd.conf.j2
└── vars
└── main.yml
5 directories, 11 files
[root@zabbix_server tasks]# cat file.yml
- name: copy file
copy: src=testfile.conf dest=/etc/httpd/conf.d/ owner=app
[root@zabbix_server tasks]# cat group.yml
- name: create group
group: name=app system=yes gid=80
[root@zabbix_server tasks]# cat user.yml
- name: create user
user: name=app system=yes group=app shell=/sbin/nologin uid=80
[root@zabbix_server tasks]# cat start.yml
- name: start service
service: name=httpd state=started enabled=yes
[root@zabbix_server tasks]# cat templ.yml
- name: copy templates
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: restart service
[root@zabbix_server tasks]# cat yum.yml
- name: install package
yum: name=httpd
[root@zabbix_server tasks]# cat main.yml
- include: group.yml
- include: user.yml
- include: yum.yml
- include: templ.yml
- include: file.yml
- include: start.yml
-rw-r--r-- 1 root root 0 May 3 10:44 testfile.conf
[root@zabbix_server files]# pwd
/etc/ansible/roles/app/files
[root@zabbix_server app]# cd handlers/
[root@zabbix_server handlers]# ll
total 4
-rw-r--r-- 1 root root 62 May 3 10:49 main.yml
[root@zabbix_server handlers]# cat main.yml
- name: restart service
service: name=httpd state=restarted
-rw-r--r-- 1 root root 11801 May 3 10:42 httpd.conf.j2
[root@zabbix_server templates]# grep '{{.*}}' httpd.conf.j2
Listen {{ ansible_processor_vcpus*100 }}
User {{ username }}
Group {{ groupname }}
[root@zabbix_server templates]# pwd
/etc/ansible/roles/app/templates
[root@zabbix_server app]# cd vars/
[root@zabbix_server vars]# ll
total 4
-rw-r--r-- 1 root root 29 May 3 10:46 main.yml
[root@zabbix_server vars]# cat main.yml
username: app
groupname: app
[root@zabbix_server ansible]# cat httpd_role.yml
- hosts: LYP
remote_user: root
roles:
- app
[root@zabbix_server ansible]# ansible-playbook httpd_role.yml
PLAY [LYP] **************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [172.28.102.131]
ok: [172.28.102.130]
TASK [app : create group] ***********************************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]
TASK [app : create user] ************************************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]
TASK [app : install package] ********************************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]
TASK [app : copy templates] *********************************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]
TASK [app : copy file] **************************************************************************************************************************************************************************************
changed: [172.28.102.131]
changed: [172.28.102.130]
TASK [app : start service] **********************************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]
RUNNING HANDLER [app : restart service] *********************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]
PLAY RECAP **************************************************************************************************************************************************************************************************
172.28.102.130 : ok=8 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.28.102.131 : ok=8 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0