ansible

举报
winnerwhy 发表于 2024/09/26 18:08:26 2024/09/26
【摘要】 1.配置数据库[root@zabbix-server ~]# find / -name schema.sql [root@zabbix-server ~]# ls /usr/share/zabbix-mysql/[root@ansible zabbix]# cat mariadb_cfg.yaml - name: 配置数据库 hosts: server tasks: - name: 设...

1.配置数据库

[root@zabbix-server ~]# find / -name schema.sql 

[root@zabbix-server ~]# ls /usr/share/zabbix-mysql/

[root@ansible zabbix]# cat mariadb_cfg.yaml 
- name: 配置数据库
  hosts: server
  tasks:
  - name: 设置数据库登录密码
    shell: mysqladmin -uroot password password
    ignore_errors: yes
  - name: 创建数据库
    shell: mysql -uroot -ppassword -e "create database zabbix;"
    ignore_errors: yes
  - name: 创建用户zabbix
    shell: mysql -uroot -ppassword -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'password';flush privileges;"
    ignore_errors: yes
  - name: 导入sql
    shell: mysql -uroot -ppassword -e "use zabbix;source /usr/share/zabbix-mysql/schema.sql;source /usr/share/zabbix-mysql/images.sql; source /usr/share/zabbix-mysql/data.sql;"
    ignore_errors: yes
  - name: 重启mariadb
    service:
      name: mariadb
      state: restarted
      enabled: yes

[root@ansible zabbix]# ansible-playbook mariadb_cfg.yaml

2.编辑zabbix配置文件

[root@zabbix-server ~]# find / -name  zabbix_server.conf

[root@ansible zabbix]# scp zabbix_server:/etc/zabbix_server.conf .

[root@ansible zabbix]# scp zabbix_server:/etc/zabbix_agentd.conf .

[root@ansible zabbix]# vim zabbix_server.conf

DBPassword=password

[root@ansible zabbix]# vim zabbix_agentd.conf

Server=192.168.100.20
ServerActive=192.168.100.20
Hostname=192.168.100.20

[root@ansible zabbix]# mv zabbix_server.conf  zabbix_server.j2

[root@ansible zabbix]# mv zabbix_agentd.conf  zabbix_agentd.j2

[root@ansible zabbix]# cat zsa.yaml 
- name: 编排zabbix
  hosts: server
  tasks:
  - name: 复制server配置文件
    template:
      src: ./zabbix_server.conf.j2
      dest: /etc/zabbix_server.conf
  - name: 复制agent配置文件
    template:
      src: ./zabbix_agentd.conf.j2
      dest: /etc/zabbix_agentd.conf
  - name: 重启zabbix
    service:
      name: "{{ item }}"
      state: restarted
    loop:
      - zabbix-server
      - zabbix-agent

[root@ansible zabbix]# ansible-playbook zsa.yaml

3.编辑php配置文件

[root@zabbix-server ~]# find / -name php.ini

[root@ansible zabbix]# scp zabbix_server:/etc/remi/php74/php.ini .

[root@ansible zabbix]# vim php.ini

post_max_size = 16M
max_execution_time = 300
max_input_time = 300
datetimezone = Asia/Shanghai

[root@ansible zabbix]# mv php.ini php.ini.j2

[root@ansible zabbix]# cat php.yaml 
- name: 编辑php
  hosts: server
  tasks:
  - name: 复制php配置文件
    template:
      src: ./php.ini.j2
      dest: /etc/opt/remi/php74/php.ini
  - name: 重启php
    service:
      name: "{{item}}"
      state: restarted
    loop:
    - php74-php-fpm

[root@ansible zabbix]# ansible-playbook php.yaml

4.配置www.conf

[root@zabbix-server ~]# find / -name  www.conf

[root@ansible zabbix]# scp zabbix_server:/etc/php-fpm.d/www.conf .

[root@ansible zabbix]# vim www.conf

user = nginx
group = nginx

[root@ansible zabbix]# mv www.conf www.conf.j2 

[root@ansible zabbix]# cat www.yaml 
- name: 编辑php
  hosts: server
  tasks:
  - name: 复制php配置文件
    template:
      src: ./www.conf.j2 
      dest: /etc/php-fpm.d/www.conf
  - name: 重启php
    service:
      name: "{{item}}"
      state: restarted
    loop:
    - php74-php-fpm

[root@ansible zabbix]# ansible-playbook www.yaml

5.编辑nginx配置文件

[root@zabbix-server ~]# find / -name default.conf

[root@ansible zabbix]# scp zabbix_server:/etc/nginx/conf.d.default.conf .

[root@ansible zabbix]# vim default.conf 

    root   /usr/share/zabbix;
location ~ \.php$ {                       #让这一段生效
    root    /usr/share/zabbix;         #路径和上面一样
    fastcgi_pass 127.0.0.1:9000;     
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;  #这行要修改

[root@ansible zabbix]# mv default.conf default.conf.j2

[root@ansible zabbix]# cat default.yaml 
- name: 编辑nginx
  hosts: server
  tasks:
  - name: 复制nginx配置文件
    template:
      src: ./default.conf.j2.j2 
      dest: /etc/nginx/conf.d.default.conf
  - name: 重启nginx
    service:
      name: "{{item}}"
      state: restarted
    loop:
    - nginx

[root@ansible zabbix]# ansible-playbook default.yaml 

6.配置zabbix.conf

[root@zabbix-server ~]# find / -name zabbix.conf

[root@ansible zabbix]# scp zabbix_server:/etc/php-fpm.d/zabbix.conf .

[root@ansible zabbix]#vim zabbix.conf
user = nginx
group = nginx

[root@ansible zabbix]# mv zabbix.conf zabbix.conf.j2

[root@ansible zabbix]# cat zabbix.yaml 
- name: 编辑zabbix.conf
  hosts: server
  tasks:
  - name: 复制zabbix.conf配置文件
    template:
      src: ./zabbix.conf.j2 
      dest: /etc/php-fpm.d/zabbix.conf
  - name: 重启相关服务
    service:
      name: "{{item}}"
      state: restarted
    loop:
    - zabbix_server
    - zabbix-agent
    - php74-php-fpm
    - mariadb
    - nginx

[root@ansible zabbix]# ansible-plabook zabbix.yaml

7.编写playbook

[root@ansible zabbix]# cat agent.yaml 
- name: 配置agent节点
  hosts: agent
  tasks:
    - name: 删除repo
      file:
        path: /etc/yum.repos.d
        state: absent

    - name: 设置755
      file:
        path: /etc/yum.repos.d
        state: directory
        mode: 755

    - name: 复制
      copy:
        src: files/ftp.repo
        dest: /etc/yum.repos.d/

    - name: 安装zabbix-agent
      yum:
        name: zabbix6.0-agent

    - name: 复制zabbix配置文件
      template:
        src: ./zabbix_agentd.conf.j2
        dest: /etc/zabbix_agentd.conf

    - name: 重启zabbix-agent
      service:
        name: "{{ item }}"
        state: restarted
      loop:
        - zabbix-agent

[root@ansible zabbix]# ansible-playbook agent.yaml

【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。