Problem referencing variables

39 views
Skip to first unread message

Dave Florek

unread,
Sep 19, 2017, 1:13:37 PM9/19/17
to Ansible Project
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,

Branko Majic

unread,
Sep 21, 2017, 6:01:37 AM9/21/17
to ansible...@googlegroups.com
On Tue, 19 Sep 2017 10:13:37 -0700 (PDT)
Dave Florek <dave.a...@gmail.com> wrote:

> *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:
>
>
> - lineinfile:
> 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)?

Best regards

--
Branko Majic
XMPP: bra...@majic.rs
Please use only Free formats when sending attachments to me.

Бранко Мајић
XMPP: bra...@majic.rs
Молим вас да додатке шаљете искључиво у слободним форматима.

Dave Florek

unread,
Sep 21, 2017, 11:20:45 AM9/21/17
to ansible...@googlegroups.com
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:

---
- hosts: all

  become: yes

  vars_prompt:
   - name: "new_user"
   - prompt: "What is the name of the new user to create?"

  tasks:
  - name: Create new user
    command: useradd {{ new_user }}


  - 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?

  - name: Add user to local authorized_keys file
  - lineinfile:
        path: /home/"{{ new_user }}"/.ssh/authorized_keys
        state: present
        create: True
#       line: #Reference to what upper decision was
        group: {{ new_user }}
        owner: {{ 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?


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/tXRBpl4c0U8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20170921120110.5e7791f1%40majic.rs.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Sep 21, 2017, 1:40:12 PM9/21/17
to ansible...@googlegroups.com
On 21. sep. 2017 17:20, Dave Florek wrote:
> 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
>
> become: yes
>
> vars_prompt:
> - name: "new_user"
> - 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.


> tasks:
> - name: Create new user
> command: useradd {{ new_user }}
>
>
> - 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.


> - name: Add user to local authorized_keys file
> - lineinfile:

You need to remove the dash in front of lineinfile.


> path: /home/"{{ new_user }}"/.ssh/authorized_keys

Remove the double quotes.


> state: present
> create: True
> # line: #Reference to what upper decision was
> group: {{ new_user }}
> 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.


--
Kai Stian Olstad

Dave Florek

unread,
Sep 21, 2017, 3:19:52 PM9/21/17
to ansible...@googlegroups.com
Thanks Kai

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/tXRBpl4c0U8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages