I am having the following playbook in ansible where "{{env}}" is a dynamic variable: could be sit,nft,prod,etc - based on user selection. The action in the play should be executed only if the selected environment is nft.

- hosts: "{{ host }}"
  remote_user: "{{ user }}"

    home_dir: "/home/{{ user }}"
    env: "{{ env }}"

    - name: backup file
        src: "/home/dtcd/gabi/FF_environmentConfig.ini"
        dest: "/home/dtcd/gabi/FF_environmentConfig.ini_orig"
        remote_src: yes

    - name: change params nft
        path: "/home/dtcd/gabi/FF_environmentConfig.ini"
        regexp: '{{item.From}}'
        line: '{{item.To}}'
        state: present
        - { From: 'FF_AMQProcessingMessageTimeoutInMilliseconds=5000', To: 'FF_AMQProcessingMessageTimeoutInMilliseconds=10000' }
        - { From: 'FF_solutionDetectionServiceProcess_FF_ALL_FF_customFeedChannel_ConcurrentThreads=1', To: 'FF_solutionDetectionServiceProcess_FF_ALL_FF_customFeedChannel_ConcurrentThreads=10' }
      when: env == nft

Now, when I am running the playbook, it errors out with the message:

fatal: [servera.com]: FAILED! => { "msg": "The conditional check 'env == nft' failed. The error was: error while evaluating conditional (env == nft): 'nft' is undefined\n\nThe error appears to be in '/ansible-playbook/1-sed.yml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: change params nft\n ^ here\n" }

I have tried using when: "{{env}}" == "nft", using set_facts... but in the end I am still facing the error above.

Using set_facts I was able to overcome the error, however the actions were skipped, despite env=nft.

What am I missing here?


  • Did you try when: env == "nft" ?
    – thrig
    Commented Aug 16, 2022 at 15:06

1 Answer 1


When you write:

      when: env == nft

You are saying: "When the value of the variable env matches the value of the variable nft".

Ansible is telling you that you have no variable named nft ('nft' is undefined), which is accurate.

If you want to compare a variable to a literal string, you need to quote the string:

      when: env == "nft"
  • Apparently, when I tried that, one "=" was missing from the check and it did not worked. Tried it again now and worked. Thank you!
    – JugaruGabi
    Commented Aug 16, 2022 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .