【问题标题】:Only allow `--check` and / or `--diff` for certain ansible hosts / groups只允许某些 ansible 主机/组使用 `--check` 和/或 `--diff`
【发布时间】:2023-02-23 04:08:16
【问题描述】:

我想将 ansible 部署操作限制为空运行仅(即--check 和/或--diff)某些主机/组。

我能想到的最好办法是检查每个剧本,如果部署发生在这样的情况下,可能作为pre_task仅试运行机器。如果是这样,请检查是否设置了ansible_check_mode,如果未设置则退出并显示消息,否则继续。这只是一个不错的方法。

是否有类似于ansible_ssh_extra_args 的东西用于我错过的 ansible args? 至少https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#connecting-to-hosts-behavioral-inventory-parameters 的文档中没有提到任何内容。

还有其他选择吗?

【问题讨论】:

  • 根据 ”我能想到的最好的...“ 看来您已经实施了一些似乎也有效的东西。您介意在这里分享它吗?通过这样做,可能会根据要求提供当前未知的替代方案。

标签: ansible ansible-inventory


【解决方案1】:

由于variable precedence,似乎不可能基于group_varsset_fact或其他强制ansible_check_modetrue,除了[在任务级别强制执行](在任务上强制或阻止检查模式)。

这将与已经提到的方法之一

---
- hosts: test
  become: false
  gather_facts: false

  pre_tasks:

  - name: Check Mode
    fail:
      msg: The system may not be provisioned according to the CMDB status.
    when: not ansible_check_mode and 'test' in group_names

  tasks:

  - debug:
      var: ansible_check_mode

并导致输出

TASK [Check Mode] ****************************************************
fatal: [test.example.com]: FAILED! => changed=false
  msg: The system may not be provisioned according to the CMDB status.

或者当使用 --check 运行时

TASK [debug] *********************************************************
ok: [test.example.com] =>
  ansible_check_mode: true

这也是在某些用户帐户下普遍阻止执行的可行方法。

  pre_tasks:

  - name: Check Ansible User
    fail:
      msg: Do not execute under root!
    when: ansible_user == 'root'

进一步的文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    相关资源
    最近更新 更多