zoukankan      html  css  js  c++  java
  • 自动化运维工具 Ansible 安装、配置及使用

    一、官方文档地址:https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

    二、ubuntu 20.04 安装 Ansible

    $ sudo apt update
    $ sudo apt install software-properties-common
    $ sudo apt-add-repository --yes --update ppa:ansible/ansible
    $ sudo apt install ansible
    
    On older Ubuntu distributions, “software-properties-common” is called “python-software-properties”. 
    You may want to use apt-get instead of apt in older versions. Also, be aware that only newer distributions
    (i.e. 18.04, 18.10, etc.) have a -u or --update flag, so adjust your script accordingly.

    按照官方文档的补充说明,在老版 ubuntu 中,需要将 software-properties-common 更改为 python-software-properties,根据本人以往安装该依赖的实际经验,
    ubuntu16.04 以后的版本(不包括 ubuntu16.04)已更改为 software-properties-common

    这里建议使用 pip 的方式进行安装:
      
      pip install ansible

      In order to use the paramiko connection plugin or modules that require paramiko, install the required module:

        
    pip install paramiko
     

    三、这里主要对使用 ansible-playbook 模式 进行自动化运维进行一下记录,如果有兴趣希望继续了解  ad-hoc (临时任务执行模式),可以去研读官方文档

    3.1、ansible-playbook 的定义及作用

    About Playbooks
    Playbooks are a completely different way to use ansible than in ad-hoc task execution mode, and are particularly powerful.
    
    Simply put, playbooks are the basis for a really simple configuration management and multi-machine deployment system, unlike any that already exist, and one that is very well suited to deploying complex applications.
    
    Playbooks can declare configurations, but they can also orchestrate steps of any manual ordered process, even as different steps must bounce back and forth between sets of machines in particular orders. They can launch tasks synchronously or asynchronously.
    
    While you might run the main /usr/bin/ansible program for ad-hoc tasks, playbooks are more likely to be kept in source control and used to push out your configuration or assure the configurations of your remote systems are in spec.
    
    There are also some full sets of playbooks illustrating a lot of these techniques in the ansible-examples repository. We’d recommend looking at these in another tab as you go along.

    谷歌翻译如下:
      关于剧本

        与临时任务执行模式相比,剧本是使用ansible的完全不同的方式,并且功能特别强大。

        简而言之,剧本是真正简单的配置管理和多机部署系统的基础,这与现有的系统不同,并且非常适合于部署复杂的应用程序。

        剧本可以声明配置,但是它们也可以编排任何手动订购的过程的步骤,即使不同的步骤必须在特定顺序的机器之间来回跳动也是如此。他们可以同步或异步启动任务。

        尽管您可能会运行/usr/bin/ansible主程序来执行临时任务,但更可能将剧本保留在源代码管理中,并用于推送您的配置或确保远程系统的配置符合规范。

     个人理解就是:ansible-playbook 是操作或者部署服务器的流程指令的编排语言(或者说工具)

     3.2、yaml 格式文件的语法规范

    https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html#yaml-syntax

    3.3、ansible-playbook 语法结构示例

    https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#playbooks-intro

    ---
    - hosts: webservers  # 需要和 hosts 文件中的主机名对应(不是组名)
      vars:
        http_port: 80
        max_clients: 200
      remote_user: root
      tasks:  # 定义一个任务
      - name: ensure apache is at the latest version  # 任务名称
        yum:  # ansible-playbook 的一个模块,官方已提供了大量模块,如若官方模块不满足,可根据实际情况使用 shell 及 command 模块
          name: httpd
          state: latest
      - name: write the apache config file
        template:
          src: /srv/httpd.j2
          dest: /etc/httpd.conf
        notify:
        - restart apache
      - name: ensure apache is running
        service:
          name: httpd
          state: started
      handlers:
        - name: restart apache
          service:
            name: httpd
            state: restarted

    一个 playbook 中 可同时定义多个服务:

    ---
    - hosts: webservers
      remote_user: root
    
      tasks:
      - name: ensure apache is at the latest version
        yum:
          name: httpd
          state: latest
      - name: write the apache config file
        template:
          src: /srv/httpd.j2
          dest: /etc/httpd.conf
    
    - hosts: databases
      remote_user: root
    
      tasks:
      - name: ensure postgresql is at the latest version
        yum:
          name: postgresql
          state: latest
      - name: ensure that postgresql is started
        service:
          name: postgresql
          state: started

    3.2 hosts 文件配置

      默认情况下,ansible  会默认去读取 /etc/ansible/hosts 文件内的主机连接信息,但是可以通过以下方式指定 hosts 文件

    -i INVENTORY, --inventory INVENTORY, --inventory-file INVENTORY

      hosts 文件的配置方式

    [test1]
    db        ansible_ssh_host=192.168.3.25 ansible_ssh_user="root" ansible_ssh_pass="pwd" ansible_ssh_port=22
    webserver ansible_ssh_host=192.168.3.26 ansible_ssh_user="root" ansible_ssh_pass="pwd" ansible_ssh_port=22

      这里定义了 test1 这么一个组名,不定义组名也可以,但是定义组名是有用处的,例如可以通过组名进行组合,方便调用:

    [test1]
    db ansible_ssh_host=192.168.3.25 ansible_ssh_user="root" ansible_ssh_pass="pwd" ansible_ssh_port=22

    [test2]
    webserver ansible_ssh_host=192.168.3.26 ansible_ssh_user="root" ansible_ssh_pass="pwd" ansible_ssh_port=22

    [test:children]
    test1
    test2
    # 如果仅需要操作其中某一个组时,指定响应的组名就可以,如果需要操作所有组,可通过指定为 test 实现,ansible hosts 还有更多复杂组合形式,详细可以查看官方文档

    3.3 group_vars 组变量 和 host_vars 主机变量的定义

      Ansible在运行任何yml文件之前,都会去搜索与 hosts 文件同级目录下(也可以放在 和 playbook 的同级目录下)的两个用于保存变量文件(yaml格式)的目录 group_vars 和 host_vars,当 hosts 文件的同级目录和  playbook的同级目录中同时存在 变量目录 时,并且存在同名的变量时,会优先使用 playbook 同级目录中定
    义的变量
      组变量:对 hosts 中同一个组的所有主机均可调用
      主机变量:只允许某个主机进行调用
     
      组变量的定义方式,在和 hosts 文件同级目录中,定义一个和组名相同名称的 yaml 文件,如:test1.yaml
    var1: test1_var1  # 定义一个变量,名称为 var1,值为 test1_var1

      变量的调用方式,在  playbook  中

    ---
    - hosts: db
      remote_user: root

       tasks:
       - debug:
          msg: "{{var1}}"

     

      主机变量的定义和调用方式也是类似的,只是变量文件的文件名需要与主机名相同

    3.4 定义变量方式的优先级

    ansible变量优先级(由高到低)
    
    ansible-playbook命令中的变量,ansible-playbook -e var=value
    task变量
    block变量
    role中定义的变量和include变量
    set_fact
    registered变量
    vars_files
    var_prompt
    play变量
    host facts
    playbook中设置的host_vars
    playbook中设置的group_vars
    inventory中设置的host_vars
    inventory中设置的group_vars
    inventory变量
    role中defaults/main.yml中定义的变量
     
    3.5 ansible-playbook 的常用模块
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    第一阶段冲刺05
    生成器
    三元表达式 ,列表推导式 , 字典生成式
    装饰器
    闭包函数
    函数的定义和参数
    列表内置方法
    字符串的内置方法
    py_11_ 0731
    py_11_ 0730
  • 原文地址:https://www.cnblogs.com/lowmanisbusy/p/13194030.html
Copyright © 2011-2022 走看看