• Ansible角色定制实例


    目录

    角色定制:roles

    角色定制实例:利用角色部署wordpress

    1.在roles目录下生成对应的目录结构

    2.定义配置文件

    ①nginx

    ②php

     ③mysql

    ④定义剧本文件

    ⑤启动服务


    角色定制:roles

          对于普通的剧本(playbook)有个弊端就是无法实现复用假设在同时部署Web、db、ha 时或不同服务器组合不同的应用就需要写多个yml文件。很难实现灵活的调用。

      roles 用于层次性、结构化地组织playbook。roles 能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量(vars)、文件(file)、任务(tasks)、模块(modules)及处理器(handlers)放置于单独的目录中,并可以便捷地include它们的一种机制。

    角色定制实例:利用角色部署wordpress

    192.168.134.165 server01

    192.168.134.166 server02

    192.168.134.163 server03

    server01管理其他两个服务器资产。

    1.在roles目录下生成对应的目录结构
    1. [root@server01 ~]# mkdir roles
    2. #生成相应的目录
    3. [root@server01 roles]# ansible-galaxy init nginx
    4. [root@server01 roles]# ansible-galaxy init mysql
    5. [root@server01 roles]# ansible-galaxy init php
    6. [root@server01 roles]# tree
    7. .
    8. ├── mysql
    9. │   ├── defaults
    10. │   │   └── main.yml
    11. │   ├── files
    12. │   ├── handlers
    13. │   │   └── main.yml
    14. │   ├── meta
    15. │   │   └── main.yml
    16. │   ├── README.md
    17. │   ├── tasks
    18. │   │   └── main.yml
    19. │   ├── templates
    20. │   ├── tests
    21. │   │   ├── inventory
    22. │   │   └── test.yml
    23. │   └── vars
    24. │   └── main.yml
    25. ├── nginx
    26. │   ├── defaults
    27. │   │   └── main.yml
    28. │   ├── files
    29. │   ├── handlers
    30. │   │   └── main.yml
    31. │   ├── meta
    32. │   │   └── main.yml
    33. │   ├── README.md
    34. │   ├── tasks
    35. │   │   └── main.yml
    36. │   ├── templates
    37. │   │   └── nginx.conf
    38. │   ├── tests
    39. │   │   ├── inventory
    40. │   │   └── test.yml
    41. │   └── vars
    42. │   └── main.yml
    43. ├── php
    44. │   ├── defaults
    45. │   │   └── main.yml
    46. │   ├── files
    47. │   ├── handlers
    48. │   │   └── main.yml
    49. │   ├── meta
    50. │   │   └── main.yml
    51. │   ├── README.md
    52. │   ├── tasks
    53. │   │   └── main.yml
    54. │   ├── templates
    55. │   ├── tests
    56. │   │   ├── inventory
    57. │   │   └── test.yml
    58. │   └── vars
    59. │   └── main.yml
    2.定义配置文件

    我们需要修改每个文件对应的配置。

    ①nginx
    • [root@server01 roles]# vim nginx/tasks/main.yml
    1. [root@server01 roles]# vim nginx/tasks/main.yml
    2. ---
    3. # tasks file for nginx
    4. - name: install {{ server }}
    5. yum: name={{ server }},epel-release state=present
    6. - name: start {{ server }}
    7. service: name={{ server }} state=started
    8. - name: 拷贝wordpress源代码
    9. unarchive: src=/root/wordpress-6.4.1-zh_CN.tar.gz dest=/usr/share/nginx/html
    10. - name: copy wordpress
    11. copy: src=/root/roles/wp-config.php dest=/usr/share/nginx/html/wordpress/
    12. - name: copy
    13. template: src=/root/roles/nginx/templates/nginx.conf dest=/etc/nginx/ #将nginx的配置文件模板发送到资产上
    14. notify: restartnginx
    15. tags: rs
    16. #上传wordpress的包
    17. [root@server01 ~]# rz
    18. [root@server01 ~]# ll
    19. -rw-r--r-- 1 root root 25302043 11月 13 09:58 wordpress-6.4.1-zh_CN.tar.gz
    • 创建 /root/roles/wp-config.php文件写入一下内容
    1. /**
    2. * The base configuration for WordPress
    3. *
    4. * The wp-config.php creation script uses this file during the installation.
    5. * You don't have to use the web site, you can copy this file to "wp-config.php"
    6. * and fill in the values.
    7. *
    8. * This file contains the following configurations:
    9. *
    10. * * Database settings
    11. * * Secret keys
    12. * * Database table prefix
    13. * * ABSPATH
    14. *
    15. * @link https://wordpress.org/documentation/article/editing-wp-config-php/
    16. *
    17. * @package WordPress
    18. */
    19. // ** Database settings - You can get this info from your web host ** //
    20. /** The name of the database for WordPress */
    21. define( 'DB_NAME', 'wordpress' );
    22. /** Database username */
    23. define( 'DB_USER', 'wordpress' );
    24. /** Database password */
    25. define( 'DB_PASSWORD', 'Aren@123' );
    26. /** Database hostname */
    27. define( 'DB_HOST', '192.168.134.166' );
    28. /** Database charset to use in creating database tables. */
    29. define( 'DB_CHARSET', 'utf8mb4' );
    30. /** The database collate type. Don't change this if in doubt. */
    31. define( 'DB_COLLATE', '' );
    32. /**#@+
    33. * Authentication unique keys and salts.
    34. *
    35. * Change these to different unique phrases! You can generate these using
    36. * the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}.
    37. *
    38. * You can change these at any point in time to invalidate all existing cookies.
    39. * This will force all users to have to log in again.
    40. *
    41. * @since 2.6.0
    42. */
    43. define( 'AUTH_KEY', 'ug.ct&MB!sL1_o/6t.}Xt4|f?;%|@.Zw6>wUwM3uSXcgB_nnOaZF-m^oUAuV.i-n' );
    44. define( 'SECURE_AUTH_KEY', 'EP#~FNQDJRfL4C8Lboja}KVrc:fhdq_L~gn#wN>^qrn.DHZQwkjl=]-:QFcv:aY-' );
    45. define( 'LOGGED_IN_KEY', '3NQgD!)H{lR=,s(EQm7!jYrlR*T|3:h:Ag>_|#6qAO k6U#vvF)gXqb EKdcu[]v' );
    46. define( 'NONCE_KEY', ',LMJd60b=Qj1]B)ut!JAYCuJ)xN?`Dlnc>P8Tl_hCTk3Sl+qyAvpi_[VbCTAR?:/' );
    47. define( 'AUTH_SALT', 'uj<)Ftb_ZA8+;ms%1AqGik.P@35[]r?.d~jc4J?]und3^vEV*=noZ}z^Gbj?u,oQ' );
    48. define( 'SECURE_AUTH_SALT', '-CoV$a0F^9AF ?Zk()y<{}*WB`QP;$++T`F2NC6OUb]2=i9GW`*/1RjLb&sTO>}/' );
    49. define( 'LOGGED_IN_SALT', '2)?iJr4fi!gqk5~76^f1}Apdwynt:;$JoNdw]ty:kL]tEfy[%$H(oLNdCX/bFhJy' );
    50. define( 'NONCE_SALT', 'zU(c-T%ayYW17wFle,oVj0@VG&m,e#Ujs~M|@>q[^|RCp*q)GbGTbRh*zh_#V5h6' );
    51. /**#@-*/
    52. /**
    53. * WordPress database table prefix.
    54. *
    55. * You can have multiple installations in one database if you give each
    56. * a unique prefix. Only numbers, letters, and underscores please!
    57. */
    58. $table_prefix = 'wp_';
    59. define( 'WP_DEBUG', false );
    60. /* Add any custom values between this line and the "stop editing" line. */
    61. /* That's all, stop editing! Happy publishing. */
    62. /** Absolute path to the WordPress directory. */
    63. if ( ! defined( 'ABSPATH' ) ) {
    64. define( 'ABSPATH', __DIR__ . '/' );
    65. }
    66. /** Sets up WordPress vars and included files. */
    67. require_once ABSPATH . 'wp-settings.php';

    • [root@server01 roles]# vim nginx/vars/main.yml
    1. [root@server01 roles]# vim nginx/vars/main.yml
    2. ---
    3. # vars file for nginx
    4. server: nginx
    5. port: 80
    • [root@server01 roles]# vim nginx/templates/nginx.conf
    1. user nginx;
    2. worker_processes auto;
    3. error_log /var/log/nginx/error.log;
    4. pid /run/nginx.pid;
    5. # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
    6. include /usr/share/nginx/modules/*.conf;
    7. events {
    8. worker_connections 1024;
    9. }
    10. http {
    11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    12. '$status $body_bytes_sent "$http_referer" '
    13. '"$http_user_agent" "$http_x_forwarded_for"';
    14. access_log /var/log/nginx/access.log main;
    15. sendfile on;
    16. tcp_nopush on;
    17. tcp_nodelay on;
    18. keepalive_timeout 65;
    19. types_hash_max_size 4096;
    20. include /etc/nginx/mime.types;
    21. default_type application/octet-stream;
    22. # Load modular configuration files from the /etc/nginx/conf.d directory.
    23. # See http://nginx.org/en/docs/ngx_core_module.html#include
    24. # for more information.
    25. include /etc/nginx/conf.d/*.conf;
    26. server {
    27. listen {{ port }};
    28. server_name localhost;
    29. root /usr/share/nginx/html/wordpress;
    30. index index.php;
    31. # Load configuration files for the default server block.
    32. include /etc/nginx/default.d/*.conf;
    33. location ~ \.php$ {
    34. root /usr/share/nginx/html/wordpress; #指定网站目录
    35. fastcgi_pass 127.0.0.1:9000; #指定访问地址
    36. fastcgi_index index.php; #指定默认文件
    37. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #站点根目录,取决于root配置项
    38. include fastcgi_params; #包含nginx常量定义
    39. }
    40. error_page 404 /404.html;
    41. location = /404.html {
    42. }
    43. error_page 500 502 503 504 /50x.html;
    44. location = /50x.html {
    45. }
    46. }
    47. }
    • 定义handlers文件 [root@server01 roles]# vim nginx/handlers/main.yml 
    1. ---
    2. # handlers file for nginx
    3. - name: restartnginx
    4. service: name={{ server }} state=restarted
    ②php
    • root@server01 roles]# vim php/tasks/main.yml 
    1. ---
    2. # tasks file for php
    3. - name: install php
    4. yum: name=php80-php-xsl,php80-php,php80-php-cli,php80-php-devel,php80-php-gd,php80-php-pdo,php80-php-mysql,php80-php-fpm state=present
    5. - name: start php
    6. service: name=php80-php-fpm state=started
     ③mysql
    • [root@server01 roles]# vim mysql/tasks/main.yml 
    1. ---
    2. # tasks file for mysql
    3. - name: install mariadb
    4. yum: name=mariadb-server,mariadb state=present disablerepo=mysql-5.7-community
    5. - name: start db
    6. service: name=mariadb state=started enabled=true
    7. - name: change passwd
    8. shell: mysql -e "create database {{ db_name }}; grant all on wordpress.* to 'wordpress'@'%' identified by '{{ db_pass }}'; flush privileges"
    9. tags: db
    • [root@server01 roles]# vim mysql/vars/main.yml
    1. ---
    2. # vars file for mysql
    3. db_name: 'wordpress'
    4. db_pass: Aren@123
    ④定义剧本文件

    接下来,我们就来定义剧本文件,由于大部分设置我们都单独配置在了roles里面,所以,接下来剧本就只需要写一点点内容即可:

    1. [root@server01 roles]# vim roles.yaml
    2. ---
    3. - hosts: all
    4. remote_user: root
    5. roles:
    6. - nginx
    7. - php
    8. - mysql
    ⑤启动服务
    • [root@server01 roles]# ansible-playbook /root/roles/roles.yaml

     进入web页面

  • 相关阅读:
    activiti默认模型ER图
    Python中两种网络编程方式:Socket和HTTP协议
    【面试题】JS使用parseInt()、正则截取字符串中数字
    9.0 堆体系结构概述之GC
    C++编译错误
    【精讲】vue计算属性的基本使用、计算属性中的getted及setter、计算属性与methods对比、侦听事件watch
    基于PHP的人才招聘网站设计
    尚医通gateway配置bug
    nodejs+vue+elementui高校新闻管理系统
    Springboot中解析JSON字符串(jackson库ObjectMapper解析JSON字符串)
  • 原文地址:https://blog.csdn.net/l1727377/article/details/134384446