Ansible-在同一主机(不同组)上使用覆盖变量(相同名称)启动多个自定义进程? [英] Ansible- start multiple custom processes with overriding variables (same name) on same host (different group)?

查看:19
本文介绍了Ansible-在同一主机(不同组)上使用覆盖变量(相同名称)启动多个自定义进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

So,我们有一个场景,我们需要能够在同一变量的各种可能值的组中的单个或多个主机上执行自定义命令.

So, We have a scenario, where we need the ability to execute a custom command on a single or multiple hosts from a group with various possible values of the same variable.

例如-

#Inventory:
[ServerGroup_1]
abc0001 node=node1
abc0002 node=node2

[ServerGroup_2]
abc0001 node=node3
abc0002 node=node4

[ServersGroups: children]
ServerGroup_1
ServerGroup_2

group_vars/ServerGroup_1
JAVA_HOME: /home/java
PORT: 9998

group_vars/ServerGroup_2
JAVA_HOME: /home/java
PORT: 9999

目标是在主机 abc0001 上执行以下 shell 命令,端口为 9998 和 9999,并在单个剧本运行中运行.

The goal is to execute below shell command on host abc0001 with Ports as 9998 and 9999 within a single playbook run.

shell: {{ JAVA_HOME }} -Dprocess.port={{ PORT }}

目前每次根据 Ansible 默认变量行为,它只在端口 9999 上执行.现在,作为替代,我们可以手动分离任务并在我们的剧本中调用它两次,如此处.

Currently every time as per Ansible default variable behavior it is only getting executed for port 9999. Now, as an alternative, we could manually separate out the tasks and call it twice inside our playbook as explained here.

但是,如果我们有 50 个不同的端口,编写起来会很乏味,而且我们希望配置能够从清单文件或变量文件中动态获取,以便添加任何新实例或运行不同端口上的命令,我们只需要将它添加到我们的清单/变量文件中,而不是编写一个单独的任务来覆盖端口.最终配置应该适用于在组的一台主机或组中的所有主机或特定主机和节点组合上运行该命令的所有可能场景......

But, if we have 50 different ports that would be tedious to write and also we would want the configuration in such a way that it dynamically picks up from either inventory file or variable files, so for adding any new instance or running the command on different port, we just have to add it to our inventory/variable files rather than writing a separate task covering the port. The end configuration should work for all possible scenarios of running that command on one host of a group or all hosts from a group or a particular host and node combination....

ansible-playbook -i staging test_multinode.yml --limit=ServersGroups -l abc0001

上面的 playbook 运行应该对 abc0001 上的 9998 和 9999 端口都执行 shell 命令,如果只是想说只为 abc0001 上的 9998 端口启动进程,那么 playbook 需要足够灵活.

The above playbook run should execute the shell command for both the ports 9998 and 9999 on abc0001 and the playbook needs to be flexible enough if just want to say start the process only for port 9998 on abc0001.

注意:我们已经通过在主机的清单文件中设置 Port 变量来尝试使用 with_items 块,但这种设置非常严格,不适用于其他场景.我们也试过ansible.cfg中的hash_behavior=merge和hash_behavior=replace设置,没发现有什么变化.

Note: We have tried the with_items block by setting a Port variable in inventory file for the host, but that set up is very rigid and will not work for other scenarios. We have also tried hash_behavior=merge and hash_behavior=replace settings in ansible.cfg, did not notice any change.

希望这是有道理的,我们没有把事情复杂化!请建议几个选项!!!

Hope this makes sense and We have not over-complicated things! Please suggest few options!!!

推荐答案

详细解析

简短回答 - 使用别名重写清单文件

Short Answer- Rewrite the inventory file using aliases

 #Inventory:
[ServerGroup_1]
#variable with name PORT on host abc0001 from group1
group1_node1 ansible_host=abc0001 PORT=9998
group1_node2 ansible_host=abc0002 PORT=9999

[ServerGroup_2]
#same variable name Port on the same host abc0001 present in a different group
group2_node1 ansible_host=abc0001 PORT=9998
group2_node2 ansible_host=abc0002 PORT=9999

[ServersGroups: children]
ServerGroup_1
ServerGroup_2

我们使用 group1_node1 作为别名,因此通过这样做 Ansible 会将 group1_node1 和 group2_node1 注册为两个不同的主机,即使它们是同一个主机 abc0001.

We are using group1_node1 as an alias, so by doing this Ansible will register group1_node1 and group2_node1 as two different hosts even though it’s the same host abc0001.

现在,我们将能够在同一主机 abc0001 上使用相同变量名 PORT 的不同参数启动两个进程.

Now, we will be able to start two processes on the same host abc0001 using different parameters for the same variable name PORT.

ansible-playbook -i staging test_multinode.yml --limit=ServersGroups -l group1_node1:group2_node1

希望这很清楚.

这篇关于Ansible-在同一主机(不同组)上使用覆盖变量(相同名称)启动多个自定义进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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