如果变量可以位于清单文件或group_vars目录中,如何从清单中获取变量的值?
例如,region=place-a可以在库存文件中或在group_vars某处的文件中。我希望命令能够使用 ansible 或其他方式检索该值来检索该值。喜欢:
$ ansible -i /somewhere/production/web --get-value region
place-a
Run Code Online (Sandbox Code Playgroud)
这将帮助我进行部署并了解要部署到哪个区域。
。
需要更长的解释来澄清,我的库存结构如下所示:
/somewhere/production/web
/somewhere/production/group_vars/web
Run Code Online (Sandbox Code Playgroud)
库存文件的变量内容/somewhere/production/web如下所示:
[web:children]
web1 ansible_ssh_host=10.0.0.1
web2 ansible_ssh_host=10.0.0.2
[web:vars]
region=place-a
Run Code Online (Sandbox Code Playgroud)
我可以通过简单地解析文件来从库存文件中获取值。像这样:
$ awk -F "=" '/^region/ {print $2}' /somewhere/production/web
place-a
Run Code Online (Sandbox Code Playgroud)
但该变量也可能位于group_vars文件中。例如:
$ cat /somewhere/production/group_vars/web
region: place-a
Run Code Online (Sandbox Code Playgroud)
或者它可能看起来像一个数组:
$ cat /somewhere/production/group_vars/web
region:
- place-a
Run Code Online (Sandbox Code Playgroud)
我不想查找并解析所有可能的文件。
Ansible 有办法获取这些值吗?有点像它是如何做的--list-hosts?
$ ansible web -i /somewhere/production/web --list-hosts
web1
web2
Run Code Online (Sandbox Code Playgroud) ansible ×1