Ansible 嵌套循环,需要帮助
我想从用户那里获取输入,以逗号分隔的服务器组名称:变量目标中的 abc、cde,然后我想循环每个组的所有服务器并检查给定端口上的每个服务器访问权限。这里的 port 是我创建并存储在文件中的变量。我试图循环 port 变量中组的所有服务器,并为每个服务器检查 port 变量中该特定组下的所有端口。
vars 文件中的 port 变量如下所示:
port:
abc:
- server1
- server2
abc_port:
- 22
- 34
cde:
- server3
- server
cde_port:
- 122
- 343
我期望结果:
curl -kv server1:22
curl -kv server1:34
curl -kv server2:22
curl -kv server2:34
curl -kv server3:122
curl -kv server3:343
curl -kv server4:122
curl -kv server4:343
对于组 abc 和 cde 的服务器,根据用户输入的组,其形式为“abc ,cde”
我怎样才能实现这一点?
task:
- name: check port opening
shell: curl -kv {{item[1]}}:{{item[2]}}
with_nested:
- "{{destination.split(',')}}"
- "{{port.+item[0]}}"
- "{{port.+item[0]+_port}}
I want to take input from the user, group name of servers separated by a comma: abc, cde in variable destination, and then I want to loop over all the servers of each group and check for each server access on given ports. Here port is the variable that I created and stored in a file. I am trying to loop over all servers of the group in the port variable and for each of them check for all ports under that particular group in the port variable.
port variable in vars file looks like:
port:
abc:
- server1
- server2
abc_port:
- 22
- 34
cde:
- server3
- server
cde_port:
- 122
- 343
I expect the result:
curl -kv server1:22
curl -kv server1:34
curl -kv server2:22
curl -kv server2:34
curl -kv server3:122
curl -kv server3:343
curl -kv server4:122
curl -kv server4:343
For servers of group abc and cde as per the group input by user which will be of form "abc ,cde"
How can I achieve this?
task:
- name: check port opening
shell: curl -kv {{item[1]}}:{{item[2]}}
with_nested:
- "{{destination.split(',')}}"
- "{{port.+item[0]}}"
- "{{port.+item[0]+_port}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先创建目的地列表。如果您在命令行中声明dest,例如
-e dest='abc,cde'
给出
现在,您可以迭代命令
给出(删节)
Create the list of the destinations first. If you declare dest in the command line, e.g.
-e dest='abc,cde'
gives
Now, you can iterate the command
gives (abridged)