一,Ansible Roles目录结构
Role能做什么
ansible官方给出的目录编排方式。使目录清晰明了。(固定写法)
Role目录结构
[root@ansible roles]# tree ./
├── httpd
│ ├── handlers
│ │ └── main.yml
│ ├── tasks
│ │ ├── config.yml
│ │ ├── group.yml
│ │ ├── install.yml
│ │ ├── main.yml
│ │ ├── start.yml
│ │ └── user.yml
│ ├── templates
│ │ └── httpd.conf.j2 (默认调用这个路径下的文件)
│ └── vars
│ └── main.yml
│(——group_vars/all 内也可以定义变量)
│ └──all
└── xxxx.yml (task内有 roles: xxxx.yml)
③Role方式编写Nginx
④Role方式编写Rsync、NFS、…
案例
二,NFS模块
NFS安装,配置,启动
[root@manager ansible_roles_prod]# mkdir nfs/{tasks,handlers,templates,vars} -p
[root@manager ansible_roles_prod]# cat nfs/tasks/main.yml
name: Install NFS Server yum: name: nfs-utils state: present
name: Configure NFS Server template: src: exports.j2 dest: /etc/exports notify: Restart NFS Server
name: Init NFS Server group: name: www gid: 666
name: Init NFS Server user: name: www uid: 666 group: www
name: Init NFS Share Directory file: path: /data state: directory owner: www group: www
name: Started NFS Server systemd: name: nfs state: started enabled: yes
[root@manager ansible_roles_prod]# cat nfs/handlers/main.yml
- name: Restart NFS Server systemd: name: nfs state: restarted
[root@manager ansible_roles_prod]# cat nfs/templates/exports.j2
/data 172.16.1.0/24(rw,async,all_squash,anonuid=666,anongid=666)
三,rsync模块
1.创建目录结构
[root@manager ansible_roles_prod]# mkdir rsync/{tasks,templates,files,handlers} -p
2.编写tasks
[root@manager ansible_roles_prod]# cat rsync/tasks/main.yml
name: Install Rsync Server yum: name: rsync state: present
name: Configure Rsync Server template: src: “{{ item.src }}” dest: “{{ item.dest }}” mode: “{{ item.mode }}” loop:
- {src: rsyncd.conf.j2 , dest: /etc/rsyncd.conf , mode: “0644” }
- {src: rsync.password.j2 , dest: /etc/rsync.passwd, mode: “0600” } notify: Restart Rsync Server
name: Init Rsync Group group: name: www gid: 666
name: Init Rsync User user: name: www uid: 666 group: www shell: /sbin/nologin create_home: no
name: Create Rsync Server Directory file: path: /backup state: directory owner: www group: www
- name: Started Rsync Server systemd: name: rsyncd state: started enabled: yes
[root@manager ansible_roles_prod]# cat rsync/handlers/main.yml
- name: Restart Rsync Server systemd: name: rsyncd state: restarted
[root@manager ansible_roles_prod]# cat rsync/templates/rsync.password.j2
uid = www gid = www port = 873 fake super = yes use chroot = no max connections = 200 timeout = 600 ignore errors read only = false list = false auth users = rsync_backup secrets file = /etc/rsync.passwd log file = /var/log/rsyncd.log
#
[backup] path = /backup
四,Web模块(Nginx)
1.创建nginx的目录结构
2.编写task
3.playbook调用
[root@manager ansible_roles_prod]# mkdir nginx/{tasks,handlers,templates,files} -p
[root@manager ansible_roles_prod]# cat nginx/tasks/main.yml
- name: Install Nginx Server
yum:
name: nginx
state: present
- name: Configure Nginx Server
template:
src: blog.oldxu.com.conf.j2
dest: /etc/nginx/conf.d/blog.oldxu.com.conf
notify: Restart Nginx Server
- name: Started Nginx Server
systemd:
name: nginx
state: started
enabled: yes
[root@manager ansible_roles_prod]# cat nginx/handlers/main.yml
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
[root@manager ansible_roles_prod]# cat group_vars/all
#web站点变量
blog_port: 80
blog_site: blog.oldxu.com.cn
[root@manager ansible_roles_prod]# cat nginx/templates/blog.oldxu.com.conf.j2
server {
listen {{ blog_port }};
server_name {{ blog_site }};
root /code/wordpress;
client_max_body_size 100m;
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
include fastcgi_params;
}
}
[root@manager ansible_roles_prod]# cat site.yml
- hosts: webservers
roles:
role: nginx
ansible-playbook site.yml 测试
目录为: [root@61 ]# tree /ansible_project1
/ansible_project1
├── ansible.cfg
├── hosts
└── site.yml
├── group_vars
│ └── all
└── nginx
├── flies
├── handlers
│ └── main.yml
├── tasks
│ └── main.yml
└── templates
├── nginx.conf.j2
└── phpadmin.com.conf.j2
作业: Role方式编写Proxy+Nginx+PHP+Redis项目 ( 2遍 )
1.roles方式
2.自定义变量(灵活)
3.facts变量
3.when语句,针对主机判断
4.loop循环,启动多个服务,拷贝多个文件
5.handlers
6.ignore_errors
7.change_when
文件描述符