如何在 Ansible 剧本中获取 --limit 参数的值? [英] How to get value of --limit argument inside an Ansible playbook?
问题描述
在 ansible 中,是否可以在剧本中获取--limit"选项的参数值?我想做的是这样的:
In ansible, is it possible to get the value of the argument to the "--limit" option within a playbook? I want to do is something like this:
---
- hosts: all
remote user: root
tasks:
- name: The value of the --limit argument
debug:
msg: "argument of --limit is {{ ansible-limit-arg }}"
然后当我运行他的命令时:
Then when I run he command:
$ ansible-playbook getLimitArg.yaml --limit webhosts
我会得到这个输出:
argument of --limit is webhost
当然,我编造了变量ansible-limit-arg"的名称,但是有没有有效的方法来做到这一点?我可以指定webhosts"两次,第二次使用--extra-args,但这似乎是一种必须这样做的迂回方式.
Of course, I made up the name of the variable "ansible-limit-arg", but is there a valid way of doing this? I could specify "webhosts" twice, the second time with --extra-args, but that seems a roundabout way of having to do this.
推荐答案
从 Ansible 2.5 开始,您可以使用新的魔法变量 ansible_limit
访问该值,因此:
Since Ansible 2.5 you can access the value using a new magic variable ansible_limit
, so:
- debug:
var: ansible_limit
这篇关于如何在 Ansible 剧本中获取 --limit 参数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!