简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM