自動化運維-Ansible-playbook

Ansible Playbook

https://ansible-tran.readthedocs.io/en/latest/docs/playbooks_intro.html Ansible中文網址html

  • 什麼是Ansible Playbook
  • Playbook使用入門
  • Playbook實戰

什麼是Ansible Playbookpython

  • 是一門編程語言
  • 命令集合
  • YAML格式

功能列表mysql

  • 聲明配置
  • 編排複雜任務
  • 控制任務執行

與Adhoc關係linux

  • Playbook是對Adhoc的編排
  • Adhoc適合簡單快速的任務
  • Playbook適合複雜異步的任務

支持的特性web

  • 變量定義
  • 順序結構
  • 選擇結構
  • 循環結構
Playbook起步
  • 選定Host
  • 指定登陸用戶
  • 使用Shell模塊輸出Hello World

hosts行的內容是一個或多個組或主機patternsredis

remote_user就是賬號名:sql

---
- hosts: test
  remote_user: root
  tasks:
  - name: hello world
    shell: ls /root

Playbook基本結構shell

  • host: 被操做的機器的正則
  • remote_user: 登陸機器的用戶
  • tasks: 須要在機器上執行的任務

變量定義:數據庫

  • 以字母、數字以及下劃線組成
  • 始終應該以字母開頭

變量位置編程

  • Inventory

  • Playbook

    每個 task 必須有一個名稱 name,這樣在運行 playbook 時,從其輸出的任務執行信息中能夠很好的辨別出是屬於哪一ra個 task 的. 若是沒有定義 name,‘action’ 的值將會用做輸出信息中標記特定的 task.

    ---
    - hosts: test
      remote_user: root
      vars:
        com: ls /root
      tasks:
      - name: hello world
        shell: "{{ com }}"

    YAML語法要求若是值以{{ foo }}開頭的話咱們須要將整行用雙引號包起來.這是爲了確認你不是想聲明一個YAML字典

系統變量

  • ansible hostname -m setup
  • {{ ansible_devices.sda.model }}
  • jinjia2模版

Ansible經常使用模塊

  • ping 檢查指定節點機器是否還能連通,主機若是在線,則回覆pong
  • raw 執行原始的命令
  • yum RedHat和CentOS的軟件包安裝和管理工具。
  • apt Ubuntu/Debian的軟件包安裝和管理工具。
  • pip 用於管理Python庫依賴項,爲了使用pip模塊,必須提供參數name或者requirements。
  • synchronize 使用rsync同步文件,將主控方目錄推送到指定節點的目錄下
  • template 基於模板方式生成一個文件複製到遠程主機(template使用Jinjia2格式做爲文件模版,進行文檔內變量的替換的模塊
  • copy 在遠程主機執行復制操做文件
  • user 和group user模塊是請求的是useradd, userdel, usermod三個指令,goup模塊請求的是groupadd, groupdel, groupmod 三個指令
  • service 用於管理遠程主機的服務
  • get_url 該模塊主要用於從http、ftp、https服務器上下載文件(相似於wget)
  • fetch 它用於從遠程機器獲取文件,並將其本地存儲在由主機名組織的文件樹中
  • file 主要用於遠程主機上的文件操做
  • lineinfile 遠程主機上的文件編輯模塊
  • unarchive 用於解壓文件
  • command 和 shell 用於在各被管理節點運行指定的命令. shell和command的區別:shell模塊能夠特殊字符,而command是不支持
  • hostname 修改遠程主機名的模塊
  • script 在遠程主機上執行主控端的腳本,至關於scp+shell組合
  • stat 獲取遠程文件的狀態信息,包括atime,ctime,mtime,md5,uid,gid等信息
  • cron 遠程主機crontab配置
  • mount 掛載文件系統
  • find 幫助在被管理主機中查找符合條件的文件,就像 find 命令同樣
  • selinux 遠程管理受控節點的selinux的模塊

經常使用參數配置:

  • ansible_ssh_host # 目標主機地址
  • ansible_ssh_port #目標主機端口
  • ansible_ssh_user # 目標主機用戶
  • ansible_ssh_pass #目標主機ssh密碼
  • ansible_sudo_pass # sudo密碼
  • ansible_sudo_exe
  • ansible_connection # 與主機的鏈接類型,好比:local, ssh,paramiko
  • ansible_ssh_private_key_file # 私鑰地址
  • ansible_shell_type # 目標系統的shell類型
  • ansible_python_interpreter # python版本

when語句

tasks:
- name: "shutdown Debian flavored systems"
  command: /sbin/shutdown -t now
  when: ansible_os_family == "Debian"

bool值

vars:
     epic: true
     
tasks:
- shell: echo "This certainly is epic"
  when: epic
- shell: echo "This certainly is epic"
  when: not epic

with_items循環語句

- name: add several users
  user: name={{ item }} state=present groups=wheel
  with_items:
    - testuser1
    - testuser2

with_nested 嵌套循環

- name: users access control
  mysql_user: name={{ item[0] }}
              priv={{ item[1] }}.*:All
              append_privs=yes
              password=foo
              
  with_nested:
    - ['alice','bob']
    - ['clientdb','employeedb','providerdb']

有條件的循環

tasks:
  - command: echo {{item}}
  with_items: [0,2,4,6,8,10]
  when: item > 5

Playbook實戰

需求分析

  • python Flask開發環境
  • 具有數據庫和緩存的功能
---
- hosts: test
  remote_user: root
  become: true   # root用戶能夠省去這部
  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: lastest
      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

出現報錯,報錯信息寫的很明白,版本問題在網上也沒有找到答案,只能根據不一樣版本先這麼寫,後面再看看資料補充

[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a 
loop to supply multiple items and specifying `name: "{{ item }}"`, please use `name: ['python-devel', 'python-setuptools']`
 and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
ansible-playbook --version
ansible-playbook 2.8.4
---
- hosts: test
  remote_user: root
  become: true  
  tasks:
  - name: install python for centos
    yum:
      name: ['python-devel','python-setuptools']
      state: installed
    when: ansible_distribution == 'CentOS'
  - name: install python for ubuntu
    apt:
      name: ['libpython-dev','python-setuptools']
      state: lastest
      update_cache: yes
    when: ansible_distribution == 'Ubuntu'
  - name: install pip
    shell: easy_install pip
  - name: pip install flask and redis
    pip:
      name: ['flask','redis']

4.實戰-Zabbix安裝

zabbix安裝需求

  • zabbix server 安裝
  • Master和Client,Centos和Ubuntu各一個
  • Zabbix進程啓動正常
---
- hosts: test
  become: true
  tasks:
  - name: install zabbix rpm source
    yum:
      name: http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm
      state: installed
    when: ansible_distribution == 'CentOS'
  - name: donwload ubuntu deb
    get_url:
      url: http://repo.zabbix.com/zabbix/3.4/ubuntu/pool/main/z/zabbix-release/zabbix-release_3.4-1+xenial_all.deb
      dest: /tmp/zabbix.deb
    when: ansible_distribution == 'Ubuntu'
  - name: install zabbix source
    apt:
      deb: /tmp/zabbix.deb
    when: ansible_distribution == 'Ubuntu'
  - name: install centos zabbix package
    yum:
      name: ['zabbix-server-mysql','zabbix-proxy-mysql','zabbix-web-mysql']
      state: installed
    when: ansible_distribution == 'CentOS'
  - name: install ubuntu zabbix package
    yum:
      name: zabbix-agent
      update_cache: yes
    when: ansible_distribution == 'Ubuntu'
  - name: modify zabbix config
    replace:
      path:  /etc/zabbix/zabbix_server.conf 
      regexp: DBUser=zabbix
      replace: DBUser=root
    when: ansible_distribution == 'CentOS'
  - name: import zabbix sql table
    shell: zcat /usr/share/doc/zabbix-server-mysql-3.4.7/create.sql.gz | mysql -uroot zabbix
    when: ansible_distribution == 'CentOS'
  - 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-agent
    systemd:
      name: zabbix-agent
      state: started
    when: ansible_distribution == 'CentOS'
相關文章
相關標籤/搜索