如何在ansible中更改dict变量

时间:2017-01-16 14:58:08

标签: ansible

我有这样的字典:

developers:
  djohn:
    fullname: John Doe
    group: developer
    sshkey: "{{ lookup('file', 'ssh_keys/djohn.pub') }}" 

我如何在字典when: env == 'dev'中设置group = sudo?

- name: change dict value
  set_fact:
    users: "{{ developers | default({}) | combine(item.key:{group: 'sudo'}) }}"
  with_dict: "{{ developers }}"
  when: env == 'dev'

不起作用

1 个答案:

答案 0 :(得分:0)

如果直接在dict定义中设置依赖值,这可能是一个简单的解决方案:

developers:
  djohn:
    fullname: John Doe
    group: "{{ (env == "dev") | ternary("sudo", "developer" }}"
    sshkey: "{{ lookup('file', 'ssh_keys/djohn.pub') }}"