I want to create a ansible role to add user with ssh_keys

├── ansible.cfg
├── hosts
├── playbooks
│   └── add_user.yml
└── roles
    └── add-user
        ├── files
        │   ├── ansible.pub
        │   ├── dhirendra.pub
        │   ├── nitigyas.pub
        │   └── vinayaks.pub
        ├── tasks
        │   └── main.yml
        └── vars
            └── main.yml

tasks/main.yml file is :

- name: AddUser in the server
   name: ansible
   password: $6$sHQAZJ32E$ujHauabN7ZTbOQ/zhT5899EIaFErOecXYvTEyhekexy4dydsAXTGdUpaB8M4fQf2UGpZgX/Zg2Uv6areeuv/r0
   comment: Ansible Configuration Mgmt
   shell: /bin/bash

- name: Add SSH key to users home direcotory
   user: "{{ item.name }}"
   key: "{{ item.file }}"
   state: present
   - ssh_vars

vars/main.yml file is:

 - { name: "ansible" , file: 'ansible.pub' }

while i run this roles I get following error

PLAY [backend1.greengst.com] *************************************************************************************************************

TASK [add-user : AddUser in the server] **************************************************************************************************
ok: [backend1.greengst.com]

TASK [add-user : Add SSH key to users home direcotory] ***********************************************************************************
fatal: [backend1.greengst.com]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'name'\n\nThe error appears to have been in '/etc/ansible/roles/add-user/tasks/main.yml': line 9, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Add SSH key to users home direcotory\n  ^ here\n"}
Debugger invoked

The problem is with the way you use with_items: bare variables are unsupported for quite a while already.

Correct your loop like this: with_items: "{{ ssh_vars }}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.