ansible常用模块之 -- meta模块 – 执行Ansible的“actions”

  • meta模块 – 执行Ansible的“actions”
  • 一、摘要
  • 二、参数
  • 三、示例


meta模块 – 执行Ansible的“actions”

一、摘要

  • 元任务是一种特殊的任务,它可以影响Ansible的内部执行或状态。
  • 元任务可以在剧本的任何地方使用。
  • Windows目标也支持此模块。

二、参数

参数

选项/默认值

描述

free_form

required

· clear_facts

· clear_host_errors

· end_host

· end_play

· flush_handlers

· noop

· refresh_inventory

· reset_connection

这个模块接受一个free_form的命令,作为字符串。实际上并没有一个叫做“free_form”的选项。参见示例!

flush_handlers使Ansible运行任何已经被通知的处理程序任务。Ansible在某些点内部插入这些任务,隐式触发处理程序运行(在pre/post任务之后,最终角色执行之后,以及play的主要任务部分)。

refresh_inventory(在Ansible 2.0中添加)强制重新加载库存,这在动态库存脚本的情况下意味着它们将被重新执行。如果动态库存脚本使用缓存,Ansible无法知道这一点,也没有办法刷新它(你可以禁用缓存,或者,如果可用的特定库存数据源(例如aws),你可以使用库存插件而不是库存脚本)。当创建了额外的主机并且用户希望使用它们而不是使用add_host模块时,这主要是有用的。

noop (Ansible 2.0中添加的)这实际上“什么都不做”。本品主要内用,不建议一般使用。

clear_facts(在Ansible 2.1中添加的)导致在剧的主机列表中指定的主机收集的事实被清除,包括事实缓存。

clear_host_errors(在Ansible 2.1中添加)从播放的主机列表中指定的主机中清除失败状态(如果有的话)。

end_play(在Ansible 2.2中添加)导致播放结束,而不会使主机失败。注意,这将影响所有主机。

reset_connection (Ansible 2.3中添加的)中断一个持久连接(即ssh + control persist)

end_host(在Ansible 2.8中添加)是end_play的逐主机变体。导致当前主机的播放结束,而不会使其失败。

三、示例

# 示例显示按需刷新处理程序,而不是在play结束时
- template:
    src: new.j2
    dest: /etc/config.txt
  notify: myhandler

- name: 强制所有已通知的处理程序在此时运行,而不是等待正常的同步点
  meta: flush_handlers

# 演示如何在play中刷新库存
- name: Reload inventory, useful with dynamic inventories when play makes changes to the existing hosts
  cloud_guest:            # this is fake module
    name: newhost
    state: present

- name: 刷新库存以确保库存中存在新实例
  meta: refresh_inventory

# 示例显示如何清除目标主机的所有现有事实
- name: Clear gathered facts from all currently targeted hosts
  meta: clear_facts

# 显示如何继续使用失败的目标的示例
- name: Bring host back to play after failure
  copy:
    src: file
    dest: /etc/file
  remote_user: imightnothavepermission

- meta: clear_host_errors

# 显示如何重置现有连接的示例
- user:
    name: '{{ ansible_user }}'
    groups: input

- name: Reset ssh connection to allow user changes to affect 'current login user'
  meta: reset_connection

# 演示如何为特定目标结束play的例子
- name: End the play for hosts that run CentOS 6
  meta: end_host
  when:
  - ansible_distribution == 'CentOS'
  - ansible_distribution_major_version == '6'

官方文档:https://docs.ansible.com/ansible/2.9/modules/meta_module.html#meta-module