• 利用角色roles上线wordpress项目


    角色订制:roles

    ① 简介

    对于以上所有的方式有个弊端就是无法实现复用假设在同时部署Web、db、ha 时或不同服务器组合不同的应用就需要写多个yml文件。很难实现灵活的调用。   roles 用于层次性、结构化地组织playbook。roles 能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量(vars)、文件(file)、任务(tasks)、模块(modules)及处理器(handlers)放置于单独的目录中,并可以便捷地include它们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中。

    ② 角色集合

    角色集合:roles/ mysql/ httpd/ nginx/ files/:存储由copy或script等模块调用的文件; tasks/:此目录中至少应该有一个名为main.yml的文件,用于定义各task;其它的文件需要由main.yml进行“包含”调用; handlers/:此目录中至少应该有一个名为main.yml的文件,用于定义各handler;其它的文件需要由main.yml进行“包含”调用; vars/:此目录中至少应该有一个名为main.yml的文件,用于定义各variable;其它的文件需要由main.yml进行“包含”调用; templates/:存储由template模块调用的模板文本; meta/:此目录中至少应该有一个名为main.yml的文件,定义当前角色的特殊设定及其依赖关系;其它的文件需要由main.yml进行“包含”调用; default/:此目录中至少应该有一个名为main.yml的文件,用于设定默认变量;

    实验:

    192.168.231.210 安装ansible机器 管理机

    192.168.231.214 

    192.168.231.215 被管理的俩台机器

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

    1. [root@localhost ~]# mkdir roles
    2. [root@localhost ~]# cd roles
    3. [root@localhost roles]# ansible-galaxy init nginx
    4. - Role nginx was created successfully
    5. [root@localhost roles]# ansible-galaxy init mysql
    6. - Role mysql was created successfully
    7. [root@localhost roles]# ansible-galaxy init php
    8. - Role php was created successfully
    9. [root@localhost roles]# tree
    10. .
    11. ├── mysql
    12. │   ├── defaults
    13. │   │   └── main.yml
    14. │   ├── files
    15. │   ├── handlers
    16. │   │   └── main.yml
    17. │   ├── meta
    18. │   │   └── main.yml
    19. │   ├── README.md
    20. │   ├── tasks
    21. │   │   └── main.yml
    22. │   ├── templates
    23. │   ├── tests
    24. │   │   ├── inventory
    25. │   │   └── test.yml
    26. │   └── vars
    27. │   └── main.yml
    28. ├── nginx
    29. │   ├── defaults
    30. │   │   └── main.yml
    31. │   ├── files
    32. │   ├── handlers
    33. │   │   └── main.yml
    34. │   ├── meta
    35. │   │   └── main.yml
    36. │   ├── README.md
    37. │   ├── tasks
    38. │   │   └── main.yml
    39. │   ├── templates
    40. │   ├── tests
    41. │   │   ├── inventory
    42. │   │   └── test.yml
    43. │   └── vars
    44. │   └── main.yml
    45. └── php
    46. ├── defaults
    47. │   └── main.yml
    48. ├── files
    49. ├── handlers
    50. │   └── main.yml
    51. ├── meta
    52. │   └── main.yml
    53. ├── README.md
    54. ├── tasks
    55. │   └── main.yml
    56. ├── templates
    57. ├── tests
    58. │   ├── inventory
    59. │   └── test.yml
    60. └── vars
    61. └── main.yml
    62. 27 directories, 24 files

    分别定义配置文件 ,nginx配置文件

    1. [root@localhost roles]# vim nginx/tasks/main.yml
    2. ---
    3. # tasks file for nginx
    4. - name: 下载yum源
    5. shell: curl -o /opt/yum-server.sh http://10.36.192.100/yum-server.sh
    6. - name: 安装yum源
    7. shell: sh /opt/yum-server.sh
    8. - name: 安装nginx
    9. yum: name=nginx state=present
    10. - name: nginx配置文件
    11. template: src=/root/nginx.conf dest=/etc/nginx/
    12. - name: 拷贝php
    13. copy: src=/root/roles/wp-config.php dest=/usr/share/nginx/html/wordpress
    14. - name: 启动Nginx
    15. service: name=nginx state=started enabled=true
    16. - name: 拷贝wordpress源代码
    17. unarchive: src=/root/wordpress-6.4.1-zh_CN.tar.gz dest=/usr/share/nginx/html
    • 创建 /root/roles/wp-config.php文件写入一下内容

    1. [root@localhost roles]# vim wp-config.php
    2. <?php
    3. /**
    4. * The base configuration for WordPress
    5. *
    6. * The wp-config.php creation script uses this file during the installation.
    7. * You don't have to use the web site, you can copy this file to "wp-config.php"
    8. * and fill in the values.
    9. *
    10. * This file contains the following configurations:
    11. *
    12. * * Database settings
    13. * * Secret keys
    14. * * Database table prefix
    15. * * ABSPATH
    16. *
    17. * @link https://wordpress.org/documentation/article/editing-wp-config-php/
    18. *
    19. * @package WordPress
    20. */
    21. // ** Database settings - You can get this info from your web host ** //
    22. /** The name of the database for WordPress */
    23. define( 'DB_NAME', 'wordpress' );
    24. /** Database username */
    25. define( 'DB_USER', 'wordpress' );
    26. /** Database password */
    27. define( 'DB_PASSWORD', 'Qianfeng@123' );
    28. /** Database hostname */
    29. define( 'DB_HOST', '192.168.231.214' );
    30. /** Database charset to use in creating database tables. */
    31. define( 'DB_CHARSET', 'utf8mb4' );
    32. /** The database collate type. Don't change this if in doubt. */
    33. define( 'DB_COLLATE', '' );
    34. /**#@+
    35. * Authentication unique keys and salts.
    36. *
    37. * Change these to different unique phrases! You can generate these using
    38. * the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}.
    39. *
    40. * You can change these at any point in time to invalidate all existing cookies.
    41. * This will force all users to have to log in again.
    42. *
    43. * @since 2.6.0
    44. */
    45. define( 'AUTH_KEY', 'U^UE ~}t)n(9.~=_qMaI:,K`X;Iji<Bsuh[b%yL-VpltA#_zRAX<~kJ>`D;PB:g4' );
    46. define( 'SECURE_AUTH_KEY', 'mZcTrBj{U/-M#+AX:U +&?.&sXlV~/Dlm:rO|=/XL66gI+pl#IcwWUe(<p6HzYs1' );
    47. define( 'LOGGED_IN_KEY', 'D(a}-NENZ{u[& =;|fSIDZt]`z aVKYyt|wt$5[jC#<yvgaM9mvvZCn|W(Wo=8rY' );
    48. define( 'NONCE_KEY', 'MRC+QQ+o6`uE?S^`&#7A1cwBP1I2jC#S?%xoR9dQSSs1+S}n.F{Cf7l*%c:dUSbo' );
    49. define( 'AUTH_SALT', 'Ai6`CEc,KaGUmk-/mLFEU $:b0#!Mjg6_qTB)+*tx=SRsuD6+;z[Ji6gL|@8.TRn' );
    50. define( 'SECURE_AUTH_SALT', '`NL:<P=!HtaQ> q]*}_TB9LA!%Kmh7CjXeM-s-/;>U0o8huC7&KPq{i_wNX%3OI{' );
    51. define( 'LOGGED_IN_SALT', 'q9p;a=$q|m}NI*z!?UKgMzs*xKsF)L.TPhnZ/|U[5bw+RzDv.|$W#vMzu<g5!xYb' );
    52. define( 'NONCE_SALT', 'F+/f&;FTu yZ^(qtdT3 zY%s~FN6lS71iic:X1&q4f^pzi4w3e/$b %,tRt?),$4' );
    53. /**#@-*/
    54. /**
    55. * WordPress database table prefix.
    56. *
    57. * You can have multiple installations in one database if you give each
    58. * a unique prefix. Only numbers, letters, and underscores please!
    59. */
    60. $table_prefix = 'wp_';
    61. /**
    62. * For developers: WordPress debugging mode.
    63. *
    64. * Change this to true to enable the display of notices during development.
    65. * It is strongly recommended that plugin and theme developers use WP_DEBUG
    66. * in their development environments.
    67. *
    68. * For information on other constants that can be used for debugging,
    69. * visit the documentation.
    70. *
    71. * @link https://wordpress.org/documentation/article/debugging-in-wordpress/
    72. */
    73. define( 'WP_DEBUG', false );
    74. /* Add any custom values between this line and the "stop editing" line. */
    75. /* That's all, stop editing! Happy publishing. */
    76. /** Absolute path to the WordPress directory. */
    77. if ( ! defined( 'ABSPATH' ) ) {
    78. define( 'ABSPATH', __DIR__ . '/' );
    79. }
    80. /** Sets up WordPress vars and included files. */
    81. require_once ABSPATH . 'wp-settings.php';

    修改变量文件

    1. [root@localhost roles]# vim nginx/vars/main.yml
    2. ---
    3. # vars file for nginx
    4. nginx_user: root
    5. nginx_port: 80

    放置我们所需要的文件到指定目录

    1. 直接 cp /root/nginx.conf /root/roles/nginx/templates/nginx.conf
    2. 也可以直接写
    3. [root@localhost roles]# vim nginx/templates/nginx.conf
    4. # For more information on configuration, see:
    5. # * Official English Documentation: http://nginx.org/en/docs/
    6. # * Official Russian Documentation: http://nginx.org/ru/docs/
    7. user {{ nginx_user }};
    8. worker_processes auto;
    9. error_log /var/log/nginx/error.log;
    10. pid /run/nginx.pid;
    11. # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
    12. include /usr/share/nginx/modules/*.conf;
    13. events {
    14. worker_connections 1024;
    15. }
    16. http {
    17. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    18. '$status $body_bytes_sent "$http_referer" '
    19. '"$http_user_agent" "$http_x_forwarded_for"';
    20. access_log /var/log/nginx/access.log main;
    21. sendfile on;
    22. tcp_nopush on;
    23. tcp_nodelay on;
    24. keepalive_timeout 65;
    25. types_hash_max_size 4096;
    26. include /etc/nginx/mime.types;
    27. default_type application/octet-stream;
    28. # Load modular configuration files from the /etc/nginx/conf.d directory.
    29. # See http://nginx.org/en/docs/ngx_core_module.html#include
    30. # for more information.
    31. include /etc/nginx/conf.d/*.conf;
    32. server {
    33. listen {{ nginx_port }};
    34. listen [::]:80;
    35. server_name _;
    36. root /usr/share/nginx/html/wordpress;
    37. # Load configuration files for the default server block.
    38. include /etc/nginx/default.d/*.conf;
    39. location / {
    40. root /usr/share/nginx/html/wordpress;
    41. index index.php;
    42. }
    43. location ~ \.php$ {
    44. root /usr/share/nginx/html/wordpress; #指定网站目录
    45. fastcgi_pass 127.0.0.1:9000; #指定访问地址
    46. fastcgi_index index.php; #指定默认文件
    47. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    48. include fastcgi_params; #包含nginx常量定义
    49. }
    50. error_page 404 /404.html;
    51. location = /404.html {
    52. }
    53. error_page 500 502 503 504 /50x.html;
    54. location = /50x.html {
    55. }
    56. }
    57. # Settings for a TLS enabled server.
    58. #
    59. # server {
    60. # listen 443 ssl http2;
    61. # listen [::]:443 ssl http2;
    62. # server_name _;
    63. # root /usr/share/nginx/html;
    64. #
    65. # ssl_certificate "/etc/pki/nginx/server.crt";
    66. # ssl_certificate_key "/etc/pki/nginx/private/server.key";
    67. # ssl_session_cache shared:SSL:1m;
    68. # ssl_session_timeout 10m;
    69. # ssl_ciphers HIGH:!aNULL:!MD5;
    70. # ssl_prefer_server_ciphers on;
    71. #
    72. # # Load configuration files for the default server block.
    73. # include /etc/nginx/default.d/*.conf;
    74. #
    75. # error_page 404 /404.html;
    76. # location = /40x.html {
    77. # }
    78. #
    79. # error_page 500 502 503 504 /50x.html;
    80. # location = /50x.html {
    81. # }
    82. # }
    83. }

    php配置文件

    1. [root@localhost roles]# vim php/tasks/main.yml
    2. ---
    3. # tasks file for php
    4. - name: 安装PHP
    5. 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
    6. - name: 启动PHP
    7. service: name=php80-php-fpm state=started enabled=true

    mysql配置文件

    1. [root@localhost roles]# cat mysql/tasks/main.yml
    2. ---
    3. # tasks file for mysql
    4. - name: 安装数据库
    5. yum: name=mariadb-server,mariadb state=present disablerepo=mysql-5.7-community
    6. - name: 启动数据库
    7. service: name=mariadb state=started enabled=true
    8. - name: 配置数据库
    9. shell: mysql -e "create database {{ db_name }}; grant all on wordpress.* to 'wordpress'@'%' identified by '{{ db_passwd }}'; flush privileges"

    mysql变量文件

    1. [root@localhost roles]# vim mysql/vars/main.yml
    2. ---
    3. # vars file for mysql
    4. db_passwd: 'Qianfeng@123'
    5. db_name: 'wordpress'

    定义剧本文件

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

    1. [root@localhost roles]# vim roles.yml
    2. ---
    3. - hosts: web
    4. remote_user: root
    5. roles:
    6. - nginx
    7. - php
    8. - mysql

    启动服务

    1. [root@localhost roles]# ansible-playbook roles.yml
    2. PLAY [web] ********************************************************************************
    3. TASK [Gathering Facts] ********************************************************************
    4. ok: [web1]
    5. ok: [web2]
    6. TASK [nginx : 下载yum源] *********************************************************************
    7. [WARNING]: Consider using the get_url or uri module rather than running 'curl'. If you
    8. need to use command because get_url or uri is insufficient you can add 'warn: false' to
    9. this command task or set 'command_warnings=False' in ansible.cfg to get rid of this
    10. message.
    11. changed: [web1]
    12. changed: [web2]
    13. TASK [nginx : 安装yum源] *********************************************************************
    14. changed: [web1]
    15. changed: [web2]
    16. TASK [安装nginx] ****************************************************************************
    17. changed: [web1]
    18. changed: [web2]
    19. TASK [nginx配置文件] **************************************************************************
    20. changed: [web1]
    21. changed: [web2]
    22. TASK [nginx : 启动Nginx] ********************************************************************
    23. changed: [web2]
    24. changed: [web1]
    25. TASK [nginx : 拷贝wordpress源代码] *************************************************************
    26. changed: [web2]
    27. changed: [web1]
    28. TASK [php : 安装PHP] ************************************************************************
    29. changed: [web1]
    30. changed: [web2]
    31. TASK [php : 启动PHP] ************************************************************************
    32. changed: [web1]
    33. changed: [web2]
    34. TASK [mysql : 安装数据库] **********************************************************************
    35. changed: [web1]
    36. changed: [web2]
    37. TASK [mysql : 启动数据库] **********************************************************************
    38. changed: [web1]
    39. changed: [web2]
    40. TASK [mysql : 配置数据库] **********************************************************************
    41. changed: [web1]
    42. changed: [web2]
    43. PLAY RECAP ********************************************************************************
    44. web1 : ok=12 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    45. web2 : ok=12 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    查看所有服务的端口

    1. [root@localhost roles]# ansible web -m shell -a 'ss -nplt'
    2. web1 | CHANGED | rc=0 >>
    3. State Recv-Q Send-Q Local Address:Port Peer Address:Port
    4. LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=2504,fd=9),("php-fpm",pid=2503,fd=9),("php-fpm",pid=2502,fd=9),("php-fpm",pid=2501,fd=9),("php-fpm",pid=2500,fd=9),("php-fpm",pid=2499,fd=7))
    5. LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=2986,fd=14))
    6. LISTEN 0 128 *:80 *:* users:(("nginx",pid=2057,fd=6),("nginx",pid=2056,fd=6),("nginx",pid=2055,fd=6))
    7. LISTEN 0 128 *:22 *:* users:(("sshd",pid=922,fd=3))
    8. LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1115,fd=13))
    9. LISTEN 0 128 [::]:80 [::]:* users:(("nginx",pid=2057,fd=7),("nginx",pid=2056,fd=7),("nginx",pid=2055,fd=7))
    10. LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=922,fd=4))
    11. LISTEN 0 100 [::1]:25 [::]:* users:(("master",pid=1115,fd=14))
    12. web2 | CHANGED | rc=0 >>
    13. State Recv-Q Send-Q Local Address:Port Peer Address:Port
    14. LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=2499,fd=9),("php-fpm",pid=2498,fd=9),("php-fpm",pid=2497,fd=9),("php-fpm",pid=2496,fd=9),("php-fpm",pid=2495,fd=9),("php-fpm",pid=2494,fd=7))
    15. LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=2983,fd=14))
    16. LISTEN 0 128 *:80 *:* users:(("nginx",pid=2052,fd=6),("nginx",pid=2051,fd=6),("nginx",pid=2050,fd=6))
    17. LISTEN 0 128 *:22 *:* users:(("sshd",pid=920,fd=3))
    18. LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1115,fd=13))
    19. LISTEN 0 128 [::]:80 [::]:* users:(("nginx",pid=2052,fd=7),("nginx",pid=2051,fd=7),("nginx",pid=2050,fd=7))
    20. LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=920,fd=4))
    21. LISTEN 0 100 [::1]:25 [::]:* users:(("master",pid=1115,fd=14))

    进去浏览器查看

  • 相关阅读:
    【校招VIP】java语言考点之双亲委派模型
    5种API网关选型,yyds!
    Linux环境变量与程序地址空间
    结合实战,浅析GB/T28181(四)——录像回放及控制
    java计算机毕业设计可视化工器具信息管理系统MyBatis+系统+LW文档+源码+调试部署
    常见6种易被忽略的软件隐藏缺陷
    自然语言处理中的文本聚类:揭示模式和见解
    Gin框架入门实战系列教程之Gin环境搭建 Gin程序的热加载 Gin路由 GET POST PUT DELETE
    LVM使用与管理
    机器学习深入浅出
  • 原文地址:https://blog.csdn.net/m0_59933574/article/details/134386121