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.yamlweb

- 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.confvim

變量定義vars
[root@zxw8 playbook]# vim roles/dbservers/vars/main.yamlssh

file: httpd.confspa

觸發器:handlers
[root@zxw8 playbook]# vim roles/dbservers/tasks/main.yaml rest

- 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.yamlserver

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

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

[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=80file


[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

相關文章
相關標籤/搜索