强制极限参数设置为ansible

Wal*_*ldo 4 ansible

有没有办法强制命令ansible-playbookansible-variable等...要与执行的--limit选项(否则拒绝它)?

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

Vic*_*man 5

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

tasks:
  - fail:
      msg: "you must use -l or --limit"
    when: ansible_limit is not defined
    run_once: true
Run Code Online (Sandbox Code Playgroud)


Geo*_*lin 1

这与我最近解决的任务相反。我的目标是检测到--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
Run Code Online (Sandbox Code Playgroud)

all当然,您可以使用不同的组来代替(在hostsset_fact行中更改它)。