编辑10OCT2017感谢@techraf使用jinja2模板 我有一个让我疯狂的安心任务(顺便说一下,我只有1天的Ansible经验,所以我的测试/思维方式可能会出错)。我试图将对象转换为不同的对象。我的原始对象是这样的:
[
{"allowed":[{"IPProtocol":"tcp","ports":["1234-1235"]},{"IPProtocol":"udp","ports":["1238-1239"]}],
"description":"hello1",
"name":"hello1",
"sourceRanges":["128.0.0.0/16","192.0.0.0/16"]},
{"allowed":[{"IPProtocol":"tcp","ports":["2345-2346"]},{"IPProtocol":"udp","ports":["4567-4578"]}],
"description":"hello2",
"name":"hello2",
"sourceRanges":["128.0.0.0/16","192.0.0.0/16"]}
]
我希望将其转换为:
[
{"allowed":"tcp:1234-1235,udp:1238-1239",
"description":"hello1",
"name":"hello1",
"sourceRanges":"128.0.0.0/16,192.0.0.0/16"},
{"allowed":"tcp:2345-2346,udp:4567-4578",
"description":"hello2",
"name":"hello2",
"sourceRanges":"128.0.0.0/16,192.0.0.0/16"}
]
我扁平化协议/端口和源范围。
我尝试使用jinja2模板和以下ansible playbook:
#ansible-playbook issue.yml -i 'local,' --connection=local
- hosts:
- local tasks:
- name: setVar
set_fact:
aOriginal='[{"allowed":[{"IPProtocol":"tcp","ports":["1234-1235"]},{"IPProtocol":"udp","ports":["1238-1239"]}],"description":"hello1","name":"hello1","sourceRanges":["128.0.0.0/16","192.0.0.0/16"]},{"allowed":[{"IPProtocol":"tcp","ports":["2345-2346"]},{"IPProtocol":"udp","ports":["4567-4578"]}],"description":"hello2","name":"hello2","sourceRanges":["128.0.0.0/16","192.0.0.0/16"]}]'
- debug:
var: aOriginal
- name: Populate SubnetIds
set_fact:
test3: "{{ lookup('template', 'subnet2.j2') }}"
vars:
rules: "{{ aOriginal }}"
- debug:
var: test3
- name: Create rules
shell: gclofud compute firewall-rules create {{ item.name }} --allow {{ item.altIpProtos }} --description {{ item.description }} --source-ranges {{ item.flatSrcRanges }}
with_items: "{{ test1 }}"
使用我的模板文件(相同的文件夹 - 文件名为“subnet2.j2”):
[ { {% for s in rules %}name:{{s.name}},description:{{s.description}},allowed:{% for aOneAllowedProtoPort in s.allowed %}{{aOneAllowedProtoPort.IPProtocol}}:{% for aOneAllowedPort in aOneAllowedProtoPort.ports %}{{aOneAllowedPort}}{% endfor %}{% if not loop.last %},{% endif %}{% endfor %},sourceRanges:"{% for aOneAllowedSource in s.sourceRanges %}{{aOneAllowedSource}}{% if not loop.last %},{% endif %}{% endfor %}{% endfor %} } ]
正如您在下面的test3结果中看到的那样:
ok: [local] => {
"test3": "[\"name\":hello1,description:hello1,allowed:tcp:1234-1235,udp:1238-1239,sourceRanges:128.0.0.0/16,192.0.0.0/16\"name\":hello2,description:hello2,allowed:tcp:2345-2346,udp:4567-4578,sourceRanges:128.0.0.0/16,192.0.0.0/16]\n"
}
我设法将protocole / ports和范围列表组合在一起然而似乎模板的输出被解释为字符串并注释为对象。我尝试了几个测试(将字符串放在字符串旁边,因为ansible为它们添加前缀),我不知道发生了什么。
我想知道是否有特定的事情可以将模板的结果转换为对象。仍在努力,并将根据我的进展更新问题。 Thx @techraf
最终更新。我做到了。这是ansible playbook(里面的jinja2模板):
#ansible-playbook issue.yml -i 'local,' --connection=local
- hosts:
- local
tasks:
- name: setVar
set_fact:
aOriginal='[{"allowed":[{"IPProtocol":"tcp","ports":["1234-1235"]},{"IPProtocol":"udp","ports":["1238-1239"]}],"description":"hello1","name":"hello1","sourceRanges":["128.0.0.0/16","192.0.0.0/16"]},{"allowed":[{"IPProtocol":"tcp","ports":["2345-2346"]},{"IPProtocol":"udp","ports":["4567-4578"]}],"description":"hello2","name":"hello2","sourceRanges":["128.0.0.0/16","192.0.0.0/16"]}]'
- debug:
var: aOriginal
- name: Populate SubnetIds
set_fact:
test3='[ {% for s in rules %} {"name":"{{s.name}}","description":"{{s.description}}","allowed":"{% for aOneAllowedProtoPort in s.allowed %}{{aOneAllowedProtoPort.IPProtocol}}:{% for aOneAllowedPort in aOneAllowedProtoPort.ports %}{{aOneAllowedPort}}{% endfor %}{% if not loop.last %},{% endif %}{% endfor %}","sourceRanges":"{% for aOneAllowedSource in s.sourceRanges %}{{aOneAllowedSource}}{% if not loop.last %},{% endif %}{% endfor %}"}{% if not loop.last %},{% endif %} {% endfor %} ]'
vars:
rules: "{{ aOriginal }}"
- debug:
var: test3
- name: Create rules
shell: gclofud compute firewall-rules create {{ item.name }} --allow {{ item.altIpProtos }} --description {{ item.description }} --source-ranges {{ item.flatSrcRanges }}
with_items: "{{ test3 }}"
答案 0 :(得分:0)
由于@techraf关于jinja2模板的提示,我找到了解决方案。
我只是在这里分享结果,以防将来帮助某人:
<div class="panel panel-default">
<div class="panel-heading">
</div>
<div class="panel-body">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-event" class="form-horizontal">
<div class="col-sm-4">
<label>name</label>
<input type="text" name="name" value="name" placeholder="name" id="name" class="form-control" />
</div>
<div class="col-sm-4">
<label>address</label>
<input type="text" name="address" value="address" placeholder="address" id="address" class="form-control" />
</div>
<div class="col-sm-4">
<label>phone</label>
<input type="text" name="phone" value="phone" placeholder="phone" id="phone" class="form-control" />
<div class="text-danger"></div>
</div>
</div>
<div class="row">
<div class="add_component">
<button id='launch'>Add Component</button>
</div>
</div>
</div>
<div class="wrapper" id="add-components">
<div class="panel panel-default " id="addon">
<div class="panel-heading">
</div>
<div class="panel-body">
<div class="col-sm-6">
<label>component</label>
<input type="text" name="component" value="component" placeholder="component" id="component" class="form-control" />
</div>
</form>
</div>
</div>
</div>