zoukankan      html  css  js  c++  java
  • 自动化运维-Ansible-playbook

    Ansible Playbook

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

    • 什么是Ansible Playbook
    • Playbook使用入门
    • Playbook实战

    什么是Ansible Playbook

    • 是一门编程语言
    • 命令集合
    • YAML格式

    功能列表

    • 声明配置
    • 编排复杂任务
    • 控制任务执行

    与Adhoc关系

    • Playbook是对Adhoc的编排
    • Adhoc适合简单快速的任务
    • Playbook适合复杂异步的任务

    支持的特性

    • 变量定义
    • 顺序结构
    • 选择结构
    • 循环结构
    Playbook起步
    • 选定Host
    • 指定登录用户
    • 使用Shell模块输出Hello World

    hosts行的内容是一个或多个组或主机patterns

    remote_user就是帐号名:

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

    Playbook基本结构

    • 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'
    
  • 相关阅读:
    LeetCode 189. Rotate Array
    LeetCode 965. Univalued Binary Tree
    LeetCode 111. Minimum Depth of Binary Tree
    LeetCode 104. Maximum Depth of Binary Tree
    Windows下MySQL的安装与配置
    LeetCode 58. Length of Last Word
    LeetCode 41. First Missing Positive
    LeetCode 283. Move Zeroes
    《蚂蚁金服11.11:支付宝和蚂蚁花呗的技术架构及实践》读后感
    删除docker下的镜像
  • 原文地址:https://www.cnblogs.com/only-me/p/11429974.html
Copyright © 2011-2022 走看看