Renat Galiev
Renat Galiev

Reputation: 23

How to change a dict variable in ansible

I have dictionary like this:

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

how i may set group = sudo in dictionary when: env == 'dev'?

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

doesn't work

Upvotes: 2

Views: 499

Answers (1)

Johannes Müller
Johannes Müller

Reputation: 5661

It might be an easy solution if you set the depending value directly in the dict definition:

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

Upvotes: 1

Related Questions