ansible的roles角色

创建树状目录
[root@zxw8 ~]# mkdir -pv playbook/roles/{dbservers,webservers}/{files,handlers,tasks,templates,vars}
本地安装tree
[root@zxw8 ~]# cd Packages/
[root@zxw8 ~]# rpm -ivh tree-1.6.0-10.el7.x86_64.rpm
yum安装
[root@zxw8 ~]# yum install tree -y
查看playbook
[root@zxw8 ~]# tree playbook/
playbook/
└── roles
├── dbservers
│   ├── files
│   ├── handlers
│   ├── tasks
│   ├── templates
│   └── vars
└── webservers
├── files
├── handlers
├── tasks
├── templates
└── vars
创建任务tasks
[root@zxw8 playbook]# vim roles/dbservers/tasks/main.yaml
- name: service httpd restarted
service: name=httpd state=restarted
- name: touch file
command: touch /root/a.txt
[root@zxw8 playbook]# ls
Roles
创建执行任务的角色roles
[root@zxw8 playbook]# vim site.yaml
- hosts: zxw
remote_user: root
roles:
- dbservers
[root@zxw8 playbook]# ansible-playbook site.yaml
复制文件files
[root@zxw8 playbook]# cp /etc/httpd/conf/httpd.conf roles/dbservers/files/
[root@zxw8 playbook]# vim roles/dbservers/tasks/main.yaml

- name: service httpd restarted
service: name=httpd state=restarted
- name: touch file
command: touch /root/a.txt
- name: cp httpd.conf
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf

变量定义vars
[root@zxw8 playbook]# vim roles/dbservers/vars/main.yaml

file: httpd.conf

触发器:handlers
[root@zxw8 playbook]# vim roles/dbservers/tasks/main.yaml

- name: service httpd restarted
service: name=httpd state=restarted
- name: touch file
command: touch /root/a.txt
- name: cp httpd.conf
copy: src={{ file }} dest=/etc/httpd/conf/{{ file }}
notify:
- service httpd restarted
~
[root@zxw8 playbook]# vim roles/dbservers/handlers/main.yaml

- name: service httpd restarted
service: name=httpd state=restarted

Templates
[root@zxw8 playbook]# cp /etc/httpd/conf/httpd.conf roles/dbservers/templates/

[root@zxw8 ~]# vim playbook/roles/dbservers/tasks/main.yaml

- name: service httpd restarted
service: name=httpd state=restarted
- name: touch file
command: touch /root/a.txt
- name: cp httpd.conf
templates: src={{ file }} dest=/etc/httpd/conf/{{ file }}
notify:
- service httpd restarted
~
[zxw]
192.168.126.7 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123 port=80
192.168.126.6 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123 port=80


[root@zxw8 ~]# tree playbook/
playbook/
├── 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

 

posted on 2019-07-31 09:05  我就是我没毛病  阅读(149)  评论(0编辑  收藏  举报

导航