zoukankan      html  css  js  c++  java
  • ansible使用指北(二)

    前言
    在上一篇文章里我们了解了ansible的常用模块,今天我们来了解下ansible-playbook,ansbile-playbook是一系统ansible命令的集合,其利用yaml 语言编写,ansbile-playbook命令根据自上而下的顺序依次执行。

    playbook通过ansible-playbook命令使用,它的参数和ansible命令类似,如参数-k(–ask-pass) 和 -K (–ask-sudo) 来询问ssh密码和sudo密码,-u指定用户,这些指令也可以通过规定的单元写在playbook 。
    ansible-playbook的简单使用方法: ansible-playbook play.yml

    ansible Example

    现在给出一个ansible-playbook示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    ---
    - hosts: webservers
    vars:
    http_port: 80
    max_clients: 200
    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
    notify:
    - restart apache
    - name: ensure apache is running (and enable it at boot)
    service: name=httpd state=started enabled=yes
    handlers:
    - name: restart apache
    service: name=httpd state=restarted

    hosts参数指定了对哪些主机进行操作
    vars参数指定了变量
    remote_user 则用于指定执行任务的用户
    tasks指定了一个任务

    • name参数是对任务的描述
    • yum参数是执行的任务
    • template参数指拷贝的模板
    • notify触发条件,指config有变化是重启服务
      handlers指task 执行完成以后需要调用的任务
      备注:在 notify 中定义内容一定要和tasks中定义的 - name 内容一样,这样才能达到触发的效果,否则会不生效

    从这个示例我们可以看出playbook的构成

    1
    2
    3
    4
    5
    playbooks组成:
    Target section: 定义要执行 playbook 主机组
    Variable section: 定义 playbook 运行使用的变量
    Task section: 定义执行的任务列表
    Handler section: 定义 task 执行完成以后需要调用的任务

    Include Statements

    • 普通的include:一个 task include file 由一个普通的 task 列表所组成,像这样:

      1
      2
      3
      4
      5
      6
      ---
      # possibly saved as tasks/foo.yml
      - name: placeholder foo
      command: /bin/foo
      - name: placeholder bar
      command: /bin/bar

      Include 指令看起来像下面这样,在一个 playbook 中,Include 指令可以跟普通的 task 混合在一起使用:

      1
      2
      tasks:
      - include: tasks/foo.yml
    • 参数化的 include
      如果我们要部署多个 wordpress 实例,我们可将所有的 wordpress task 写在一个 wordpress.yml 文件中, 然后像下面这样使用 wordpress.yml 文件

      1
      2
      3
      4
      tasks:
      - include: wordpress.yml wp_user=timmy
      - include: wordpress.yml wp_user=alice
      - include: wordpress.yml wp_user=bob

      备注:Ansible 1.4 及以后的版本,include 语法可更为精简,这种写法同样允许传递列表和字典参数:

      1
      2
      tasks:
      - { include: wordpress.yml, wp_user: timmy, ssh_keys: [ 'keys/one.txt', 'keys/two.txt' ] }
    • Include 语句也可用来将一个 playbook 文件导入另一个 playbook 文件。这种方式允许你定义一个 顶层的 playbook,这个顶层 playbook 由其他 playbook 所组成,先看一个例子吧

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      - name: this is a play at the top level of a file
      hosts: all
      remote_user: root
      tasks:
      - name: say hi
      tags: foo
      shell: echo "hi..."
      - include: load_balancers.yml
      - include: webservers.yml
      - include: dbservers.yml

    Playbook Roles

    • Roles 基于一个已知的文件结构,去自动的加载某些 vars_files,tasks 以及 handlers。基于 roles 对内容进行分组,使得我们可以容易地与其他用户分享 roles

      Ansible主要是通过一个inventory来定义role和主机之间的匹配,通过一个ini风格的配置文件来管理所有的主机,通过一个
      group_vars下与主机组同名的文件来管理变量,或者host_vars下与主机同名的文件来管理变量(和pillar类似),然后按照固定的目录
      结构在角色名目录下创建好files, handlers, tasks, templates,
      vars(角色级别的变量)等目录;最后通过ansible命令再跟一系列的参数指定好inventory, playbooks,
      user等来触发对所有主机的配置
      项目的结构如下

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      site.yml
      webservers.yml
      fooservers.yml
      roles/
      common/
      files/
      templates/
      tasks/
      handlers/
      vars/
      defaults/
      meta/
      webservers/
      files/
      templates/
      tasks/
      handlers/
      vars/
      defaults/
      meta/

      playbook 如下

      1
      2
      3
      4
      5
      ---
      - hosts: webservers
      roles:
      - common
      - webservers

      这个 playbook 为一个角色 ‘x’ 指定了如下的行为

      • 如果 roles/x/tasks/main.yml 存在, 其中列出的 tasks 将被添加到 play 中
      • 如果 roles/x/handlers/main.yml 存在, 其中列出的 handlers 将被添加到 play 中
      • 如果 roles/x/vars/main.yml 存在, 其中列出的 variables 将被添加到 play 中
      • 如果 roles/x/meta/main.yml 存在, 其中列出的 “角色依赖” 将被添加到 roles 列表中 (1.3 and later)
      • 所有 copy tasks 可以引用 roles/x/files/ 中的文件,不需要指明文件的路径。
      • 所有 script tasks 可以引用 roles/x/files/ 中的脚本,不需要指明文件的路径。
      • 所有 template tasks 可以引用 roles/x/templates/ 中的文件,不需要指明文件的路径。
      • 所有 include tasks 可以引用 roles/x/tasks/ 中的文件,不需要指明文件的路径。

        如果 roles 目录下有文件不存在,这些文件将被忽略。比如 roles 目录下面缺少了 ‘vars/’ 目录,这也没关系。
        注意:你仍然可以在 playbook 中松散地列出 tasks,vars_files 以及 handlers,这种方式仍然可用,但 roles 是一种很好的具有组织性的功能特性,我们强烈建议使用它。如果你在 playbook 中同时使用 roles 和 tasks,vars_files 或者 handlers,roles 将优先执行

      也可以使用参数化的 roles,这种方式通过添加变量来实现,比如

      1
      2
      3
      4
      5
      6
      ---
      - hosts: webservers
      roles:
      - common
      - { role: foo_app_instance, dir: '/opt/a', port: 5000 }
      - { role: foo_app_instance, dir: '/opt/b', port: 5001 }

      也可以为 roles 设置触发条件,像这样:

      1
      2
      大专栏  ansible使用指北(二)"line">3
      4
      ---
      - hosts: webservers
      roles:
      - { role: some_role, when: "ansible_os_family == 'RedHat'" }

      它的工作方式是:将条件子句应用到 role 中的每一个 task 上。关于”条件子句”的讨论参见本文档后面的章节。

      最后,你可能希望给 roles 分配指定的 tags。比如:

      1
      2
      3
      4
      ---
      - hosts: webservers
      roles:
      - { role: foo, tags: ["bar", "baz"] }

      如果 play 仍然包含有 ‘tasks’ section,这些 tasks 将在所有 roles 应用完成之后才被执行。
      如果你希望定义一些 tasks,让它们在 roles 之前以及之后执行,你可以这样做:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      ---
      - hosts: webservers
      pre_tasks:
      - shell: echo 'hello'
      roles:
      - { role: some_role }
      tasks:
      - shell: echo 'still busy'
      post_tasks:
      - shell: echo 'goodbye'

      备注:如果对 tasks 应用了 tags,需确保给 pre_tasks 以及 post_tasks 也同样应用 tags,并且将它们一并传递。特别是当 pre_tasks 和 post_tasks 被用来监视 “停止窗口控制” 或者 “负载均衡” 时要确保这样做

    • 角色默认变量(Role Default Variables)
      角色默认变量允许你为 included roles 或者 dependent roles(见下) 设置默认变量。要创建默认变量,只需在 roles 目录下添加 defaults/main.yml 文件。这些变量在所有可用变量中拥有最低优先级,可能被其他地方定义的变量(包括 inventory 中的变量)所覆盖

    • 角色依赖(Role Dependencies)
      “角色依赖” 使你可以自动地将其他 roles 拉取到现在使用的 role 中。”角色依赖” 保存在 roles 目录下的 meta/main.yml 文件中。这个文件应包含一列 roles 和 为之指定的参数,下面是在 roles/myapp/meta/main.yml 文件中的示例

      1
      2
      3
      4
      5
      ---
      dependencies:
      - { role: common, some_parameter: 3 }
      - { role: apache, port: 80 }
      - { role: postgres, dbname: blarg, other_parameter: 12 }
    • 实战nginx

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      # mkdir -pv roles/nginx/{tasks,files,templates,handlers,vars,meta,default}
      # ansible all -m setup | grep ansible_processor_cores
      "ansible_processor_cores": 1, 获取ansible的要调用的相关函数
      # cd roles/nginx/templates/ 模板文件一定要放到此目录
      vim nginx.conf
      worker_processes {{ ansible_processor_cores }}; 调用获取到的函数

      # ls -l roles/nginx/files/
      -rw-r--r--. 1 root root 1290 Nov 12 2014 default.conf
      -rw-r--r--. 1 root root 319456 Mar 29 20:44 nginx-1.4.7-1.el6.ngx.x86_64.rpm

      #cd roles/nginx/tasks/
      [root@localhost tasks]# vim main.yml
      - name: copy nginx.rpm
      copy: src=nginx-1.4.7-1.el6.ngx.x86_64.rpm dest=/tmp/nginx-1.4.7-1.el6.ngx.x86_64.rpm
      - name: install nginx
      shell: yum -y install /tmp/nginx-1.4.7-1.el6.ngx.x86_64.rpm
      - name: provides nginx.conf
      template: src=nginx.conf dest=/etc/nginx/nginx.conf
      tags: nginxconf
      notify:
      - server restart
      - name: provides default.conf
      copy: src=default.conf dest=/etc/nginx/conf.d/default.conf
      tags: nginxconf
      - name: server start
      service: name=nginx enabled=true state=started

      [root@localhost playbook]# cd roles/nginx/handlers/
      [root@localhost handlers]# vim main.yml
      - name: server restart
      service: name=nginx state=restarted

      [root@localhost playbook]# cat site.yml
      - hosts: nginx
      remote_user: root
      roles:
      - nginx

      [root@localhost ]# ansible-playbook site.yml

      [root@localhost playbook]# tree roles/
      roles/
      └── nginx
      ├── default
      ├── files
      │ ├── default.conf
      │ └── nginx-1.4.7-1.el6.ngx.x86_64.rpm
      ├── handlers
      │ └── main.yml
      ├── meta
      ├── tasks
      │ └── main.yml
      ├── templates
      │ └── nginx.conf
      └── vars
      `

    ansible Conditionals

    • Loops and Conditionals

      1
      2
      3
      4
      5
      6
      7
      8
      9
      #use list
      tasks:
      - command: echo {{ item }}
      with_items: [ 0, 2, 4, 6, 8, 10 ]
      when: item > 5
      #use dict
      - command: echo {{ item.key }}
      with_dict: "{{ mydict|default({}) }}"
      when: item.value > 5
    • The When Statement

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      #group conditions(a logical 'or')
      tasks:
      - name: "shut down CentOS 6 and Debian 7 systems"
      command: /sbin/shutdown -t now
      when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
      (ansible_distribution == "Debian" and ansible_distribution_major_version == "7")

      #multiple(a logical 'and')
      tasks:
      - name: "shut down CentOS 6 systems"
      command: /sbin/shutdown -t now
      when:
      - ansible_distribution == "CentOS"
      - ansible_distribution_major_version == "6"

      备注: example requires the lsb_release package on the target host in order to return the ansible_lsb.major_release fact

    ref

    Docs-Conditionals


    您的鼓励是我写作最大的动力

    俗话说,投资效率是最好的投资。 如果您感觉我的文章质量不错,读后收获很大,预计能为您提高 10% 的工作效率,不妨小额捐助我一下,让我有动力继续写出更多好文章。

  • 相关阅读:
    maven的安装教程
    webstorm的中文教程和技巧分享
    WebStorm
    grunt配置任务
    grunt快速入门
    CSS简介
    浅介HTML DOM
    【转】计算机是如何启动的?
    【转】深入理解C++中public、protected及private用法
    【转】VS2013动态库文件的创建及其使用详解
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12268155.html
Copyright © 2011-2022 走看看