如何在Ansible剧本中获取--limit参数的值? [英] How to get value of --limit argument inside an Ansible playbook?

查看:170
本文介绍了如何在Ansible剧本中获取--limit参数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆