Ansible variable with find

Running the playbook below and wondering whats the correct syntax so I can use a variable within the find statement below

This line is in question -
when: p.stdout.find"{{(‘user’)}}" != -1

This would be correct without a variable

when: p.stdout.find(‘daniel’) != -1

How do I do this with a variable?

Playbook

  • hosts: localhost
    become: yes
    vars:
    user: daniel
    tasks:
  • name: Create a new user on the server
    user:
    name: “{{user}}”
    state: present
  • name: Check to ensure the user has been created
    shell: cat /etc/passwd | grep “{{user}}”
    register: p
  • debug: msg=“The user {{user}} exists”
    when: p.stdout.find"{{(‘user’)}}" != -1
  • name: Create a file in the /opt folder called program1.sh
    command: touch program1.sh
    args:
    chdir: /opt
    creates: program1.sh

when: p.stdout.find(user) != -1

Thanks! But how does it differentiate between just the text user, and the actual variable user? Or does it just know because user is defined as a variable at the top in the vars section?

I tried that out and it works well.

Text is indicated with single or double quote, so 'user' or "user".
When you don't use quotes it's taken as the variable user.

Great explanation. Thanks