只允许某些 Ansible 主机/组使用 `--check` 和/或 `--diff`

问题描述 投票:0回答:1

我想将 Ansible 部署操作限制为仅对某些主机/组进行dry-run(即

--check
和/或
--diff
)。

我能想到的最好的办法是检查每本剧本,如果部署发生在这样的

空运行
机器上,可能作为pre_task。如果是这样,请检查是否设置了
ansible_check_mode
,如果没有退出并显示消息,否则继续。这就是一个不错的方法。

对于我错过的 Ansible args,是否有类似于

ansible_ssh_extra_args
的东西?

至少在Connecting to hosts: behavioral inventory parameters的文档中没有提到任何内容。

还有其他选择吗?

ansible ansible-inventory
1个回答
0
投票

因为variable precedence似乎不可能基于

ansible_check_mode
true
或其他强制
group_vars
set_fact
,除了在任务级别执行

这将采用已经提到的方法

---
- 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'

当必须指定

--limit
.

更多文档

© www.soinside.com 2019 - 2024. All rights reserved.