Zumo de Vidrio
Zumo de Vidrio

Reputation: 2091

Ansible playbook with when variable not executed

I have the following playbook example.yml:

- hosts: all
  remote_user: administrator
  become: yes
  tasks:
  - name: Put resolv.conf
    template:
      src: /home/user/resolv.conf.j2
      dest: /etc/resolv.conf
      backup: yes
      mode: 0644
      when: variable_name == "string"

The purpose is to update the resolv.conf by a customized one.

For executing it I am running:

ansible-playbook example.yml -k -u administrator --become --ask-become-pass --limit server_name -e variable_name='string'

However I get the following error:

SSH password: 
SUDO password[defaults to SSH password]: 

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [server_name]

TASK [Put resolv.conf] *******************************************************
fatal: [server_name]: FAILED! => {"changed": true, "failed": true, "msg": "unsupported parameter for module: when"}
    to retry, use: --limit @/home/user/playbooks/example.retry

PLAY RECAP *********************************************************************
server_name              : ok=1    changed=0    unreachable=0    failed=1

I have tried with different syntax by applying "()" to the variable, change between double and single commas, etc. But always the same error.

If I don't use both, variable and when condition, the task is succesfully completed.

Where is the problem?

Upvotes: 0

Views: 688

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

Mind the padding!

- hosts: all
  remote_user: administrator
  become: yes
  tasks:
  - name: Put resolv.conf
    template:
      src: /home/user/resolv.conf.j2
      dest: /etc/resolv.conf
      backup: yes
      mode: 0644
    when: variable_name == "string"

when is a task property, not template's parameter.

Upvotes: 3

Related Questions