Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ansible中枚举主机_Ansible - Fatal编程技术网

在ansible中枚举主机

在ansible中枚举主机,ansible,Ansible,在使用Ansible在Linux服务器上安装工具的过程中,我想在集群内自定义一个具有唯一编号的配置文件 设想一个3节点集群 [mycluster] machine07 machine08 machine09 配置只是一个根据机器定制的模板文件: - template: src: admin.json.j2 dest: /home/my_user/tool/mytool/admin.json 此模板包含以下动态部分: ... "contextPath": "/web-inter

在使用Ansible在Linux服务器上安装工具的过程中,我想在集群内自定义一个具有唯一编号的配置文件

设想一个3节点集群

[mycluster]
machine07
machine08
machine09
配置只是一个根据机器定制的模板文件:

- template:
    src: admin.json.j2
    dest: /home/my_user/tool/mytool/admin.json
此模板包含以下动态部分:

...
"contextPath": "/web-interface-{{id_number}}",
...
我想找到一种方法,能够正确地自定义此动态部件,如下所示:

在机器07上:

...
"contextPath": "/web-interface-1",
...
...
"contextPath": "/web-interface-1",
...
在机器上108

...
"contextPath": "/web-interface-2",
...
...
"contextPath": "/web-interface-2",
...
在机器上09

...
"contextPath": "/web-interface-3",
...
...
"contextPath": "/web-interface-3",
...
我怎么能做到

我尝试过使用shell脚本,我目前正在努力使用索引,但没有多少成功。如果可能的话,我不想为每个节点创建一个清单文件,你知道我如何实现它吗


谢谢你,我想可以有多种方法。我想到的第一个方法是对组使用索引。我试过这个简单的剧本:

---
- name: test enumerate
  hosts: all
  gather_facts: no
  tasks:
    - debug:
        msg: "host-{{ groups['mycluster'].index(inventory_hostname) }}"
根据您的库存,它提供:

TASK [debug] *********
ok: [machine07] => {
    "msg": "host-0"
}
ok: [machine08] => {
    "msg": "host-1"
}
ok: [machine09] => {
    "msg": "host-2"
}
因此,您可以在模板中尝试以下操作:

"contextPath": "/web-interface-{{ groups['mycluster'].index(inventory_hostname) }}",
第二种方法是使用主机变量。例如,库存将是:

[mycluster]
machine07 num=1
machine08 num=2
machine09 num=3
在模板中,您将使用:

"/web-interface-{{ hostvars[inventory_hostname].num }}"

这给出了与上面相同的输出。

我使用了第一个Rolf的想法,但做了一些改动

在将模板下载到远程主机之前,我设置了一个带有所需数字的事实

- set_fact:
   id_number: "{{ansible_play_hosts.index(inventory_hostname) + 1 }}"

- template:
    src: admin.json.j2
    dest: /home/my_user/tool/mytool/admin.json
在模板中

...
"contextPath": "/web-interface-{{id_number}}",
...
所以最后我们得到一个不同的值,这取决于主机对索引号+1的赋值

在机器07上:

...
"contextPath": "/web-interface-1",
...
...
"contextPath": "/web-interface-1",
...
在机器上108

...
"contextPath": "/web-interface-2",
...
...
"contextPath": "/web-interface-2",
...
在机器上09

...
"contextPath": "/web-interface-3",
...
...
"contextPath": "/web-interface-3",
...

谢谢大家!

哦,谢谢你!我从来都不喜欢定义只与一个主机链接的变量。所以我使用了你的第一个想法,但为了做我需要的事情,我做了一些修改,请看我下面的评论。我不认为顺序是有保证的,所以从一次执行到另一次执行,你可能不总是有相同的索引。不过,对于您的用例来说,这可能不是问题。但在cas中,这是一个问题,或者如果您需要其他类型的值,您可能需要一个hostvar。它很容易维护,也没有魔法,所以我认为它很干净,但在你的情况下,这取决于你的喜好!没错,如果将多个组进行中间/并集,则无法确定其相交方式,但当您仅针对一个组时,该组被理解为标准数组,并将按照主机文件中列出的顺序对主机进行索引。如果我必须针对几个群体,我肯定会使用你的第二个案例。