使用模式在Ansible清单文件中填充主机属性 [英] Using patterns to populate host properties in Ansible inventory file

查看:87
本文介绍了使用模式在Ansible清单文件中填充主机属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像的主机文件

[foo]
foox 192.168.0.1 id=1
fooy 192.168.0.1 id=2
fooz 192.168.0.1 id=3

但是,我想使用以下模式更简洁地编写此代码:

[foo]
foo[x:z] 192.168.0.1 id=[1:3]

但是这被解释为id等于"[1:3]"的原始文本,而不是1、2或3.是否有办法在清单文件中实现此目标,或者我需要做是通过主机变量和/或组变量来完成的?

解决方案

这无法在清单文件中完成.我认为set_fact是最好的选择,以这种方式以编程方式建立广告资源.

---
- hosts: all
  tasks:
    - add_host:
        name: "host{{ item }}"
        ansible_ssh_host: "127.0.0.1"
        ansible_connection: "local"
        group: "new"
        id: "{{ item }}"
      with_sequence: count=3
      delegate_to: localhost
      run_once: yes
- hosts: new
  tasks:
    - debug:
        msg: "{{ id }}"

如果我没记错的话,Jinja功能已从不该使用的所有位置删除,例如,外引号,花括号以及YML文件中when:之类的特殊情况.

不过,当我以编程方式发言时,我们正在谈论的是Ansible ..,这是世界上通用脚本编写的最后候选人之一.除非我们确切地说是三台服务器,否则动态清单脚本是解决此类问题的更好方法.

最简单的清单脚本可以做到这一点(在您的hosts目录中,或由-i开关指向:

#!/usr/bin/env python
import json
inv = {}
for i in range(3):
  inv[i] = {"hosts":["host%s" % i],"vars":{"id":i,"ansible_ssh_host":"127.0.0.1", "ansible_connection":"local"}}
print json.dumps(inv)

再次,恐怕没有什么比您想要的漂亮".如果您的用例变得越来越复杂,则set_factset_hostgroup_by可能会派上用场,或者是清单脚本或group_vars(我目前确实使用group_vars文件作为服务器编号).

I have a host file that looks like

[foo]
foox 192.168.0.1 id=1
fooy 192.168.0.1 id=2
fooz 192.168.0.1 id=3

However, I'd like to more concisely write this using patterns like:

[foo]
foo[x:z] 192.168.0.1 id=[1:3]

But this is getting interpreted as id equaling the raw text of "[1:3]", rather than 1, 2, or 3. Is there a way to achieve this in the inventory file, or will I need to do something through host vars and/or group vars?

解决方案

This can't be done within an inventory file. I think set_fact is your best bet to programmatically build an inventory this simple.

---
- hosts: all
  tasks:
    - add_host:
        name: "host{{ item }}"
        ansible_ssh_host: "127.0.0.1"
        ansible_connection: "local"
        group: "new"
        id: "{{ item }}"
      with_sequence: count=3
      delegate_to: localhost
      run_once: yes
- hosts: new
  tasks:
    - debug:
        msg: "{{ id }}"

If I recall correctly, Jinja capabilities have been removed from every place they shouldn't have been, i.e. outside quotes, braces, special cases like when: in YML files.

When I say programmatically, though, we're talking about Ansible.. one of the last candidates on earth for general purpose scripting. Dynamic inventory scripts are a better approach to problems like these, unless we're talking three servers exactly.

The simplest inventory script to accomplish this would be (in your hosts dir or pointed to by the -i switch:

#!/usr/bin/env python
import json
inv = {}
for i in range(3):
  inv[i] = {"hosts":["host%s" % i],"vars":{"id":i,"ansible_ssh_host":"127.0.0.1", "ansible_connection":"local"}}
print json.dumps(inv)

Again, I'm afraid there is nothing as "pretty" as what you're looking for. If your use case grows more complex, then set_fact, set_host and group_by may come in handy, or an inventory script, or group_vars (I do currently use group_vars files for server number).

这篇关于使用模式在Ansible清单文件中填充主机属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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