Ansible - 修改variable.stdout子字段

时间:2016-07-27 13:52:39

标签: ansible ansible-playbook

我有一个ansible shell任务的输出,它是一个json:

stdout: {
  "override_attributes": {
    "prod": {
      "hosts": [
        "10.100.0.150",
        "10.100.0.151"
]
}}}

我想修改这样的IP:

set_fact: variable.stdout.override_attributes.prod.hosts=groups['GROUP_A']

之后我想看到完整的json修改变量.stdout

这可能吗?

1 个答案:

答案 0 :(得分:0)

combine过滤器有一个丑陋的解决方案:

---
- hosts: localhost
  vars:
    stdout: {
      "override_attributes": {
        "prod": {
          "hosts": [
            "10.100.0.150",
            "10.100.0.151"
           ],
          "users": [
            "user1",
            "user2"
          ]
    }}}
    new_hosts:
      - host_name1
      - host_name2
  tasks:
    # original stdout
    - debug: var=stdout
    # update override_attributes.prod.hosts with new_hosts
    - set_fact:
        stdout: "{{stdout | combine({\"override_attributes\":{\"prod\":{\"hosts\":new_hosts} } }, recursive=True )}}"
    # updated stdout, users list unchanged
    - debug: var=stdout