When condition to check domainName

Hello,

I am running into an issue, this is simple playbook which tries to copy right config file based on Domain name, I tried executing below code snippet it’s skipping the non prod and executing prod, Not sure whats wrong here, can some one throw a light here.

`

  • name: Gathering facts locally at role level
    setup:
    filter: ansible_*

  • set_fact:
    domainName: “{{ ansible_fqdn.split(‘.’, 1)[1] }}”

  • name: Display all variables/facts known for a host
    debug:
    var: domainName

  • name: Copy the non prod config file
    win_template:
    src: nonprod.yaml.j2
    dest: C:\ProgramData\program\file.yaml
    newline_sequence: \n
    when: ((domainName == “AD1”) or (domainName == “AD2”))

  • name: Copy the prod config file
    win_template:
    src: prod.yaml.j2
    dest: C:\ProgramData\program\file.yaml
    when: ((domainName != “AD1”) or (domainName != “AD2”))
    `

Thanks and Regards
Raj

Mustaches don't stack,

when: ((domainName == "AD1") or (domainName == "AD2")) <= no
mustaches needed when 'when:', you are trying to do double
interpolation (conditionals have implicit {{ }})

Also you probably want to use 'in' instead of multiple comparisons:

  when: domainName in ["AD1", "AD2"]

Thanks for the explanation Brian, you are “Awesome”. I’ve used “When & In” and it worked.