1. 程式人生 > >3.中小型企業通用自動化運維架構 -- Ansible playbook

3.中小型企業通用自動化運維架構 -- Ansible playbook

實戰:

---
- hosts: test
  remote_user: root
  tasks:
  - name: Hello World
    shell: ls /root
---
- hosts: test
  remote_user: root
  vars:
    com: /root
  tasks:
  - name: Hello World
    shell: ls {{ com }}
---
- hosts: test
  remote_user: root
  become: true
  tasks:
  - name: install python for centos
    yum:
      name: "{{ item }}"
      state: installed
    with_items:
        - python-devel
        - python-setuptools
    when: ansible_distribution == 'CentOS'
  - name: install python for ubuntu
    apt:
      name: "{{ item }}"
      state: latest
      update_cache: yes
    with_items:
        - libpython-dev
        - python-setuptools
    when: ansible-distribution == 'Ubuntu'
  - name: install pip
    shell: easy_install pip
  - name: pip install flask and redis
    pip:
      name: "{{ item }}"
      with_items:
        - flask
        - redis

安裝 zabbix:

---
- hosts: test
  become: true
  tasks:
  - name: install zabbix rpm
    yum:
        name: zabbix yum 路徑
        state: installed
    when: ansible_distribution == 'CentOS'
  - name: download zabbix deb
    get_url:
        url: deb 路徑
        dest: /tmp/zabbix.deb
    when: ansible_distribution == 'Ubuntu'
  - name: install zabbix deb
    apt:
        name: /tmp/zabbix.deb
        state: installed
    when: ansible_distribution == 'Ubuntu'
  - name: install zabbix
    yum:
        name: "{{ item }}"
        state: installed
        with_item:
        - zabbix-server-mysql
        - zabbix-proxy-mysql
        - zabbix-web-mysql
        when: ansible_distribution == 'CentOS'
  - name: config zabbix server
    replace:
        path: /etc/zabbix/zabbix_server.conf
        regexp: DBUser=zabbix
        replace: DBUser=root
    when: ansible_distribution == 'CentOS'
  - name: import db format
    shell: zcat ...
  - name: disable selinux
    selinux:
        state: disabled
   when: ansible_distribution == 'CentOS'
  - name: start zabbix server
    systemd:
        name: zabbix-server
        state: started
    when: ansible_distribution == 'CentOS'
 - name: start zabbix client
   systemd:
        name: zabbix-agent
        state: started
   when: ansible_distribution == 'CentOS'