# Ansible variable with find

**URL:** <https://forum.ansible.com/t/ansible-variable-with-find/24835>\
**Category:** Ansible Project\
**Created:** [June 27, 2017, 11:55pm UTC](https://forum.ansible.com/t/ansible-variable-with-find/24835 "2017-06-27T23:55:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [June 27, 2017, 11:55pm UTC](https://forum.ansible.com/t/ansible-variable-with-find/24835/1 "2017-06-27T23:55:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 28, 2017, 2:44pm UTC](https://forum.ansible.com/t/ansible-variable-with-find/24835/2 "2017-06-28T14:44:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [June 30, 2017, 7:03pm UTC](https://forum.ansible.com/t/ansible-variable-with-find/24835/3 "2017-06-30T19:03:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 30, 2017, 7:27pm UTC](https://forum.ansible.com/t/ansible-variable-with-find/24835/4 "2017-06-30T19:27:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 3, 2017, 3:58pm UTC](https://forum.ansible.com/t/ansible-variable-with-find/24835/5 "2017-07-03T15:58:50Z")

</div>

Great explanation. Thanks
