Des Horsley
Des Horsley

Reputation: 1888

Ansible vars using lookups

I have an Ansible playbook that populates some variables, here is a snippet:

#myTest playbook
---
- hosts: localhost
  connection: local
  become: False
  vars:
    - APP_NAME: "{{lookup( 'env', 'name')| mandatory }}"

I'd like to use another lookup first, and take that value if its been populated. Is this achievable in one line? I'm figuring something like Javascript's ||:

- APP_NAME: "{{lookup( 'env', 'customName') || lookup( 'env', 'name')| mandatory }}"

Upvotes: 1

Views: 1824

Answers (1)

techraf
techraf

Reputation: 68479

You can use the default filter with an option to trigger it if the value of the preceding expression is an empty string (as in the case of an undefined environment variable):

- APP_NAME: "{{ lookup('env', 'customName') | default(lookup('env', 'name'), true) | mandatory }}"

Upvotes: 2

Related Questions