Playbook集合
Playbook集合

Playbook集合

配置时间同步(timesync.yml)(v1.0)

---
- name: timesync
  hosts: all
  tasks:
    - name: block1
      block: 
        - name: rocky
          yum: 
            name: chrony
            state: present
        - name: file
          copy:
            content: "server 10.0.0.163 iburst"
            dest: /etc/chrony.conf 
        - name: service
          service:
            name: chronyd
            state: restarted
            enabled: yes
      when: "'RedHat' in ansible_os_family"
    - name: block2
      block:  
        - name: ubuntu
          apt: 
            name: chrony
            state: present
        - name: file
          copy:
            content: "server 10.0.0.163 iburst"
            dest: /etc/chrony/chrony.conf 
        - name: service
          service:
            name: chrony
            state: restarted
            enabled: yes
      when: "'Debian' in ansible_os_family"

安装zabbix-agent2(install_zabbix_agent2.yml)(v2.0)

---
- name: install zabbix_agent2
  hosts: all 
  serial: 2
  vars:
    - ZX_SERVER: "10.0.0.100"
    - ZX_VER: "5.0"
    - ZX_URL: "https://mirror.tuna.tsinghua.edu.cn/zabbix"
  tasks: 
    - name: rocky8
      block:
        - name: rocky repo
          shell: "rpm -Uvh {{ ZX_URL }}/zabbix/{{ ZX_VER }}/rhel/8/x86_64/zabbix-release-{{ ZX_VER }}-1.el8.noarch.rpm"
          ignore_errors: yes
        - name: replace uri
          replace: 
            path: /etc/yum.repos.d/zabbix.repo
            regexp: "http://repo.zabbix.com"
            replace: "{{ ZX_URL }}"
        - name: rocky install 
          yum: 
            name: zabbix-agent2
            state: present
      when: ansible_distribution_file_variety == "RedHat"

    - name: ubuntu2004  
      block:
        - name: ubuntu apt
          shell: "wget {{ ZX_URL }}/zabbix/{{ ZX_VER }}/ubuntu/pool/main/z/zabbix-release/zabbix-release_{{ ZX_VER }}-1+focal_all.deb"
        - name: dpkg
          shell: "dpkg -i zabbix-release_{{ ZX_VER }}-1+focal_all.deb"
        - name: replace uri
          replace:
            path: /etc/apt/sources.list.d/zabbix.list
            regexp: "http://repo.zabbix.com"
            replace: "{{ ZX_URL }}"
        - name: apt
          apt:
            update_cache: yes
            name: zabbix-agent2
            state: present
      when: ansible_distribution_file_variety == "Debian"

    - name: config hostname
      replace: 
        path: /etc/zabbix/zabbix_agent2.conf
        regexp: "Hostname=Zabbix server"
        replace: "Hostname={{ ansible_eth0.ipv4.address }}"
      notify:
        - restart agent2

    - name: config Server
      replace:
        path: /etc/zabbix/zabbix_agent2.conf
        regexp: "Server=127.0.0.1"
        replace: "Server={{ ZX_SERVER }}"
      notify:
        - restart agent2

    - name: service
      service: 
        name: zabbix-agent2.service
        state: started
        enabled: yes

  handlers:
    - name: restart agent2
      service: 
        name: zabbix-agent2.service
        state: restarted