# Problem referencing variables

**URL:** <https://forum.ansible.com/t/problem-referencing-variables/25528>\
**Category:** Ansible Project\
**Created:** [September 19, 2017, 5:13pm UTC](https://forum.ansible.com/t/problem-referencing-variables/25528 "2017-09-19T17:13:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Dave](https://forum.ansible.com/u/Dave)\
**Post date:** [September 19, 2017, 5:13pm UTC](https://forum.ansible.com/t/problem-referencing-variables/25528/1 "2017-09-19T17:13:37Z")

</div>

Hi,

I’m trying to see where I’m going wrong here:

**Relevant code snippet:**

`- lineinfile:`  
`path: /home/"{{ new_user }}"/.ssh/`

**Error message I get at execution:**

`ERROR! Syntax Error while loading YAML.`

`The error appears to have been in '/opt/ansible/setup_scripts/create_user.yml': line 20, column 1, but may`  
`be elsewhere in the file depending on the exact syntax problem.`

`The offending line appears to be:`

`- lineinfile:`  
`path: /home/"{{ new_user_acct_name }}"/.ssh/authorized_keys`  
`^ here`  
`We could be wrong, but this one looks like it might be an issue with`  
`missing quotes. Always quote template expression brackets when they`  
`start a value. For instance:`

`with_items:`  
`- {{ foo }}`

`Should be written as:`

`with_items:`  
`- ``"{{ foo }}"`

Can someone point me in the right direction?

Thanks,

---

<div class="post-metadata">

**Author:** ![azaghal](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/azaghal/32/4743_2.png) [@azaghal](https://forum.ansible.com/u/azaghal)\
**Post date:** [September 21, 2017, 10:01am UTC](https://forum.ansible.com/t/problem-referencing-variables/25528/2 "2017-09-21T10:01:10Z")

</div>

> \*Error message I get at execution:\*
> 
> ERROR! Syntax Error while loading YAML.
> 
> The error appears to have been in  
> '/opt/ansible/setup\_scripts/create\_user.yml': line 20, column 1, but  
> may be elsewhere in the file depending on the exact syntax problem.

It can easily happen to be an issue elsewhere in the file, but it's  
hard to tell since you posted only a very small snippet.

If you comment-out that particular task do you still have the same  
issue being reported?

I would double-check the indentation as well, since what you posted as  
sample had a bit "deeper" indentation (maybe tabs vs spaces or too much  
indentation).

What version of Ansible are you using?

> The offending line appears to be:
> 
> &nbsp;&nbsp;- lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /home/"{{ new\_user\_acct\_name }}"/.ssh/authorized\_keys

Any reason you wouldn't be using authorized\_key module  
([http://docs.ansible.com/ansible/latest/authorized\_key\_module.html](http://docs.ansible.com/ansible/latest/authorized_key_module.html))?

Best regards

---

<div class="post-metadata">

**Author:** ![Dave](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Dave](https://forum.ansible.com/u/Dave)\
**Post date:** [September 21, 2017, 3:20pm UTC](https://forum.ansible.com/t/problem-referencing-variables/25528/3 "2017-09-21T15:20:20Z")

</div>

Hi Branko,

Thanks for the response. I only posted what was on line 20. I’m noticing a general issue where if I double-bracket variables, even as Ansible instructs me, I still receive error messages. I’m using Ansible version 2.1.1.0.

Code:

---

<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:** [September 21, 2017, 5:39pm UTC](https://forum.ansible.com/t/problem-referencing-variables/25528/4 "2017-09-21T17:39:54Z")

</div>

> Hi Branko,
> 
> Thanks for the response. I only posted what was on line 20.

It's recommended to show everything, you'll get help a lot faster that way because the problem can be anywhere as you can see by my comments bellow.

> Code:
> 
> ---  
> - hosts: all
> 
> &nbsp;&nbsp;become: yes
> 
> &nbsp;&nbsp;vars\_prompt:  
> &nbsp;&nbsp;&nbsp;- name: "new\_user"  
> &nbsp;&nbsp;&nbsp;- prompt: "What is the name of the new user to create?"

You need to remove the dash in front of prompt, one list item contains name and prompt.

> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;- name: Create new user  
> &nbsp;&nbsp;&nbsp;&nbsp;command: useradd {{ new\_user }}
> 
> &nbsp;&nbsp;- name: Generate new SSH pub/priv key for user  
> # Do you just have a file with a pasted public key for distribution or  
> auto-generate one?

You can't have a name: without a module, Ansible will fail.

> &nbsp;&nbsp;- name: Add user to local authorized\_keys file  
> &nbsp;&nbsp;- lineinfile:

You need to remove the dash in front of lineinfile.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /home/"{{ new\_user }}"/.ssh/authorized\_keys

Remove the double quotes.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create: True  
> # line: #Reference to what upper decision was  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: {{ new\_user }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: {{ new\_user }}

When you have {{ after a colon you need to wrap them in single or double quotes like this "{{ new\_user }}".

> For some reason, when I reference the new user name as part of a path to  
> set a file, I get the requirement that the variable needs to be in quotes.  
> Any ideas?

I don't understand the problem, but maybe the comments about give you the answer if not show the code and the error message.

---

<div class="post-metadata">

**Author:** ![Dave](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Dave](https://forum.ansible.com/u/Dave)\
**Post date:** [September 21, 2017, 7:19pm UTC](https://forum.ansible.com/t/problem-referencing-variables/25528/5 "2017-09-21T19:19:36Z")

</div>

Thanks Kai
