ansible-roles

ansible-roles

roles的作用是减少代码的复写
[root@localhost ~]# tree playbooks/
playbooks/
├── roles
│   ├── dbservers
│   │   ├── files
│   │   │   └── httpd.conf
│   │   ├── handlers
│   │   │   └── main.yaml
│   │   ├── tasks
│   │   │   └── main.yaml
│   │   ├── templates
│   │   │   └── httpd.conf
│   │   └── vars
│   │   └── main.yaml
│   └── webservers
│   ├── files
│   ├── handlers
│   ├── tasks
│   ├── templates
│   └── vars
└── site.yaml


#vim playbooks/roles/dbservers/tasks/main.yaml
- name: create {{ file1 }}
shell: touch {{ file1 }}
- name: create {{ file2 }}
shell: touch {{ file2 }}
- name: copy httpd.conf
template: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
#vim playbooks/roles/dbservers/handlers/main.yaml
- name: restart httpd
service: name=httpd state=restarted


#vim playbooks/roles/dbservers/vars/main.yaml
file1: file1.txt
file2: file2.txt
file3: file3.txt

# cat site.yaml
- hosts: testhosts
remote_user: root
roles:
- dbservers

#files/ 用copy模块来拷贝文件,src直接指定文件名字,不需要绝对路径
#templates/ 用template模块来拷贝文件,src直接指定文件名字,不需要绝对路径

你可能感兴趣的:(ansible-roles)