penguin
penguin

Reputation: 133

Ansible set dictionary value based on another variable value

How can I implement below condition, to set value of dictionary config in Ansible ?
Dictionary config and variable turn_on_encryption are both specified in same vars file.

if turn_on_encryption == true, then:

  config:
    path: /var/www/html
    cert_path: /path/to/certificate
    protocol: https

else:

  config:
    path: /var/www/html
    cert_path: ""
    protocol: http

Is this possible with Ansible ?

Upvotes: 1

Views: 579

Answers (1)

Frenchy
Frenchy

Reputation: 17007

You could do something like below, using the if else in jinja2 :

  vars:
    config:
      path: /var/www/html
      cert_path: "{{ '/path/to/certificate' if turn_on_encryption else '' }}"
      protocol: "{{ 'htpps' if turn_on_encryption else 'http' }}"

you could use too the ternary filter jinja2: ternary sample

Upvotes: 2

Related Questions