ansible高级使用知识点

ansible功能重用和高级特性

Handlers

·Handlers只有在其所在的任务被执行时,才会被运行;如果一个任务中定义了notify调用Handlers,但是由于条件判断等原因,该任务未被执行,那么Handlers同样不会被执行。

·Handlers只会在Play的末尾运行一次;如果想在一个Playbook的中间运行Handlers,则需要使用meta模块来实现,例如:-meta:flush_handlers。

·如果一个Play在运行到调用Handlers的语句之前失败了,那么这个Handlers将不会被执行。我们可以使用mega模块的--force-handlers选项来强制执行Handlers,即使是Handlers所在的Play中途运行失败也能执行。

handlers:

  • name: restart apache
    service: name=apache2 state=restarted
    notify: restart memcached
  • name: restart memcached
    service: name=memcached state=restarted

Facts

在运行任何一个Playbook之前,Ansible默认会先抓取Playbook中所指定的所有主机的系统信息,这些信息我们称之为Facts。

ansible munin -m setup

设置gather_facts:no来暂时让Ansible在执行Playbook任务之前跳过收集远程主机Facts信息这一步,这样可以为任务节省几秒钟的时间

交互式提示

---

  • hosts: all
    vars_prompt:
    • name: share_user
      prompt: "What is your network username?"
    • name: share_pass
      prompt: "What is your network password?"
      private: yes

Block

---

  • hosts: web
    tasks:

    Install and configure Apache on RedHat/CentOS hosts.

    • block:
      • yum: name=httpd state=present
      • template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
      • service: name=httpd state=started enabled=yes
        when: ansible_os_family == 'RedHat'
        sudo: yes

    Install and configure Apache on Debian/Ubuntu hosts.

    • block:
      • apt: name=apache2 state=present
      • template: src=httpd.conf.j2 dest=/etc/apache2/apache2.conf
      • service: name=apache2 state=started enabled=yes
        when: ansible_os_family == 'Debian'
        sudo: yes

块功能非常适合于多个任务共用同一套任务参数的情况

control node

控制端版本

ansible-core 2.11 and Ansible 4.0.0

make Python 3.8 a soft dependency

ansible-core 2.12 and Ansible 5.0.0

will require Python 3.8 or newer

Starting with ansible-core 2.11, the project will only be packaged for Python 3.8 and newer.

版本

  • 从2.10版本开始
    community package called ansible
    minimalist language and runtime called ansible-core (called ansible-base in version 2.10)

Includes

- include: ./static_git_pull.yml

Includes引用方式的好处不言而喻:简洁,干净,解耦,复用度高,易于维护

- include: ./static_git_pull.yml

when
动态引用

Roles

相对Includes功能,Roles更适合于大项目Playbook的编排架构

Roles不仅支持Tasks的集合,同时包括vars_files、tasks、handlers、meta、templates

Roles主要依赖于目录的命名和摆放,默认tasks/main.yml是所有任务的入口

每个目录下均由main.yml定义该功能的任务集

引用role的方式


  • hosts: all
    roles:
    • role_name

·roles/x/tasks/main.yml:主函数,包括在其中的所有任务将被执行。

·roles/x/handlers/main.yml:所有包括其中的handlers将被执行。
·roles/x/vars/main.yml:所有包括在其中的变量将在roles中生效。
·roles/x/meta/main.yml:roles所有依赖将被正常登入。
·roles/x/{files,templates,tasks}/(dir depends on task):所有文件、模板都可存放在这里,放在这里最大的好处是不用指定绝对路径。

Jinja2

  • 变量的提取使用{{variable}},{%statement execution%}括起来的内容为Jinja2命令执行语句
  • bind-address=0.0.0.0:{{ PORT | default(3306) }}

Galaxy

  • collections
  • Galaxy provides pre-packaged units of work such as roles, and new in Galaxy 3.2, collections You can find roles for provisioning infrastructure, deploying applications, and all of the tasks you do everyday. The collection format provides a comprehensive package of automation that may include multiple playbooks, roles, modules, and plugins.

XMind: ZEN - Trial Version

posted @ 2021-04-06 09:56  2012  阅读(579)  评论(0编辑  收藏  举报