强制极限参数设置为ansible

时间:2018-10-24 16:13:19

标签: ansible

是否可以通过ansible-playbook选项强制执行命令ansible-variable--limit等(否则拒绝)?

我发现,在群集上,如果您错误地无限制地运行剧本,可以轻松地在所有节点上运行剧本,我想防止它被烦人的用户使用。

3 个答案:

答案 0 :(得分:2)

这与我最近解决的任务相反。我的目标是发现有--limit并跳过一些播放。

https://medium.com/opsops/how-to-detect-if-ansible-is-run-with-limit-5ddb8d3bd145

根据您的情况,您可以在剧本中检查此内容,如果它“完整播放”则失败:

- hosts: all
  gather_facts: no
  tasks:
    - set_fact:
         full_run: '{{play_hosts == groups.all}}'
    - fail:
        msg: 'Use --limit, Luke!'
      when: full_run

您当然可以使用其他组代替all(在hostsset_fact行中都进行更改)。

答案 1 :(得分:0)

使用ansible_limit变量(在ansible 2.5中添加)。您可以像这样进行测试:

tasks:
  - fail:
    msg: "you must use -l or --limit"
  when: ansible_limit is not defined
  run_once: true

答案 2 :(得分:0)

我在任务中就是这样做的:

$ cat exit-if-no-limit.yml
---
  - name: Verifying that a limit is set
    fail:
      msg: 'This playbook cannot be run with no limit'
    run_once: true
    when: ansible_limit is not defined

  - debug:
      msg: Limit is {{ ansible_limit }}, let's continue
    run_once: true
    when: ansible_limit is defined

当我需要禁止它们在所有主机上运行时,我将其包含在我的剧本中:

- include_role:
    name: myrole
    tasks_from: "{{ item }}.yml"
  loop:
  - exit-if-no-limit
  - something
  - something_else

在需要时易于重复使用。它是这样工作的:

TASK [myrole: Verifying that a limit is set] 
fatal: [ahost]: FAILED! => {"changed": false, "msg": "This playbook cannot be run with no limit"}

TASK [myrole: debug] 
ok: [anotherhost] => {
    "msg": "Limit is anotherhost, let's continue"
}
相关问题