角色是什麼
角色:
ansible 引用变量要用双引号引起来
defaults —main.yml主文件 默认变量
files 存放要拷贝静态文件
handlers ----main.yml
meat —main.yml 定义信息,依赖
tasks定义任务
templates 模版 .j2 模版
vars --main.yml 定义变量
角色里面的优先级高
主要就是将原来的文件分区域管理,显得更有条例,对应文件放在对应目录下,代码大体没有变化,只有格式发生了小部分变化
**举个例子
**
[devops@server1 ansible]$ pwd
/home/devops/ansible
[devops@server1 ansible]$mkdir roles
[devops@server1 ansible]$cd roles
[devops@server1 roles]$ ansible-galaxy init apache
- Role apache was created successfully
[devops@server1 roles]$ ls
apache
[devops@server1 roles]$cd /home/devops/ansible
[devops@server1 ansible]$ cd roles/apache
[devops@server1 apache]$ ls
defaults files handlers meta README.md tasks templates tests vars
[devops@server1 apache]$ ansible-galaxy list
# /usr/share/ansible/roles
# /etc/ansible/roles
[WARNING]: - the configured path /home/devops/.ansible/roles does not exist.
[devops@server1 apache]$ ll
total 4
drwxrwxr-x. 2 devops devops 22 Nov 23 19:54 defaults
drwxrwxr-x. 2 devops devops 6 Nov 23 19:54 files
drwxrwxr-x. 2 devops devops 22 Nov 23 19:54 handlers
drwxrwxr-x. 2 devops devops 22 Nov 23 19:54 meta
-rw-rw-r--. 1 devops devops 1328 Nov 23 19:54 README.md
drwxrwxr-x. 2 devops devops 22 Nov 23 19:54 tasks
drwxrwxr-x. 2 devops devops 6 Nov 23 19:54 templates
drwxrwxr-x. 2 devops devops 39 Nov 23 19:54 tests
drwxrwxr-x. 2 devops devops 22 Nov 23 19:54 vars
[devops@server1 apache]$ cd tasks/
[devops@server1 tasks]$ ls
main.yml
[devops@server1 tasks]$ vim main.yml
---
- name: install apache
yum:
name: httpd
state: present
- name: config apache
template:
src: httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: restart apache
- name: start apache
service:
name: httpd
state: started
- name: copy index.html
copy:
src: index.html
dest: /var/www/html/index.html
[devops@server1 ansible]$ pwd
/home/devops/ansible
[devops@server1 ansible]$ cd roles/apache/te
templates/ tests/
[devops@server1 ansible]$ cd roles/apache/te
templates/ tests/
[devops@server1 ansible]$ cd roles/apache/templates/
[devops@server1 templates]$ ls
httpd.conf
[devops@server1 templates]$ mv httpd.conf httpd.conf.j2
[devops@server1 templates]$ vim httpd.conf.j2
[devops@server1 templates]$ ll
total 12
-rw-r--r--. 1 root root 11753 Nov 23 20:08 httpd.conf.j2
[devops@server1 templates]$ exit
exit
You have new mail in /var/spool/mail/root
[root@server1 mnt]# chgrp devops ansible/
ansible.cfg hosts ssh.yml zabbix/
[root@server1 mnt]# chgrp devops /home/devops/ansible/roles/apache/templates/httpd.conf.j2
[root@server1 mnt]# chown devops /home/devops/ansible/roles/apache/templates/httpd.conf.j2
[root@server1 mnt]# su devops
[devops@server1 mnt]$ cd
[devops@server1 ~]$ cd ansible/roles/apache/
[devops@server1 apache]$ ls
defaults files handlers meta README.md tasks templates tests vars
[devops@server1 apache]$ vim templates/httpd.conf.j2
42 Listen {{ http_host }}:{{ http_port }}
[devops@server1 apache]$ vim vars/main.yml
---
http_host: "{{ ansible_hostname }}"
http_port: 80
[devops@server1 apache]$ cd ..
[devops@server1 roles]$ ls
apache
[devops@server1 roles]$ cd ..
[devops@server1 ansible]$ ls
ansible.cfg apache hosts roles ssh.yml zabbix
[devops@server1 ansible]$ vim apache.yml
[devops@server1 ansible]$ cat hosts
[db]
172.25.62.2
[web]
172.25.62.3
[zabbix:children]
db
web
#[webserver:vars]
#http_port=80
[devops@server1 ansible]$ ls
ansible.cfg apache apache.yml hosts roles ssh.yml zabbix
[devops@server1 ansible]$ cd roles/
[devops@server1 roles]$ cd apache/
[devops@server1 apache]$ ls
defaults files handlers meta README.md tasks templates tests vars
[devops@server1 apache]$ ll templates/
total 12
-rw-r--r--. 1 devops devops 11782 Nov 23 20:12 httpd.conf.j2
[devops@server1 apache]$ vim defaults/main.yml
[devops@server1 apache]$ vim tasks/main.yml
[devops@server1 apache]$ vim tasks/main.yml
[devops@server1 apache]$ ansible-playbook apache.yml
ERROR! the playbook: apache.yml could not be found
[devops@server1 apache]$ cd ..
[devops@server1 roles]$ cd ..
[devops@server1 ansible]$ ansible-playbook apache.yml
[devops@server1 ansible]$ cd roles/apache/
[devops@server1 apache]$ ls
defaults files handlers meta README.md tasks templates tests vars
[devops@server1 apache]$ vim handlers/
[devops@server1 apache]$ vim handlers/main.yml
[devops@server1 apache]$ vim tasks/main.yml
[devops@server1 apache]$ vim handlers/main.yml
[devops@server1 apache]$ cd ..
[devops@server1 roles]$ cd ..
[devops@server1 ansible]$ ansible-playbook apache.yml
PLAY [web] ********************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************
ok: [172.25.62.3]
TASK [apache : install apache] ************************************************************************************
ok: [172.25.62.3]
TASK [apache : config apache] *************************************************************************************
ok: [172.25.62.3]
TASK [apache : start apache] **************************************************************************************
changed: [172.25.62.3]
TASK [apache : copy index.html] ***********************************************************************************
changed: [172.25.62.3]
PLAY RECAP ********************************************************************************************************
172.25.62.3 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[devops@server1 ansible]$ curl 172.25.62.3
www.westos.org
开始正文推送zabbix
**
建立四个角色
权限都给为777
**
**大体架构,根据原来文件配置,将对应文件归位
模版,静态文件。主文件,触发器文件分别放在相应文件夹下
**
**数据库文件
**数据库配置文件
注意改掉格式
去掉host和tasks
此处我删掉了火墙部分不想干扰太大
触发器文件去掉开头
Web块
一样的修改方式
Agent块
Server块
修改
Zabbix要退的文件文件,调用各个角色
互相连接解决问题
再推
Host文件3作web db 2作agent server
全部关火墙
报错
数据库初始化
再推
找不到镜像路径
检查网络
将所有虚拟机配通能上网就好