其他分享
首页 > 其他分享> > Anisble Roles

Anisble Roles

作者:互联网

Anisble Roles

Roles基本概述

roles不管是Ansible还是saltstack,我在写一键部署的时候,都不可能把所有的步骤全部写入到一个'剧本'文件当中,
我们肯定需要把不同的工作模块,拆分开来,解耦,那么说到解耦,我们就需要用到roles官方推荐,因为roles的目
录结构层次更加清晰。
例如:我们之前推荐大家写一个base.yml里面写所有基础优化的项目,其实把所有东西摞进去也是很鸡肋的,不如
我们把这些功能全部拆分开,谁需要使用,就调用即可。
建议:每个roles最好只使用一个tasks这样方便我们去调用,能够很好的做到解耦。(SOA)

Roles目录结构规范

image

## 初始化项目
[Sun Aug 22 01:09:28 root@m01 /ansible_project]
 # mkdir roles
[Sun Aug 22 01:09:28 root@m01 /ansible_project]
 # cd roles/
[Sun Aug 22 01:34:54 root@m01 /ansible_project/roles]
 # ansible-galaxy init nginx
   ansible-galaxy init nfs
   ansible-galaxy init php
   ansible-galaxy init base
   ....
[Sun Aug 22 01:34:47 root@m01 /ansible_project/roles]
 # ll
total 4
drwxr-xr-x 10 root root 154 Aug 18 18:09 base
drwxr-xr-x 10 root root 154 Aug 18 18:48 create_user
drwxr-xr-x 10 root root 154 Aug 18 18:09 keepalived
drwxr-xr-x 10 root root 154 Aug 18 18:11 lb
drwxr-xr-x 10 root root 154 Aug 18 18:09 mariadb
drwxr-xr-x 10 root root 154 Aug 18 18:12 mount
drwxr-xr-x 10 root root 154 Aug 18 18:09 nfs
drwxr-xr-x 10 root root 154 Aug 18 18:07 nginx
drwxr-xr-x 10 root root 154 Aug 18 18:08 php
drwxr-xr-x 10 root root 154 Aug 18 18:44 rsync_client
drwxr-xr-x 10 root root 154 Aug 18 18:08 rsync_server
drwxr-xr-x 10 root root 154 Aug 18 18:08 sersync
-rw-r--r--  1 root root   3 Aug 18 19:07 site.yml
drwxr-xr-x 10 root root 154 Aug 18 18:10 wordpress

roles 中的依赖关系

roles允许你再使用roles时自动引入其他的roles。role依赖关系存储在roles目录中meta/main.yml文件中。 例如:推送wordpress并解压,前提条件,必须要安装nginx和php,把服务跑起来,才能运行wordpress的页面, 此时我们就可以在wordpress的roles中定义依赖nginx和php的roles

[Sun Aug 22 01:45:51 root@m01 /ansible_project/roles]
 # vim wordpress/meta/main.yml 
dependencies:
  - {role: nginx}
  - {role: php}
  - {role: mariadb}
  
#举例: 如果要执行这个Wordpress 那么他走的流程是怎么样的?
首先系统会先查看/wordpress/meta/main.yml这里面的内容 是否有相关的依赖 因为wordpress需要依赖Nginx ,PHP。mariadb, 所以先到Nginx下的/meta/main.yml 去查看有没有相关的依赖,如果有继续找依赖 没有的话 就再看nginx里面的tasks里的main.yml, 这个里面一般装的是included install,config,start nginx ,如果在安装的过程中需要用到template模块中的东西 ,那么它会去nginx/template中去找相关的文件,如果找的东西是copy模块里的东西 ,那么它会去nginx/files中去找相关的文件 所以所有的文件我们都不要写绝对路径,就写文件名就行   ./ 都不需要加, nginx安装完之后 会再去查看php/meta/main.yml里的依赖关系,没有需要依赖的就会在去查看task/main.yml 是copy的就去找files里的 是templatede 就去找templates里的,安装好php之后  ,再继续去mariadb/meta/main.yml查看依赖 同理去找各个文件  要记住在执行任何任务之前 都是需要先去meta下的main.yml去查看依赖,同时注意依赖不要写成死循环,互相依赖.   找完依赖就找tasks, 执行任务过程如果用到变量 那就去vas里去看 handlers里装的是触发器

roles重构rsync

# 1.提前准备rsync配置文件
[Sun Aug 22 02:29:29 root@m01 /ansible_project/roles/rsync_server/templates]
 # vim /ansible_project/roles/rsync_server/templates/rsyncd.conf.j2 
uid = {{ name }}
gid = {{ name }}
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = {{ rsync_user }}
secrets file = {{ rsync_passwd_file }}
log file = /var/log/rsyncd.log
#####################################
[{{ backup_dir }}]
comment = welcome to oldboyedu backup!
path = /{{ backup_dir }}

# 2.编辑变量文件
[Sun Aug 22 02:34:21 root@m01 /ansible_project/roles/rsync_server]
 # vim vars/main.yml 
name: www
rsync_user: rsync_backup
rsync_passwd_file: /etc/rsync.passwd
backup_dir: backup
rsync_passwd: 123


# 3.主机清单
[Sun Aug 22 02:40:43 root@m01 /ansible_project/roles/rsync_server]
 # vim /etc/ansible/hosts 
[web_group]
web01 ansible_ssh_host=172.16.1.7
web02 ansible_ssh_host=172.16.1.8

[db_group]
db01 ansible_ssh_host=172.16.1.51

[nfs_group]
nfs ansible_ssh_host=172.16.1.31

[bakcup_group]
backup ansible_ssh_host=172.16.1.41

[lb_group]
lb01 ansible_ssh_host=172.16.1.5
lb02 ansible_ssh_host=172.16.1.6

# 4.安装rsync
[Sun Aug 22 02:42:31 root@m01 /ansible_project/roles/rsync_server]
 # vim tasks/install_rsync.yml 
- name: Install Rsync
  yum:
    name: rsync
    
# 5.配置rsync
[Sun Aug 22 02:51:36 root@m01 /ansible_project/roles/rsync_server]
 # vim tasks/config_rsync.yml 
- name: Push Rsync Conf
  template:
    src: rsyncd.conf.j2
    dest: /etc/rsyncd.conf
  notify: restart rsync

- name: Create Passwd File
  copy:
    content: "{{ rsync_user }}:{{ rsync_passwd }}"
    dest: "{{ rsync_passwd_file }}"
    mode: 0600

- name: Create {{ backup_dir }}
  file:
    path: /{{ backup_dir }}
    state: directory
    owner: "{{ name }}"
    group: "{{ name }}"
    
 # 6.启动rsync
 [Sun Aug 22 02:53:09 root@m01 /ansible_project/roles/rsync_server]
 # vim tasks/start_rsync.yml 
- name: Start Rsync
  service:
    name: rsyncd
    state: started
    enabled: yes

# 7.rsync触发器
[Sun Aug 22 03:00:22 root@m01 /ansible_project/roles/rsync_server]
 # vim handlers/main.yml 
---
# handlers file for rsync
- name: restart rsync
  service:
    name: rsyncd
    state: restarted
 
# 8.依赖
[Sun Aug 22 03:17:45 root@m01 /ansible_project/roles/rsync_server]
 # vim meta/main.yml
dependencies:
  - {role: create_user}

# 9.rsync include
[Sun Aug 22 03:20:16 root@m01 /ansible_project/roles/rsync_server]
 # vim tasks/main.yml
---
# tasks file for rsyn
- include_tasks: install_rsync.yml
- include_tasks: config_rsync.yml
- include_tasks: start_rsync.yml
   
### rsync依赖创建用户
[Sun Aug 22 03:04:41 root@m01 /ansible_project/roles]
 #ansible-galaxy init create_user
[Sun Aug 22 03:05:47 root@m01 /ansible_project/roles]
 # cd create_user/
[Sun Aug 22 03:06:27 root@m01 /ansible_project/roles/create_user]
 # vim vars/main.yml 
---
# vars file for create_user
name: www
id: 666
login_shell: /sbin/nologin
create_home: no


# 创建用户和组
[Sun Aug 22 03:11:40 root@m01 /ansible_project/roles/create_user]
 # vim tasks/main.yml 
---
# tasks file for create_user
- name: Create {{ name }} Group
  group:
    name: "{{ name }}"
    gid: "{{ id }}"

- name: Create {{ name }} User
  user:
    name: "{{ name }}"
    uid: "{{ id }}"
    group: "{{ name }}"
    shell: "{{ login_shell }}"
    create_home: "{{ create_home }}"
    
## site.yml
[Sun Aug 22 03:36:45 root@m01 /ansible_project/roles]
 # vim site.yml 
- hosts: all
  roles:
    - {role: rsync_server,when: ansible_hostname == 'backup'}

导数据库数据

#先提前把数据库里的数据导出来
[Sat Aug 21 20:24:15 root@web01 ~]
 # cd /code/
[Sat Aug 21 20:46:54 root@web01 /code]
 # ll
total 4
drwxr-xr-x. 5 www www 4096 Aug 21 20:44 wordpress
[Sat Aug 21 20:46:56 root@web01 /code]
 # df -h
Filesystem         Size  Used Avail Use% Mounted on
/dev/sda3           19G  1.7G   17G  10% /
devtmpfs           476M     0  476M   0% /dev
tmpfs              487M     0  487M   0% /dev/shm
tmpfs              487M   14M  473M   3% /run
tmpfs              487M     0  487M   0% /sys/fs/cgroup
/dev/sda1          497M  120M  378M  25% /boot
tmpfs               98M     0   98M   0% /run/user/0
172.16.1.31:/data   19G  1.6G   17G   9% /code/wordpress/wp-content/uploads
[Sat Aug 21 20:48:21 root@web01 /code]
 # umount /code/wordpress/wp-content/uploads
[Sat Aug 21 20:49:29 root@web01 /code]
 # tar zcf wp.tgz wordpress/
[Sat Aug 21 20:50:52 root@web01 /code]
 # ll
total 16052
drwxr-xr-x. 5 www  www      4096 Aug 21 20:44 wordpress
-rw-r--r--. 1 root root 16431373 Aug 21 20:50 wp.tgz

#(要去nfs里也要打个包,数据都在这里,上面打包的那个是没有数据的)
[Sat Aug 21 20:53:06 root@nfs ~]
 # cd /data/
[Sat Aug 21 20:53:17 root@nfs /data]
 # ll
total 0
drwxr-xr-x. 3 www www 16 Aug 21 20:42 2021
[Sat Aug 21 20:55:31 root@nfs /data]
 # tar zcf user_data.tgz 2021/
[Sat Aug 21 20:55:53 root@nfs /data]
 # ll
total 244
drwxr-xr-x. 3 www  www      16 Aug 21 20:42 2021
-rw-r--r--. 1 root root 247661 Aug 21 20:55 user_data.tgz


[Sun Aug 22 05:02:53 root@m01 ~]
 # vim mysql.yml 
- hosts: db01
  tasks:
    - name: Dump Mysql Data
      mysql_db: 
        name: wordpress
        state: dump
        target: /tmp/wordpress.sql
[Sun Aug 22 05:02:11 root@m01 ~]
 # ansible-playbook mysql.yml
 Last login: Sun Aug 22 04:23:02 2021
 (导出之后在db01查询一下导出结果)
[Sun Aug 22 04:24:21 root@db01 ~]
 # ll /tmp/
total 784
drwx------ 3 root root     17 Aug 22 04:26 systemd-private-c931f4e16b8a4629a80e181d75811ef7-mariadb.service-H5SjFS
drwx------ 2 root root      6 Aug 22 04:22 vmware-root_5399-4122257852
-rw-r--r-- 1 root root 798880 Aug 22 05:02 wordpress.sql
[Sun Aug 22 05:04:44 root@db01 ~]
 # cd /tmp/
[Sun Aug 22 05:07:14 root@db01 /tmp]
 # sz wordpress.sql

然后记得除了backup和m01 其他的虚拟机需要还原一下快照 再做其他的

roles重构wordpress

## 先决条件
# 1.下发公钥
[Sun Aug 22 05:02:59 root@m01 ~]
 # sh ssh_key.sh
 
#(上传之前打包好的安装包和数据)
[Sun Aug 22 05:17:11 root@m01 /ansible_project/roles/wordpress]
 # cd files/
[Sun Aug 22 05:18:56 root@m01 /ansible_project/roles/wordpress/files]
 # rz 

[Sun Aug 22 05:19:22 root@m01 /ansible_project/roles/wordpress/files]
 # ll
total 17076
-rw-r--r-- 1 root root   247661 Aug 21 20:55 user_data.tgz
-rw-r--r-- 1 root root   798880 Aug 22 05:02 wordpress.sql
-rw-r--r-- 1 root root 16431373 Aug 21 20:50 wp.tgz

# 2.nginx配置文件
[Sun Aug 22 05:21:26 root@m01 /ansible_project/roles/wordpress]
 # cd templates/
[Sun Aug 22 05:59:54 root@m01 /ansible_project/roles/wordpress]
 # vim templates/blog.wk.com.j2
server {
    listen 80;
    server_name {{ blog_server_name }};
    root /{{ code_dir }}/{{ service_name }};
    index index.php;

    location ~ \.php$ {
        fastcgi_pass unix:{{ socket_file }};
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
}

# 3.变量文件
[Sun Aug 22 05:58:52 root@m01 /ansible_project/roles/wordpress]
 # vim vars/main.yml 
---
# vars file for wordpress
blog_server_name: www.wk.com
code_dir: code
service_name: wordpress
socket_file: /dev/shm/php71w.sock
#(这里面的变量可以调用依赖里的变量 因为name这个变量在create_user中已经定义了 在这里就不重复定义了,其他的变量是建议哪个项目的变量 就在哪个项目的vars里定义)

# 4.依赖关系
[Sun Aug 22 05:30:51 root@m01 /ansible_project/roles/wordpress]
 # cd meta/
[Sun Aug 22 05:40:13 root@m01 /ansible_project/roles/wordpress/meta]
 # vim main.yml 
dependencies:
  - {role: nginx}
  - {role: php}
  - {role: mariadb}

# 5.task
[Sun Aug 22 05:56:40 root@m01 /ansible_project/roles/wordpress/tasks]
 # vim main.yml
---
# tasks file for wordpress
- name: Create /{{ code_dir }} Dir
  file:
    path: /{{ code_dir }}
    state: directory
    owner: "{{ name }}"
    group: "{{ name }}"

- name: Unarchive {{ service_name }}
  unarchive:
    src: wp.tgz
    dest: /{{ code_dir }}
    owner: "{{ name }}"
    group: "{{ name }}"

- name: Push Blog Nginx File
  template:
    src: "{{ blog_server_name }}.j2"
    dest: /etc/nginx/conf.d/{{ blog_server_name }}.conf
  notify: restart nginx

- name: chmod var lib
  file:
    path: /var/lib/nginx
    recurse: yes
    owner: "{{ name }}"
    group: "{{ name }}"
 
# 6.handlers
[Sun Aug 22 06:08:07 root@m01 /ansible_project/roles/wordpress]
 # cd handlers/
 [Sun Aug 22 06:08:10 root@m01 /ansible_project/roles/wordpress/handlers]
 # vim main.yml 
---
# handlers file for wordpress
- name: restart nginx
  service:
    name: nginx
    state: reloaded
    
## wordpress依赖nginx
[Sun Aug 22 06:12:09 root@m01 /ansible_project/roles]
 # cd nginx/
[Sun Aug 22 06:12:12 root@m01 /ansible_project/roles/nginx]
 # vim meta/main.yml 
dependencies:
  - {role: create_user}
  
# 安装nginx
[Sun Aug 22 06:13:10 root@m01 /ansible_project/roles/nginx]
 # vim tasks/install.yml
- name: Install Nginx
  yum:
    name: nginx
    
# 配置nginx
[Sun Aug 22 06:14:25 root@m01 /ansible_project/roles/nginx]
 # vim tasks/config.yml
- name: Configure Nginx Conf
  template:
    src: nginx.j2
    dest: /etc/nginx/nginx.conf
  notify: restart nginx
    
# 启动nginx
[Sun Aug 22 06:17:06 root@m01 /ansible_project/roles/nginx]
 # vim tasks/start.yml
- name: Start Nginx
  service:
    name: nginx
    state: started
    enabled: yes
    

[Sun Aug 22 06:20:51 root@m01 /ansible_project/roles/nginx]
 # vim templates/nginx.j2
user www;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    client_max_body_size 200m;
user {{ name }};
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    client_max_body_size 200m;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
"templates/nginx.j2" [New] 51L, 1183C written

# include
[Sun Aug 22 06:24:39 root@m01 /ansible_project/roles/nginx]
 # vim tasks/main.yml
---
# tasks file for nginx
- include_tasks: install.yml
- include_tasks: config.yml
- include_tasks: start.yml

# handlers
[Sun Aug 22 06:27:24 root@m01 /ansible_project/roles/nginx]
 # vim handlers/main.yml 
---
# handlers file for nginx
- name: restart nginx
  service:
    name: nginx
    state: reloaded
    
## wordpress 依赖 php
# php配置文件
[Sun Aug 22 06:29:35 root@m01 /ansible_project/roles/nginx]
 # cd ..
[Sun Aug 22 06:34:44 root@m01 /ansible_project/roles]
 # cd php/
[Sun Aug 22 06:34:48 root@m01 /ansible_project/roles/php]
 # vim templates/www.j2
[www]
user = {{ name }}
group = {{ name }}
listen = {{ socket_file }}
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache

#把php的安装包下载到 /ansible_project/roles/php/files这个目录里 



# 安装php
[Sun Aug 22 06:36:34 root@m01 /ansible_project/roles/php]
 # vim tasks/install.yml
- name: panduan php
  shell: 'rpm -qa|grep php'
  register: get_php
  ignore_errors: yes

- name: Unarchive PHP
  unarchive:
    src: php.tgz
    dest: /tmp

- name: Install PHP
  shell: 'rpm -Uvh /tmp/*.rpm'
  when: get_php.rc != 0


# 配置php
[Sun Aug 22 06:38:52 root@m01 /ansible_project/roles/php]
 # vim tasks/config.yml
- name: Conf PHP
  template:
    src: www.j2
    dest: /etc/php-fpm.d/www.conf
  notify:
    - restart php
    - chmod sock
    
# 启动php
[Sun Aug 22 06:43:22 root@m01 /ansible_project/roles/php]
 # vim tasks/start.yml
- name: Start PHP
  service:
    name: php-fpm
    state: started
    enabled: yes
 
# incllude
[Sun Aug 22 06:52:25 root@m01 /ansible_project/roles/php]
 # vim tasks/main.yml 
---
# tasks file for php
- include_tasks: install.yml
- include_tasks: config.yml
- include_tasks: start.yml

# handlers
Sun Aug 22 06:47:53 root@m01 /ansible_project/roles/php]
 # vim handlers/main.yml 
---
# handlers file for php
- name: restart php
  service:
    name: php-fpm
    state: restarted

- name: chmod sock
  file:
    path: "{{ socket_file }}"
    owner: "{{ name }}"
    group: "{{ name }}"


### wordpress 依赖mariadb
## 安装mariadb
[Sun Aug 22 06:55:17 root@m01 /ansible_project/roles]
 # cd mariadb/
[Sun Aug 22 06:55:28 root@m01 /ansible_project/roles/mariadb]
 # vim tasks/install.yml
- name: Install Mariadb
  yum:
    name:
      - mariadb-server
      - MySQL-python
    
## 启动mariadb
[Sun Aug 22 06:56:10 root@m01 /ansible_project/roles/mariadb]
 # vim tasks/start.yml
- name: Start Mariadb-server
  service:
    name: mariadb
    state: started
    enabled: yes
    
#include
[Sun Aug 22 08:30:46 root@m01 /ansible_project/roles/mariadb]
 # vim tasks/main.yml 
---
# tasks file for mariadb
- include_tasks: install.yml
- include_tasks: start.yml
    
## 导数据 建库 建用户
 
[Sun Aug 22 07:16:13 root@m01 /ansible_project/roles]
 # mv wordpress wordpress_code
[Sun Aug 22 07:16:43 root@m01 /ansible_project/role
 # ansible-galaxy init wordpress_data
 
[Sun Aug 22 07:23:20 root@m01 /ansible_project/roles/wordpress_data]
 # vim tasks/main.yml
---
# tasks file for wordpress_data
- name: Push SQL File
  copy:
    src: wordpress.sql
    dest: /tmp

- name: Create {{ service_name }} Database
  mysql_db:
    name: "{{ service_name }}"
    state: present
    encoding: utf8

- name: Create {{ service_name }} User
  mysql_user:
    name: "{{ service_name }}"
    priv: "{{ service_name }}.*:ALL"
    host: '%'
    password: '123'

- name: Import {{ service_name }} Data
  mysql_db:
    name: "{{ service_name }}"
    state: import
    target: /tmp/{{ service_name }}.sql



#(修改的)
[Sun Aug 22 07:30:09 root@m01 /ansible_project/roles/wordpress_data]
 # mv ../wordpress_code/files/wordpress.sql .
 [Sun Aug 22 07:31:11 root@m01 /ansible_project/roles/wordpress_data]
 # mv wordpress.sql files/
 (这个就是把相关文件放进files中)
[Mon Aug 23 05:10:02 root@m01 /ansible_project/roles/wordpress_data/files]
 # ll
total 784
-rw-r--r-- 1 root root 798880 Aug 22 05:02 wordpress.sql

(这一部分是最后添加一个挂载和导数据 )
[Sun Aug 22 08:08:35 root@m01 /ansible_project/roles/wordpress_code]
 # cat tasks/main.yml 
---
# tasks file for wordpress
- name: Create /{{ code_dir }} Dir
  file:
    path: /{{ code_dir }}
    state: directory
    owner: "{{ name }}"
    group: "{{ name }}"

- name: Unarchive {{ service_name }}
  unarchive:
    src: wp.tgz
    dest: /{{ code_dir }}
    owner: "{{ name }}"
    group: "{{ name }}"

- name: Push Blog Nginx File
  template:
    src: blog.wk.com.j2
    dest: /etc/nginx/conf.d/{{ blog_server_name }}.conf
  notify: restart nginx

- name: chmod var lib
  file:
    path: /var/lib/nginx
    recurse: yes
    owner: "{{ name }}"
    group: "{{ name }}"

- name: mount
  mount:
    path: /code/wordpress/wp-content/uploads
    src: 172.16.1.31:/data
    fstype: nfs
    state: mounted



## 变量定义
[Sun Aug 22 07:06:47 root@m01 /ansible_project/roles/mariadb]
 # vim vars/main.yml 
---
# vars file for mariadb
service_name: wordpress

#(修改的)
Sun Aug 22 07:26:00 root@m01 /ansible_project/roles]
 # cd wordpress_code/
[Sun Aug 22 07:26:14 root@m01 /ansible_project/roles/wordpress_code]
 # vim meta/main.yml 
dependencies:
  - {role: nginx}
  - {role: php}
  - {role: nfs}
 
[Sun Aug 22 07:50:49 root@m01 /ansible_project/roles]
 # cd wordpress_data/
[Sun Aug 22 07:50:57 root@m01 /ansible_project/roles/wordpress_data]
 # vim meta/main.yml 
dependencies:
  - {role: mariadb}
  - {role: nfs}
  
#(最后又临时修改的)推送数据
[Sun Aug 22 08:17:51 root@m01 /ansible_project/roles]
 # ansible-galaxy init wordpress_nfs
[Sun Aug 22 08:18:51 root@m01 /ansible_project/roles]
 # cd wordpress_nfs/
[Sun Aug 22 08:20:18 root@m01 /ansible_project/roles/wordpress_nfs]
 # vim tasks/main.yml 
---
# tasks file for wordpress_nfs
- name: User Data
  unarchive:
    src: user_data.tgz
    dest: /data
    owner: "{{ name }}"
    group: "{{ name }}"
    
[Sun Aug 22 08:21:37 root@m01 /ansible_project/roles/wordpress_nfs]
 # mv ../wordpress_code/files/user_data.tgz files/
[Mon Aug 23 05:21:30 root@m01 /ansible_project/roles/wordpress_nfs/files]
 # ll
total 244
-rw-r--r-- 1 root root 247661 Aug 21 20:55 user_data.tgz

[Sun Aug 22 08:23:04 root@m01 /ansible_project/roles/wordpress_nfs]
 # vim meta/main.yml 
dependencies:
  - {role: nfs}


  
### wordpress 依赖nfs
#安装nfs
[Sun Aug 22 07:36:18 root@m01 /ansible_project/roles/nfs]
 # vim tasks/install.yml
- name: Install NFS
  yum:
    name: nfs-utils
    
#配置nfs
[Sun Aug 22 07:36:46 root@m01 /ansible_project/roles/nfs]
 # vim tasks/config.yml
- name: Conf NFS
  copy:
    content: /data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
    dest: /etc/exports
  notify: restart nfs

 
#启动nfs
[Sun Aug 22 07:38:46 root@m01 /ansible_project/roles/nfs]
 # vim tasks/start.yml
- name: Start NFS
  service:
    name: nfs-server
    state: started
    enabled: yes
    
#创建共享目录
[Sun Aug 22 07:45:46 root@m01 /ansible_project/roles/nfs]
 # vim tasks/create_dir.yml
- name: Create dir
  file:
    path: /data
    state: directory
    owner: "{{ name }}"
    group: "{{ name }}"

#include
[Sun Aug 22 07:48:00 root@m01 /ansible_project/roles/nfs]
 # vim tasks/main.yml 
---
# tasks file for nfs
- include_tasks: install.yml
- include_tasks: config.yml
- include_tasks: start.yml
- include_tasks: create_dir.yml

#handlers
[Sun Aug 22 07:42:34 root@m01 /ansible_project/roles/nfs]
 # vim handlers/main.yml 
---
# handlers file for nfs
- name: restart nfs
  service:
    name: nfs-server
    state: restarted
    
#依赖
[Sun Aug 22 07:44:24 root@m01 /ansible_project/roles/nfs]
 # vim meta/main.yml 
dependencies:
  - {role: create_user}


#site.tml
[Sun Aug 22 08:41:17 root@m01 /ansible_project/roles]
 # cat site.yml
- hosts: all
  roles:
    - {role: rsync_server,when: ansible_hostname == 'backup'} 
    - {role: wordpress_code,when: ansible_hostname is match 'web*'} 
    - {role: wordpress_data,when: ansible_hostname == 'db01'} 
    - {role: wordpress_nfs,when: ansible_hostname == 'nfs'}
    

标签:name,Aug,Anisble,22,ansible,roles,Roles,root
来源: https://www.cnblogs.com/wkyydsnb/p/15173775.html