我必须处理来自CloudFormation Outputs
:
- debug: var: stack.stack_outputs
ok: [localhost] => { "stack.stack_outputs": { "Roles": "webserver balancer dbserver", "dbserver": "54.0.1.1 54.0.1.2", "balancer": "54.0.2.3", "webserver": "54.0.2.5 54.0.2.7 54.0.3.1" }}
为此,我想创build3个(dynamic数字!)组,相应地命名为相应的IP地址。
- name: fill roles with proper hosts local_action: add_host hostname={{item}} groupname={{role}} with_whatever: ?...?
for role in stack.stack_outputs.Roles.split(): # Python for ip in stack.stack_outputs[role].split(): # Python local_action: add_host hostname={{ip}} groupname={{role}} # Ansible
注意:
为这三个angular色静态做的方法显然是:
- name: fill role WEBSERVER local_action: add_host hostname={{item}} groupname=webserver with_items: stack.stack_outputs.webserver.split() - name: fill role DBSERVER local_action: add_host hostname={{item}} groupname=dbserver with_items: stack.stack_outputs.dbserver.split() - name: fill role BALANCER local_action: add_host hostname={{item}} groupname=balancer with_items: stack.stack_outputs.balancer.split()
我想dynamic地做,Ansible甚至可能吗?
是的,我可以使用shell module
来破解它的一切在临时文件,然后循环; 但有没有更好的解决scheme?
感谢您的任何build议。
复杂的逻辑不应该存在于剧本或任务列表中。 考虑使用dynamic库存 ,所以你可以编写一个脚本,返回JSON完整的组成员信息。 如果你真的想要在游戏手册中处理数据,你可以使用一个自定义filter ,这个filter仍然可以清理剧本。
它可以使用嵌套循环完成,包括工作脚本test.yml
:
--- - hosts: localhost vars: - stack: stack_outputs: Roles: "webserver balancer dbserver" dbserver: "54.0.1.1 54.0.1.2" balancer: "54.0.2.3" webserver: "54.0.2.5 54.0.2.7 54.0.3.1" tasks: - debug: var=stack.stack_outputs - include: dynamic.yml host_group={{ item.key }} group_ips={{ item.value }} with_dict: "{{ stack.stack_outputs }}"
dynamic.yml
:
--- - debug: msg="Group is {{ host_group }} and ip is {{ item2 }}" when: item2|ipaddr with_items: "{{ group_ips.split() }}" loop_control: loop_var: item2
运行使用ansible-playbook 2.1.2.0
:
ansible-playbook test.yml [WARNING]: provided hosts list is empty, only localhost is available PLAY [localhost] *************************************************************** TASK [setup] ******************************************************************* ok: [localhost] TASK [debug] ******************************************************************* ok: [localhost] => { "stack.stack_outputs": { "Roles": "webserver balancer dbserver", "balancer": "54.0.2.3", "dbserver": "54.0.1.1 54.0.1.2", "webserver": "54.0.2.5 54.0.2.7 54.0.3.1" } } TASK [include] ***************************************************************** included: /home/say/tmp/ansible/dynamic.yml for localhost included: /home/say/tmp/ansible/dynamic.yml for localhost included: /home/say/tmp/ansible/dynamic.yml for localhost included: /home/say/tmp/ansible/dynamic.yml for localhost TASK [debug] ******************************************************************* ok: [localhost] => (item=None) => { "item_foo": "54.0.2.5", "msg": "Group is webserver and ip is 54.0.2.5" } ok: [localhost] => (item=None) => { "item_foo": "54.0.2.7", "msg": "Group is webserver and ip is 54.0.2.7" } ok: [localhost] => (item=None) => { "item_foo": "54.0.3.1", "msg": "Group is webserver and ip is 54.0.3.1" } TASK [debug] ******************************************************************* ok: [localhost] => (item=None) => { "item_foo": "54.0.2.3", "msg": "Group is balancer and ip is 54.0.2.3" } TASK [debug] ******************************************************************* skipping: [localhost] => (item=None) skipping: [localhost] => (item=None) skipping: [localhost] => (item=None) TASK [debug] ******************************************************************* ok: [localhost] => (item=None) => { "item_foo": "54.0.1.1", "msg": "Group is dbserver and ip is 54.0.1.1" } ok: [localhost] => (item=None) => { "item_foo": "54.0.1.2", "msg": "Group is dbserver and ip is 54.0.1.2" } PLAY RECAP ********************************************************************* localhost : ok=9 changed=0 unreachable=0 failed=0
你需要Ansible 2.1或以上来运行它。