Problem referencing variables

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,

*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

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:

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.

Thanks Kai