翼度科技»论坛 云主机 LINUX 查看内容

Ansible介绍以及基于角色搭建LNMP和zabbix

11

主题

11

帖子

33

积分

新手上路

Rank: 1

积分
33
1 ansible 常用指令总结,并附有相关示例。


  • /usr/bin/ansible 主程序,临时命令执行工具
  • /usr/bin/ansible-doc 查看配置文档,模块功能查看工具,相当于man
  • /usr/bin/ansible-playbook 定制自动化任务,编排剧本工具,相当于脚本
  • /usr/bin/ansible-pull 远程执行命令的工具
  • /usr/bin/ansible-vault 文件加密工具
  • /usr/bin/ansible-console 基于Console界面与用户交互的执行工具
  • /usr/bin/ansible-galaxy 下载/上传优秀代码或Roles模块的官网平台
利用ansible实现管理的主要方式:

  • Ansible Ad-Hoc 即利用ansible命令,主要用于临时命令使用场景
  • Ansible playbook 主要用于长期规划好的,大型项目的场景,需要有前期的规划过程
ansible 使用前准备
ansible 相关工具大多数是通过ssh协议,实现对远程主机的配置管理、应用部署、任务执行等功能
建议:使用此工具前,先配置ansible主控端能基于密钥认证的方式联系各个被管理节点
1.1 ansible-doc

此工具用来显示模块帮助,相当于man
格式
  1. ansible-doc [options] [module...]
  2. -l, --list #列出可用模块
  3. -s, --snippet #显示指定模块的playbook片段
复制代码
范例:
  1. #列出所有模块
  2. ansible-doc -l
  3. #查看指定模块帮助用法
  4. ansible-doc ping
  5. #查看指定模块帮助用法
  6. ansible-doc -s ping
复制代码
范例: 查看指定的插件
  1. [13:38:40 root@ansible-rocky ~]$ ansible-doc -t connection -l
  2. [13:39:08 root@ansible-rocky ~]$ ansible-doc -t lookup -l
复制代码
1.2 ansible

1.2.1 Ansible Ad-Hoc 介绍

Ansible Ad-Hoc 的执行方式的主要工具就是 ansible
特点: 一次性的执行,不会保存执行命令信息,只适合临时性或测试性的任务
1.2.2 ansible 命令用法

格式:
  1. ansible <host-pattern> [-m module_name] [-a args]
复制代码
选项说明:
  1. --version                               #显示版本
  2. -m module                               #指定模块,默认为command
  3. -v                                      #详细过程 -vv -vvv更详细
  4. --list-hosts                            #显示主机列表,可简写 --list
  5. -C, --check                             #检查,并不执行
  6. -T, --timeout=TIMEOUT                   #执行命令的超时时间,默认10s
  7. -k, --ask-pass                          #提示输入ssh连接密码,默认Key验证
  8. -u, --user=REMOTE_USER                  #执行远程执行的用户,默认root
  9. -b, --become                            #代替旧版的sudo实现通过sudo机制实现提升权限
  10. --become-user=USERNAME                  #指定sudo的run as用户,默认为root
  11. -K, --ask-become-pass                   #提示输入sudo时的口令
  12. -f FORKS, --forks FORKS                 #指定并发同时执行ansible任务的主机数
  13. -i INVENTORY, --inventory INVENTORY     #指定主机清单文件
复制代码
范例:
  1. #先打通基于key验证
  2. #以yanlinux用户执行ping存活检测
  3. [14:28:30 yanlinux@ansible-rocky ~]$ ansible all -m ping -u yanlinux
  4. 10.0.0.18 | SUCCESS => {
  5.     "ansible_facts": {
  6.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  7.     },
  8.     "changed": false,
  9.     "ping": "pong"
  10. }
  11. 10.0.0.102 | SUCCESS => {
  12.     "ansible_facts": {
  13.         "discovered_interpreter_python": "/usr/bin/python3"
  14.     },
  15.     "changed": false,
  16.     "ping": "pong"
  17. }
  18. 10.0.0.7 | SUCCESS => {
  19.     "ansible_facts": {
  20.         "discovered_interpreter_python": "/usr/bin/python"
  21.     },
  22.     "changed": false,
  23.     "ping": "pong"
  24. }
  25. #以yanlinux sudo至root执行命令
  26. ##没有添加sudo授权之前
  27. [14:36:46 yanlinux@ansible-rocky ~]$ ansible all -a 'ls /root'
  28. 10.0.0.18 | FAILED | rc=2 >>
  29. ls: cannot open directory '/root': Permission deniednon-zero return code
  30. 10.0.0.102 | FAILED | rc=2 >>
  31. ls: cannot open directory '/root': Permission deniednon-zero return code
  32. 10.0.0.7 | FAILED | rc=2 >>
  33. ls: cannot open directory /root: Permission deniednon-zero return code
  34. ##在所有被控制主机上都加上suod授权
  35. [14:30:46 root@ansible-rocky ~]$ echo "yanlinux    ALL=(ALL)   NOPASSWD: ALL" >> /etc/sudoers
  36. [14:37:01 yanlinux@ansible-rocky ~]$ ansible all -a 'ls /root' -b
  37. 10.0.0.102 | CHANGED | rc=0 >>
  38. init_os.sh
  39. snap
  40. 10.0.0.7 | CHANGED | rc=0 >>
  41. anaconda-ks.cfg
  42. init_os.sh
  43. 10.0.0.18 | CHANGED | rc=0 >>
  44. anaconda-ks.cfg
  45. init_os.sh
  46. ##所有被管理主机上创建用户magedu
  47. [14:37:05 yanlinux@ansible-rocky ~]$ ansible all -a 'useradd magedu' -b
  48. 10.0.0.102 | CHANGED | rc=0 >>
  49. 10.0.0.18 | CHANGED | rc=0 >>
  50. 10.0.0.7 | CHANGED | rc=0 >>
  51. [14:39:46 yanlinux@ansible-rocky ~]$ ansible all -a 'getent passwd magedu' -b
  52. 10.0.0.7 | CHANGED | rc=0 >>
  53. magedu:x:1002:1002::/home/magedu:/bin/bash
  54. 10.0.0.102 | CHANGED | rc=0 >>
  55. magedu:x:1001:1001::/home/magedu:/bin/sh
  56. 10.0.0.18 | CHANGED | rc=0 >>
  57. magedu:x:1001:1001::/home/magedu:/bin/bash
复制代码
范例: 并发执行控制
  1. #并发是1一个主机一个主机的执行,一条条返回结果
  2. [14:42:47 root@ansible-rocky ~]$ ansible all -a 'sleep 5' -f1
  3. #并发是10,同时10个主机执行命令,返回结果
  4. [14:42:47 root@ansible-rocky ~]$ ansible all -a 'sleep 5' -f10
复制代码
范例: 使用普通用户连接远程主机执行代替另一个用户身份执行操作
  1. #在被管理主机上创建用户并sudo授权
  2. [14:34:00 root@ubuntu2004 ~]$ useradd magedu
  3. [14:34:29 root@ubuntu2004 ~]$ echo magedu:centos1 |chpasswd
  4. #以yanlinux的用户连接用户并利用sudo代表magedu执行whoami命令
  5. [14:58:37 yanlinux@ansible-rocky ~]$ ansible all -a 'whoami' -b --become-user=magedu
  6. 10.0.0.18 | CHANGED | rc=0 >>
  7. magedu
  8. 10.0.0.7 | CHANGED | rc=0 >>
  9. magedu
复制代码
1.3 ansible-console

此工具可交互执行命令,支持tab,ansible 2.0+新增
提示符格式:
  1. 执行用户@当前操作的主机组 (当前组的主机数量)[f:并发数]$
复制代码
常用子命令:

  • 设置并发数: forks n 例如: forks 10
  • 切换组: cd 主机组 例如: cd web
  • 列出当前组主机列表: list
  • 列出所有的内置命令: ?或help
范例
  1. [15:24:28 root@ansible-rocky ~]$ ansible-console
  2. Welcome to the ansible console. Type help or ? to list commands.
  3. root@all (3)[f:5]$ ping
  4. 10.0.0.18 | SUCCESS => {
  5.     "ansible_facts": {
  6.         "discovered_interpreter_python": "/usr/libexec/platform-python"
  7.     },
  8.     "changed": false,
  9.     "ping": "pong"
  10. }
  11. 10.0.0.102 | SUCCESS => {
  12.     "ansible_facts": {
  13.         "discovered_interpreter_python": "/usr/bin/python3"
  14.     },
  15.     "changed": false,
  16.     "ping": "pong"
  17. }
  18. 10.0.0.7 | SUCCESS => {
  19.     "ansible_facts": {
  20.         "discovered_interpreter_python": "/usr/bin/python"
  21.     },
  22.     "changed": false,
  23.     "ping": "pong"
  24. }
  25. root@all (3)[f:5]$ list
  26. 10.0.0.18
  27. 10.0.0.7
  28. 10.0.0.102
  29. root@all (3)[f:5]$ cd websrvs
  30. root@websrvs (2)[f:5]$ list
  31. 10.0.0.18
  32. 10.0.0.7
  33. root@websrvs (2)[f:5]$ forks 10
  34. root@websrvs (2)[f:10]$ cd appsrvs
  35. root@appsrvs (2)[f:10]$ list
  36. 10.0.0.102
  37. 10.0.0.18
复制代码
1.4 ansible-playbook

此工具用于执行编写好的 playbook 任务
范例:
  1. [15:27:57 root@ansible-rocky ~]$ vi hello.yml
  2. ---
  3. #hello world yml file
  4. - hosts: websrvs
  5.   remote_user: root
  6.   gather_facts: no
  7.   tasks:
  8.     - name: hello world
  9.       command: /usr/bin/wall hello world
  10. [15:30:12 root@ansible-rocky ~]$ ansible-playbook hello.yml
  11. PLAY [websrvs] ****************************************************************************************
  12. TASK [hello world] ************************************************************************************
  13. changed: [10.0.0.18]
  14. changed: [10.0.0.7]
  15. PLAY RECAP ********************************************************************************************
  16. 10.0.0.18                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  17. 10.0.0.7                   : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
复制代码


1.5 ansible-vault

此工具可以用于加密解密yml文件
格式:
  1. ansible-vault [create|decrypt|edit|encrypt|rekey|view]
复制代码
范例:
  1. #1 加密
  2. [15:31:01 root@ansible-rocky ~]$ ansible-vault encrypt hello.yml
  3. New Vault password:
  4. Confirm New Vault password:
  5. Encryption successful
  6. ##查看文件内容
  7. [15:38:15 root@ansible-rocky ~]$ cat hello.yml
  8. $ANSIBLE_VAULT;1.1;AES256
  9. 65323766623831636563636132623333623932633461396563383764333037396563633766363231
  10. 3335646336346136626231353133623566626166626336380a306630643338353031353739353538
  11. 62373930306636633430653537363534376231323839643131376335653366656634616365663063
  12. 6236663364343461610a383365643534646564316261326166316233393039386134363436313138
  13. 63323939663537666462646233613262646637306130626336336239323737623833393735666364
  14. 36336334316666326265356166326163373039616533353564353964396266376637363037353338
  15. 37623639656262303966363766356630376466666463363338353535623635633137616335383333
  16. 65333263643762353264326563326362393663316538666530616664643438666435373162616164
  17. 30313761323030343165666330326537653430333764363834326566333666316133386465663334
  18. 63353035616266396366366662643839353431653736353465626261623433343735663534663831
  19. 32636632653730323465366531353531633761623930303138643337613162613062333237633566
  20. 39663562393535343165
  21. #2 解密
  22. [15:38:18 root@ansible-rocky ~]$ ansible-vault decrypt hello.yml
  23. Vault password:
  24. Decryption successful
  25. [15:39:50 root@ansible-rocky ~]$ cat hello.yml
  26. ---
  27. #hello world yml file
  28. - hosts: websrvs
  29.   remote_user: root
  30.   gather_facts: no
  31.   tasks:
  32.     - name: hello world
  33.       command: /usr/bin/wall hello world
  34.       
  35. #3 查看加密后的yml文件内容
  36. [15:41:44 root@ansible-rocky ~]$ ansible-vault view hello.yml
  37. Vault password:
  38. ---
  39. #hello world yml file
  40. - hosts: websrvs
  41.   remote_user: root
  42.   gather_facts: no
  43.   tasks:
  44.     - name: hello world
  45.       command: /usr/bin/wall hello world
  46. #4 编辑加密文件
  47. [15:41:50 root@ansible-rocky ~]$ ansible-vault edit hello.yml
  48. Vault password:             #输入密码后进入vim编辑器进行编辑
  49. #5 修改口令
  50. [15:44:53 root@ansible-rocky ~]$ ansible-vault rekey hello.yml
  51. Vault password:    #先前的口令
  52. New Vault password:  #修改为的口令
  53. Confirm New Vault password:  #再确认一遍
  54. Rekey successful
  55. #6 创建加密新文件
  56. [15:46:31 root@ansible-rocky ~]$ ansible-vault create new.yml
  57. New Vault password:
  58. Confirm New Vault password:
  59. #7 交互式输入密码来执行加密文件
  60. [15:46:46 root@ansible-rocky ~]$ ansible-playbook --ask-vault-pass hello.yml
  61. Vault password:
  62. PLAY [websrvs] ****************************************************************************************
  63. TASK [hello world] ************************************************************************************
  64. changed: [10.0.0.18]
  65. changed: [10.0.0.7]
  66. PLAY RECAP ********************************************************************************************
  67. 10.0.0.18                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  68. 10.0.0.7                   : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  69. #8 从文件中读取密码
  70. [15:52:56 root@ansible-rocky ~]$ ansible-playbook --vault-password-file pass.txt hello.yml
  71. PLAY [websrvs] ****************************************************************************************
  72. TASK [hello world] ************************************************************************************
  73. changed: [10.0.0.18]
  74. changed: [10.0.0.7]
  75. PLAY RECAP ********************************************************************************************
  76. 10.0.0.18                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  77. 10.0.0.7                   : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  78. #9 从配置文件中添加密码文件
  79. [15:53:33 root@ansible-rocky ~]$ vi /etc/ansible/ansible.cfg
  80. #添加以下一行信息
  81. ault-password-file=pass.txt
  82. [15:58:58 root@ansible-rocky ~]$ ansible-playbook hello.yml
  83. PLAY [websrvs] ****************************************************************************************
  84. TASK [hello world] ************************************************************************************
  85. changed: [10.0.0.18]
  86. changed: [10.0.0.7]
  87. PLAY RECAP ********************************************************************************************
  88. 10.0.0.18                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  89. 10.0.0.7                   : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
复制代码
1.6 ansible-galaxy

Galaxy 是一个免费网站, 类似于github网站, 网站上发布了很多的共享的roles角色。
Ansible 提供了ansible-galaxy命令行工具连接 https://galaxy.ansible.com 网站下载相应的roles, 进行init(初始化、search( 查拘、install(安装、 remove(移除)等操作。
范例
  1. #搜索项目
  2. [16:05:04 root@ansible-rocky ~]$ ansible-galaxy search lamp
  3. Found 100 roles matching your search:
  4. Name                                      Description
  5. ----                                      -----------
  6. abhiarun_94.apache_lamp                   your role description
  7. adelaidearnauld.galaxy-lamp               your description
  8. adelaidearnauld.lamp_compose              your description
  9. ajish_antony.ansible_lamp                 your role description
  10. AlexanderAllen.Liara                      The sexiest toolkit for LAMP hackers.
  11. alphinaugustine.ansible_role              your description
  12. amtega.horde                              Setup horde
  13. ......
  14. #2 列出所有已安装的galaxy
  15. [16:06:32 root@ansible-rocky ~]$ ansible-galaxy list
  16. # /usr/share/ansible/roles
  17. # /etc/ansible/roles
  18. #3 安装galaxy,默认下载到~/.ansible/roles
  19. [16:14:06 root@ansible-rocky ~]$ ansible-galaxy install 想要安装的galaxy
  20. #删除
  21. ansible-galaxy remove
复制代码
2 总结ansible role目录结构及文件用途。

roles目录结构:
  1. ├── playbook1.yml
  2. ├── playbook2.yml
  3. ├── roles/
  4. │   ├── project1/
  5. │   │        ├── tasks/
  6. │   │        ├── files/
  7. │   │        ├── vars/
  8. │   │        ├── templates/
  9. │   │        ├── handlers/
  10. │   │        ├── default/
  11. │   │        └── meta/
  12. │   ├── project2/
  13. │   │        ├── tasks/
  14. │   │        ├── files/
  15. │   │        ├── vars/
  16. │   │        ├── templates/
  17. │   │        ├── handlers/
  18. │   │        ├── default/
  19. │   │        └── meta/
复制代码
Roles各目录作用
roles/project/ :项目名称,有以下子目录

  • files/ :存放由copy或script模块等调用的文件
  • templates/:template模块查找所需要模板文件的目录
  • tasks/:定义task,role的基本元素,至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含
  • handlers/:至少应该包含一个名为main.yml的文件;此目录下的其它的文件需要在此文件中通过include进行包含
  • vars/:定义变量,至少应该包含一个名为main.yml的文件;此目录下的其它的变量文件需要在此文件中通过include进行包含,也可以通过项目目录中的group_vars/all定义变量,从而实现角色通用代码和项目数据的分离
  • meta/:定义当前角色的特殊设定及其依赖关系,至少应该包含一个名为main.yml的文件,其它文件需在此文件中通过include进行包含
  • default/:设定默认变量时使用此目录中的main.yml文件,比vars的优先级低
3 使用ansible playbook实现一个mysql角色。
  1. #mysql角色目录
  2. [18:16:16 root@ansible-rocky opt]$ tree
  3. .
  4. ├── ansible.cfg
  5. ├── hosts
  6. ├── mysql_role.yml
  7. └── roles
  8.    └── mysql
  9.        ├── files
  10.        │   └── mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
  11.        ├── tasks
  12.        │   └── main.yml
  13.        └── templates
  14.            └── my.cnf.j2
  15.            
  16. #定义主机及变量
  17. [18:22:50 root@ansible-rocky opt]$ tail -n9 hosts
  18. [dbsrvs:vars]
  19. db_group=mysql
  20. db_gid=306
  21. db_user=mysql
  22. db_uid=306
  23. db_version=8.0.31
  24. db_file="mysql-{{db_version}}-linux-glibc2.12-x86_64.tar.xz"
  25. db_data_dir="/data/mysql"
  26. db_root_passwd="lgq123456**"
  27. #下载准备mysql源文件包
  28. [18:22:54 root@ansible-rocky opt]$ ls roles/mysql/files/
  29. mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
  30. #创建task文件
  31. [18:24:40 root@ansible-rocky opt]$ cat roles/mysql/tasks/main.yml
  32. - name: install dependent package
  33.   yum:
  34.     name: "{{ item }}"
  35.   loop:
  36.     - libaio
  37.     - numactl-libs
  38. - name: create mysql group
  39.   group: name={{db_group}} gid={{db_gid}}
  40. - name: create mysql user
  41.   user: name={{db_user}} uid={{db_uid}} system=yes shell="/sbin/nologin" create_home=no group={{db_group}}
  42. - name: copy tar to remote host and file mode
  43.   unarchive:
  44.     src: "{{ db_file }}"
  45.     dest: "/usr/local/"
  46.     owner: root
  47.     group: root
  48. - name: create lingfile /usr/local/mysql
  49.   file:
  50.     src: "/usr/local/mysql-{{ db_version }}-linux-glibc2.12-x86_64"
  51.     dest: "/usr/local/mysql"
  52.     state: link
  53. - name: path file
  54.   copy:
  55.     content: "PATH=/usr/local/mysql/bin:$PATH"
  56.     dest: "/etc/profile.d/mysql.sh"
  57. - name: config file
  58.   template:
  59.     src: my.cnf.j2
  60.     dest: "/etc/my.cnf"
  61. - name: create directory
  62.   file:
  63.     name: "/data"
  64.     state: directory
  65. - name: init mysql data
  66.   shell:
  67.     cmd: "/usr/local/mysql/bin/mysqld --initialize-insecure --user={{ db_user }} --datadir={{ db_data_dir }}"
  68.   tags:
  69.     - init
  70. - name: service script
  71.   copy:
  72.     src: "/usr/local/mysql/support-files/mysql.server"
  73.     dest: "/etc/init.d/mysqld"
  74.     remote_src: yes
  75.     mode: '+x'
  76. - name: start service
  77.   shell:
  78.     cmd: chkconfig --add mysqld;chkconfig mysqld on;service mysqld start
  79. - name: change root password
  80.   shell:
  81.     cmd: "/usr/local/mysql/bin/mysqladmin -uroot password {{ db_root_passwd }}"
  82.    
  83.    
  84. #准备MySQL 配置文件模板
  85. [18:25:25 root@ansible-rocky opt]$ cat roles/mysql/templates/my.cnf.j2
  86. [mysqld]
  87. server-id=1
  88. log-bin
  89. datadir={{ db_data_dir }}
  90. socket={{ db_data_dir }}/mysql.sock
  91. log-error={{ db_data_dir }}/mysql.log
  92. pid-file={{ db_data_dir }}/mysql.pid
  93. [client]
  94. socket={{ db_data_dir }}/mysql.sock
  95. #准备MySQL角色playbook文件
  96. [18:25:38 root@ansible-rocky opt]$ cat mysql_role.yml
  97. - hosts: dbsrvs
  98.   remote_user: root
  99.   gather_facts: no
  100.   roles:
  101.     - mysql
  102.    
  103. #部署MySQL
  104. [18:26:34 root@ansible-rocky opt]$ ansible-playbook -i hosts mysql_role.yml
  105. PLAY [dbsrvs] *****************************************************************************************
  106. TASK [mysql : install dependent package] **************************************************************
  107. ok: [10.0.0.38] => (item=libaio)
  108. ok: [10.0.0.38] => (item=numactl-libs)
  109. TASK [mysql : create mysql group] *********************************************************************
  110. changed: [10.0.0.38]
  111. TASK [mysql : create mysql user] **********************************************************************
  112. changed: [10.0.0.38]
  113. TASK [mysql : copy tar to remote host and file mode] **************************************************
  114. changed: [10.0.0.38]
  115. TASK [mysql : create lingfile /usr/local/mysql] *******************************************************
  116. changed: [10.0.0.38]
  117. TASK [mysql : path file] ******************************************************************************
  118. changed: [10.0.0.38]
  119. TASK [mysql : config file] ****************************************************************************
  120. changed: [10.0.0.38]
  121. TASK [mysql : create directory] ***********************************************************************
  122. ok: [10.0.0.38]
  123. TASK [mysql : init mysql data] ************************************************************************
  124. changed: [10.0.0.38]
  125. TASK [mysql : service script] *************************************************************************
  126. changed: [10.0.0.38]
  127. TASK [mysql : start service] **************************************************************************
  128. changed: [10.0.0.38]
  129. TASK [mysql : change root password] *******************************************************************
  130. changed: [10.0.0.38]
  131. PLAY RECAP ********************************************************************************************
  132. 10.0.0.38                  : ok=12   changed=10   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
复制代码
4 基于角色完成部署LNMP架构,并支持一键发布,回滚应用。同时基于zabbix角色批量部署zabbix。

4.1 部署LNMP架构

4.1.1 目录结构
  1. [18:57:47 root@ansible-rocky opt]$ tree /opt/
  2. /opt/
  3. ├── ansible.cfg
  4. ├── hosts
  5. ├── lnmp_role.yml
  6. ├── mysql_role.yml
  7. ├── nginx_role.yml
  8. ├── php-fpm_role.yml
  9. ├── roles
  10. │   ├── mysql
  11. │   │   ├── files
  12. │   │   │   └── mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
  13. │   │   ├── tasks
  14. │   │   │   └── main.yml
  15. │   │   └── templates
  16. │   │       └── my.cnf.j2
  17. │   ├── nginx
  18. │   │   ├── handlers
  19. │   │   │   └── main.yml
  20. │   │   ├── tasks
  21. │   │   │   └── main.yml
  22. │   │   └── templates
  23. │   │       ├── nginx.conf.j2
  24. │   │       └── nginx.service.j2
  25. │   ├── php-fpm
  26. │   │   ├── files
  27. │   │   │   ├── test.php
  28. │   │   │   └── www.conf
  29. │   │   ├── handlers
  30. │   │   │   └── main.yml
  31. │   │   ├── tasks
  32. │   │   │   └── main.yml
  33. │   │   └── templates
  34. │   │       ├── php-fpm.conf.j2
  35. │   │       └── php.ini.j2
  36. │   └── wordpress
  37. │       ├── files
  38. │       │   └── wordpress-6.1.1-zh_CN.zip
  39. │       └── tasks
  40. │           └── main.yml
  41. └── wordpress_role.yml
  42. 17 directories, 22 files
复制代码
4.1.2 LNMP架构所需主机清单以及变量设置
  1. [18:58:15 root@ansible-rocky opt]$ cat hosts
  2. [websrvs]
  3. 10.0.0.18
  4. 10.0.0.28
  5. [websrvs:vars]
  6. version="1.20.2"
  7. url="http://nginx.org/download/nginx-{{ version }}.tar.gz"
  8. install_dir="/apps/nginx"
  9. fqdn="www.yanlinux.org"
  10. root_path="/data/wordpress"
  11. app="wordpress-6.1.1-zh_CN"
  12. [dbsrvs]
  13. 10.0.0.38
  14. [dbsrvs:vars]
  15. db_group=mysql
  16. db_gid=306
  17. db_user=mysql
  18. db_uid=306
  19. db_version=8.0.31
  20. db_file="mysql-{{db_version}}-linux-glibc2.12-x86_64.tar.xz"
  21. db_data_dir="/data/mysql"
  22. db_root_passwd="lgq123456**"
复制代码
4.1.2 实现编译安装nginx角色
  1. #task文件
  2. [17:55:17 root@ansible-rocky roles]$ cat nginx/tasks/main.yml
  3. - name: add group nginx
  4.   group: name=nginx system=yes gid=80
  5. - name: add user nginx
  6.   user: name=nginx group=nginx uid=80 system=yes shell="/sbin/nologin" create_home=no
  7. - name: install dependent package
  8.   yum: name={{item}} state=latest
  9.   loop:
  10.     - gcc
  11.     - make
  12.     - pcre-devel
  13.     - openssl-devel
  14.     - zlib-devel
  15.     - perl-ExtUtils-Embed
  16. - name: get nginx source
  17.   unarchive:
  18.     src: "{{ url }}"
  19.     dest: "/usr/local/src"
  20.     remote_src: yes
  21. - name: compile and install
  22.   shell:
  23.     cmd: "./configure --prefix={{install_dir}} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make && make install"
  24.     chdir: "/usr/local/src/nginx-{{ version }}"
  25.     creates: "{{install_dir}}/sbin/nginx"
  26. - name: config file
  27.   template:
  28.     src: nginx.conf.j2
  29.     dest: "{{install_dir}}/conf/nginx.conf"
  30.     owner: nginx
  31.     group: nginx
  32.   notify: restart service
  33.   tags:
  34.     - config
  35. - name: create directory
  36.   file:
  37.     path: "{{install_dir}}/conf/conf.d"
  38.     state: directory
  39.     owner: nginx
  40.     group: nginx
  41. - name: change install directory owner
  42.   file:
  43.     path: "{{install_dir}}"
  44.     owner: nginx
  45.     group: nginx
  46.     recurse: yes
  47. - name: copy service file
  48.   template:
  49.     src: nginx.service.j2
  50.     dest: "/lib/systemd/system/nginx.service"
  51. - name: check config
  52.   shell:
  53.     cmd: "{{install_dir}}/sbin/nginx -t"
  54.   register: check_nginx_config
  55.   changed_when:
  56.     - check_nginx_config.stdout.find('successful')
  57.     - false
  58. - name: start service
  59.   systemd:
  60.     daemon_reload: yes
  61.     name: nginx.service
  62.     state: started
  63.     enabled: yes
  64.       
  65. #创建handler文件
  66. [17:59:27 root@ansible-rocky roles]$ cat nginx/handlers/main.yml
  67. - name: restart service
  68.   service:
  69.     name: nginx
  70.     state: restarted
  71. #准备两个template文件
  72. [17:59:51 root@ansible-rocky roles]$ cat nginx/templates/nginx.conf.j2
  73. #user  nobody;
  74. user nginx;
  75. worker_processes  {{ ansible_processor_vcpus*2 }};
  76. events {
  77.     worker_connections  1024;
  78. }
  79. http {
  80.     include       mime.types;
  81.     default_type  application/octet-stream;
  82.     log_format  access_json '{"@timestamp":"$time_iso8601",'
  83.         '"host":"$server_addr",'
  84.         '"clientip":"$remote_addr",'
  85.         '"size":$body_bytes_sent,'
  86.         '"responsetime":$request_time,'
  87.         '"upstreamtime":"$upstream_response_time",'
  88.         '"upstreamhost":"$upstream_addr",'
  89.         '"http_host":"$host",'
  90.         '"uri":"$uri",'
  91.         '"xff":"$http_x_forwarded_for",'
  92.         '"referer":"$http_referer",'
  93.         '"tcp_xff":"$proxy_protocol_addr",'
  94.         '"http_user_agent":"$http_user_agent",'
  95.         '"status":"$status"}';
  96.     # logging                                                                                          
  97.     access_log {{install_dir}}/logs/access-json.log access_json;
  98.     error_log {{install_dir}}/logs/error.log warn;
  99.     keepalive_timeout  65;
  100.     include {{install_dir}}/conf/conf.d/*.conf;
  101. }
  102. [18:00:28 root@ansible-rocky roles]$ cat nginx/templates/nginx.service.j2
  103. [Unit]
  104. Description=The nginx HTTP and reverse proxy server
  105. After=network.target remote-fs.target nss-lookup.target
  106. [Service]
  107. Type=forking
  108. PIDFile={{install_dir}}/logs/nginx.pid
  109. ExecStartPre=/bin/rm -f {{install_dir}}/logs/nginx.pid
  110. ExecStartPre={{install_dir}}/sbin/nginx -t
  111. ExecStart={{install_dir}}/sbin/nginx
  112. ExecReload=/bin/kill -s HUP \$MAINPID
  113. KillSignal=SIGQUIT
  114. TimeoutStopSec=5
  115. KillMode=process
  116. PrivateTmp=true                                                                                       
  117. LimitNOFILE=100000
  118. [Install]
  119. WantedBy=multi-user.target
  120. #总入口playbook文件
  121. [18:09:50 root@ansible-rocky opt]$ cat /opt/nginx_role.yml
  122. - hosts: websrvs
  123.   remote_user: root
  124.   roles:
  125.     - nginx
复制代码
4.1.4 实现php-fpm角色
  1. #首先准备php.ini.j2和www.conf文件
  2. #修改php上传限制配置
  3. [17:04:11 root@ansible-rocky ~]$ vi /opt/roles/php-fpm/templates/php.ini.j2
  4. post_max_size = 100M #将次行从8M修改为100M
  5. upload_max_filesize = 100M #将此行从2M改为100M
  6. #修改配置文件
  7. [17:14:03 root@proxy ~]$ vi /opt/roles/php-fpm/files/www.conf
  8. user = nginx #修改为nginx
  9. group = nginx #修改为nginx
  10. ;listen = /run/php-fpm/www.sock #注释此行
  11. listen = 127.0.0.1:9000 #添加此行,监控本机的9000端口
  12. #准备网页配置文件
  13. [19:51:32 root@ansible-rocky opt]$ cat /opt/roles/php-fpm/templates/php-fpm.conf.j2
  14. server {
  15.     listen 80;
  16.     server_name {{ fqdn }};
  17.     location / {
  18.         root           {{ root_path  }};
  19.         fastcgi_pass   127.0.0.1:9000;
  20.         fastcgi_index  index.php;
  21.         fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
  22.         include        fastcgi_params;
  23.     }
  24. }
  25. #准备tasks文件
  26. [19:40:32 root@ansible-rocky opt]$ cat /opt/roles/php-fpm/tasks/main.yml
  27. - name: install package
  28.   yum:
  29.     name: "{{ item }}"
  30.   loop:
  31.     - php-fpm
  32.     - php-mysqlnd
  33.     - php-json
  34.     - php-xml
  35.     - php-gd
  36.     - php-pecl-zip
  37. - name: php path permissions
  38.   file:
  39.     path: /var/lib/php/
  40.     owner: nginx
  41.     group: nginx
  42.     recurse: yes
  43. - name: config php.ini
  44.   template:
  45.     src: php.ini.j2
  46.     dest: /etc/php.ini
  47. - name: config www.conf
  48.   copy:
  49.     src: www.conf
  50.     dest: /etc/php-fpm.d/www.conf
  51. - name: website config
  52.   template:
  53.     src: php-fpm.conf.j2
  54.     dest: "{{ install_dir }}/conf/conf.d/php-fpm.conf"
  55.     owner: nginx
  56.     group: nginx
  57.   notify: restart nginx
  58. - name: start service
  59.   service:
  60.     name: php-fpm
  61.     state: started
  62.     enabled: yes
  63.    
  64. #准备handler文件
  65. [19:53:47 root@ansible-rocky opt]$ cat /opt/roles/php-fpm/handlers/main.yml
  66. - name: restart nginx
  67.   service:
  68.     name: nginx
  69.     state: restarted
  70.    
  71. #准备总入口playbook文件
  72. [19:54:48 root@ansible-rocky opt]$ cat /opt/php-fpm_role.yml
  73. - hosts: websrvs
  74.   remote_user: root
  75.   roles:
  76.     - php-fpm
复制代码
4.1.5 实现MySQL角色

注意:ansible playbook调用mysql系列模块需要依赖python3-mysql包和利用pip安装pymysql
  1. #下载准备mysql源文件包
  2. [18:22:54 root@ansible-rocky opt]$ ls roles/mysql/files/
  3. mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
  4. #创建task文件
  5. [18:24:40 root@ansible-rocky opt]$ cat roles/mysql/tasks/main.yml
  6. - name: install dependent package
  7.   yum:
  8.     name: "{{ item }}"
  9.   loop:
  10.     - libaio
  11.     - numactl-libs
  12.     - python39
  13.     - python3-mysql
  14. - name: install pymysql
  15.   pip:
  16.     name: pymysql
  17.     state: present
  18.    
  19. - name: create mysql group
  20.   group: name={{db_group}} gid={{db_gid}}
  21. - name: create mysql user
  22.   user: name={{db_user}} uid={{db_uid}} system=yes shell="/sbin/nologin" create_home=no group={{db_group}}
  23. - name: copy tar to remote host and file mode
  24.   unarchive:
  25.     src: "{{ db_file }}"
  26.     dest: "/usr/local/"
  27.     owner: root
  28.     group: root
  29. - name: create lingfile /usr/local/mysql
  30.   file:
  31.     src: "/usr/local/mysql-{{ db_version }}-linux-glibc2.12-x86_64"
  32.     dest: "/usr/local/mysql"
  33.     state: link
  34. - name: path file
  35.   copy:
  36.     content: "PATH=/usr/local/mysql/bin:$PATH"
  37.     dest: "/etc/profile.d/mysql.sh"
  38. - name: config file
  39.   template:
  40.     src: my.cnf.j2
  41.     dest: "/etc/my.cnf"
  42. - name: create directory
  43.   file:
  44.     name: "/data"
  45.     state: directory
  46. - name: init mysql data
  47.   shell:
  48.     cmd: "/usr/local/mysql/bin/mysqld --initialize-insecure --user={{ db_user }} --datadir={{ db_data_dir }}"
  49.   tags:
  50.     - init
  51. - name: service script
  52.   copy:
  53.     src: "/usr/local/mysql/support-files/mysql.server"
  54.     dest: "/etc/init.d/mysqld"
  55.     remote_src: yes
  56.     mode: '+x'
  57. - name: start service
  58.   shell:
  59.     cmd: chkconfig --add mysqld;chkconfig mysqld on;service mysqld start
  60. - name: change root password
  61.   shell:
  62.     cmd: "/usr/local/mysql/bin/mysqladmin -uroot password {{ db_root_passwd }}"
  63.    
  64. - name: create {{ wp_db_name }} database
  65.   mysql_db:
  66.     login_host: "localhost"
  67.     login_user: "root"
  68.     login_password: "{{ db_root_passwd }}"
  69.     login_port: 3306
  70.     login_unix_socket: "{{ db_data_dir }}/mysql.sock"
  71.     name: "{{ wp_db_name }}"
  72.     state: present
  73.   when: "{{ wp_db_name }} is defined"
  74. - name: create {{ wp_db_user }}
  75.   mysql_user:
  76.     login_host: "localhost"
  77.     login_user: "root"
  78.     login_password: "{{ db_root_passwd }}"
  79.     login_port: 3306
  80.     login_unix_socket: "{{ db_data_dir }}/mysql.sock"
  81.     name: "{{ wp_db_user}}"
  82.     password: "{{ wp_db_passwd }}"
  83.     priv: "{{ wp_db_name }}.*:ALL"
  84.     host: "10.0.0.%"
  85.     state: present
  86.   when: "{{ wp_db_user }} is defined"
  87.    
  88.    
  89. #准备MySQL 配置文件模板
  90. [18:25:25 root@ansible-rocky opt]$ cat roles/mysql/templates/my.cnf.j2
  91. [mysqld]
  92. server-id=1
  93. log-bin
  94. datadir={{ db_data_dir }}
  95. socket={{ db_data_dir }}/mysql.sock
  96. log-error={{ db_data_dir }}/mysql.log
  97. pid-file={{ db_data_dir }}/mysql.pid
  98. [client]
  99. socket={{ db_data_dir }}/mysql.sock
  100. #准备总入口playbook文件
  101. [18:25:38 root@ansible-rocky opt]$ cat mysql_role.yml
  102. - hosts: dbsrvs
  103.   remote_user: root
  104.   gather_facts: no
  105.   roles:
  106.     - mysql
复制代码
4.2 基于zabbix角色批量部署zabbix

依赖上面搭建好的LNMP架构实现
4.2.1 部署zabbix-server
  1. #总体目录结构
  2. [20:27:58 root@ansible-rocky opt]$ tree
  3. .
  4. ├── ansible.cfg
  5. ├── hosts
  6. ├── hosts_zabbix
  7. ├── roles
  8. │   ├── mysql
  9. │   │   ├── files
  10. │   │   │   ├── create.sql.gz
  11. │   │   │   └── mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
  12. │   │   ├── tasks
  13. │   │   │   └── main.yml
  14. │   │   └── templates
  15. │   │       └── my.cnf.j2
  16. │   ├── nginx
  17. │   │   ├── files
  18. │   │   ├── handlers
  19. │   │   │   └── main.yml
  20. │   │   ├── tasks
  21. │   │   │   └── main.yml
  22. │   │   └── templates
  23. │   │       ├── nginx.conf.j2
  24. │   │       └── nginx.service.j2
  25. │   ├── php-fpm
  26. │   │   ├── files
  27. │   │   │   ├── test.php
  28. │   │   │   └── www.conf
  29. │   │   ├── handlers
  30. │   │   │   └── main.yml
  31. │   │   ├── tasks
  32. │   │   │   └── main.yml
  33. │   │   └── templates
  34. │   │       ├── php-fpm.conf.j2
  35. │   │       └── php.ini.j2
  36. │   └── zabbix_server
  37. │       ├── handlers
  38. │       │   └── main.yml
  39. │       ├── tasks
  40. │       │   └── main.yml
  41. │       └── templates
  42. │           ├── zabbix.conf.j2
  43. │           ├── zabbix_server.conf.j2
  44. │           └── zabbix-server-ngx.conf.j2
  45. └── zabbix_server.yml
  46. 29 directories, 26 files
  47. #主入口playbook
  48. [20:24:45 root@ansible-rocky opt]$ cat zabbix_server.yml
  49. - hosts: websrvs
  50.   remote_user: root
  51.   roles:
  52.     - nginx
  53.     - php-fpm
  54. - hosts: dbsrvs
  55.   remote_user: root
  56.   roles:
  57.     - mysql
  58. - hosts: websrvs
  59.   remote_user: root
  60.   roles:
  61.     - zabbix_server
  62. #tasks文件
  63. [20:30:01 root@ansible-rocky zabbix_server]$ cat /opt/roles/zabbix_server/tasks/main.yml
  64. - name: config zabbix yum repo
  65.   yum_repository:
  66.     name: "ansible_zabbix"
  67.     description: "zabbix repo"
  68.     baseurl: "https://mirrors.aliyun.com/zabbix/zabbix/{{ zabbix_version }}/rhel/{{ ansible_distribution_major_version }}/{{ ansible_architecture }}/"
  69.     gpgcheck: yes
  70.     gpgkey: "https://mirrors.aliyun.com/zabbix/zabbix-official-repo.key"
  71. - name: install zabbix-server
  72.   yum:
  73.     name: "{{ item }}"
  74.   loop:
  75.     - zabbix-server-mysql
  76.     - zabbix-agent2
  77.     - zabbix-get
  78.     - zabbix-web-mysql
  79. - name: copy zabbix_server.conf
  80.   template:
  81.     src: zabbix_server.conf.j2
  82.     dest: /etc/zabbix/zabbix_server.conf
  83.     mode: 0600
  84.   notify:
  85.     - restart zabbix-server
  86.   tags: restart zabbix-server
  87. - name: chown  zabbix-web
  88.   file:
  89.     path:  /etc/zabbix/web   
  90.     state: directory
  91.     owner: nginx
  92.     group: nginx
  93.     recurse: yes
  94. - name: copy zabbix-server web conf
  95.   template:
  96.     src: zabbix-server-ngx.conf.j2
  97.     dest: "{{ install_dir }}/conf/conf.d/zabbix_server_ngx.conf"
  98.     owner: nginx
  99.     group: nginx
  100.   notify:
  101.     - restart nginx
  102. - name: copy zabbix.conf into php-fpm.d
  103.   template:
  104.     src: zabbix.conf.j2
  105.     dest: "/etc/php-fpm.d/zabbix.conf"
  106.   notify:
  107.     - restart php-fpm
  108. - name: start zabbix-server
  109.   service:
  110.     name: zabbix-server
  111.     state: restarted
  112.     enabled: yes
  113.    
  114. #查看handler
  115. [20:34:11 root@ansible-rocky zabbix_server]$ cat /opt/roles/zabbix_server/handlers/main.yml
  116. - name: restart zabbix-server
  117.   service:
  118.     name: zabbix-server
  119.     state: restarted
  120. - name: restart nginx
  121.   service:
  122.     name: nginx
  123.     state: restarted
  124. - name: restart php-fpm
  125.   service:
  126.     name: php-fpm
  127.     state: restarted
  128.    
  129. #查看template文件
  130. [20:34:15 root@ansible-rocky zabbix_server]$ cat /opt/roles/zabbix_server/templates/zabbix.conf.j2
  131. [zabbix]
  132. user = nginx
  133. group = nginx
  134. listen = /run/php-fpm/zabbix.sock
  135. listen.acl_users = apache,nginx
  136. listen.allowed_clients = 127.0.0.1
  137. pm = dynamic
  138. pm.max_children = 50
  139. pm.start_servers = 5
  140. pm.min_spare_servers = 5
  141. pm.max_spare_servers = 35
  142. pm.max_requests = 200
  143. php_value[session.save_handler] = files
  144. php_value[session.save_path]    = /var/lib/php/session
  145. php_value[max_execution_time] = 300
  146. php_value[memory_limit] = 128M
  147. php_value[post_max_size] = 80M
  148. php_value[upload_max_filesize] = 80M
  149. php_value[max_input_time] = 300
  150. php_value[max_input_vars] = 10000
  151. php_value[date.timezone] = Asia/Shanghai
  152. [20:38:05 root@ansible-rocky zabbix_server]$ grep -Ev '^$|#' /opt/roles/zabbix_server/templates/zabbix_server.conf.j2
  153. LogFile=/var/log/zabbix/zabbix_server.log
  154. LogFileSize=0
  155. PidFile=/var/run/zabbix/zabbix_server.pid
  156. SocketDir=/var/run/zabbix
  157. DBHost=10.0.0.58
  158. DBName=zabbix
  159. DBUser=zabbix
  160. DBPassword=lgq123456
  161. SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
  162. Timeout=4
  163. AlertScriptsPath=/usr/lib/zabbix/alertscripts
  164. ExternalScripts=/usr/lib/zabbix/externalscripts
  165. LogSlowQueries=3000
  166. StatsAllowedIP=127.0.0.1
  167. ##zabbix网页配置文件
  168. [20:39:05 root@ansible-rocky zabbix_server]$ cat /opt/roles/zabbix_server/templates/zabbix-server-ngx.conf.j2
  169. server {
  170.     listen 80;
  171.     server_name {{ zabbix_fqdn }};
  172.     root /usr/share/zabbix;
  173.     index index.php;
  174.     location = /favicon.ico {
  175.         log_not_found   off;
  176.     }
  177.     location / {
  178.         try_files       $uri $uri/ =404;
  179.     }
  180.     location /assets {
  181.          access_log      off;
  182.          expires         10d;
  183.     }
  184.     location ~ /\.ht {
  185.          deny            all;
  186.     }
  187.     location ~ /(api\/|conf[^\.]|include|locale|vendor) {
  188.          deny            all;
  189.          return          404;
  190.     }
  191.     location ~ [^/]\.php(/|$) {
  192.         fastcgi_pass   127.0.0.1:9000;
  193.         #fastcgi_pass    unix:/run/php-fpm/zabbix.sock;
  194.         fastcgi_split_path_info ^(.+\.php)(/.+)$;
  195.         fastcgi_index   index.php;
  196.         fastcgi_param   DOCUMENT_ROOT   /usr/share/zabbix;
  197.         fastcgi_param   SCRIPT_FILENAME /usr/share/zabbix$fastcgi_script_name;
  198.         fastcgi_param   PATH_TRANSLATED /usr/share/zabbix$fastcgi_script_name;
  199.         include fastcgi_params;
  200.         fastcgi_param   QUERY_STRING    $query_string;
  201.         fastcgi_param   REQUEST_METHOD  $request_method;
  202.         fastcgi_param   CONTENT_TYPE    $content_type;
  203.         fastcgi_param   CONTENT_LENGTH  $content_length;
  204.         fastcgi_intercept_errors        on;
  205.         fastcgi_ignore_client_abort     off;
  206.         fastcgi_connect_timeout         60;
  207.         fastcgi_send_timeout            180;
  208.         fastcgi_read_timeout            180;
  209.         fastcgi_buffer_size             128k;
  210.         fastcgi_buffers                 4 256k;
  211.         fastcgi_busy_buffers_size       256k;
  212.         fastcgi_temp_file_write_size    256k;
  213.     }
  214. }
复制代码

4.2.2 部署zabbix-agent
  1. #目录结构
  2. [22:59:31 root@ansible-rocky zabbix_agent2]$ tree
  3. .
  4. ├── files
  5. │   └── zabbix_agnet2.d
  6. │       ├── login.conf
  7. │       ├── mem.conf
  8. │       ├── mysql.conf
  9. │       ├── mysql_repl_status.sh
  10. │       ├── mysql.sh
  11. │       ├── nginx_status.conf
  12. │       ├── nginx_status.sh
  13. │       └── tcp_state.conf
  14. ├── handlers
  15. │   └── main.yml
  16. ├── tasks
  17. │   └── main.yml
  18. └── templates
  19.     └── zabbix_agent2.conf.j2
  20. 5 directories, 11 files
  21. #task文件
  22. [23:14:12 root@ansible-rocky opt]$ cat /opt/roles/zabbix_agent2/tasks/main.yml
  23. - name: install repo
  24.   yum_repository:
  25.     name: "ansible_zabbix"
  26.     description: "zabbix repo"
  27.     baseurl: "https://mirrors.aliyun.com/zabbix/zabbix/{{ zabbix_version }}/rhel/{{ ansible_distribution_major_version }}/{{ ansible_architecture }}/"
  28.     gpgcheck: yes
  29.     gpgkey: "https://mirrors.aliyun.com/zabbix/zabbix-official-repo.key"
  30. - name: install agent2 for centos or rocky
  31.   yum:
  32.     name: zabbix-agent2
  33.   when:
  34.     - ansible_distribution == "Rocky" or ansible_distribution == "Centos"
  35. - name: install agent2 for centos or ubuntu
  36.   apt:
  37.     name: zabbix-agent2
  38.     update_cache: yes
  39.   when:
  40.     - ansible_distribution == "Ubuntu"
  41. - name: config file
  42.   template:
  43.     src: zabbix_agent2.conf.j2
  44.     dest: "/etc/zabbix/zabbix_agent2.conf"
  45.     mode: 0644
  46.   notify:
  47.     - restart zabbix-agent2
  48. - name: copy zabbix-agent2.d content
  49.   copy:
  50.     src: zabbix_agent2.d
  51.     dest: "/etc/zabbix"
  52.   notify:
  53.     - restart zabbix-agent2
  54.   tags: zabbix_agent2.d
  55. - name: start zabbix-agent2
  56.   service:
  57.     name: zabbix-agent2
  58.     state: started
  59.     enabled: yes
  60.    
  61. #handler文件
  62. [23:14:14 root@ansible-rocky opt]$ cat /opt/roles/zabbix_agent2/handlers/main.yml
  63. - name: restart zabbix_agent2
  64.   service:
  65.     name: zabbix-agent2
  66.     state: restarted
  67.    
  68. #template文件
  69. [23:14:43 root@ansible-rocky opt]$ cat /opt/roles/zabbix_agent2/templates/zabbix_agent2.conf.j2
  70. PidFile=/var/run/zabbix/zabbix_agent2.pid
  71. LogFile=/var/log/zabbix/zabbix_agent2.log
  72. LogFileSize=0
  73. Server={{ zabbix_server_ip }}
  74. ServerActive={{ zabbix_server_ip }}
  75. Hostname={{ ansible_default_ipv4.address }}
  76. Include=/etc/zabbix/zabbix_agent2.d/*.conf
  77. ControlSocket=/tmp/agent.sock
复制代码
4.2.3 测试
  1. [23:15:29 root@rocky8 /etc/zabbix]$ zabbix_get -s 10.0.0.18 -k mem_use_percent
  2. 20.1886
  3. [23:16:51 root@rocky8 /etc/zabbix]$ zabbix_get -s 10.0.0.18 -k tcp_state[ESTABLISHED]
  4. 32
  5. [23:17:30 root@rocky8 /etc/zabbix]$ zabbix_get -s 10.0.0.28 -k tcp_state[ESTABLISHED]
  6. 28
  7. [23:17:35 root@rocky8 /etc/zabbix]$ zabbix_get -s 10.0.0.58 -k tcp_state[ESTABLISHED]
  8. 55
复制代码
来源:https://www.cnblogs.com/yan-linux/p/17039200.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具