Undefined Variable

23 views
Skip to first unread message

Keith Mills

unread,
Jan 30, 2023, 10:40:57 AM1/30/23
to Ansible Project
Hello All,

I need help with and undefined variable in my Ansible Role! Any help will be appreciated!

Ansible Version
ansible 2.9.27
  config file = /home/kjames/ansible.cfg
  configured module search path = [u'/home/kjames/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Jun 28 2022, 15:30:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

Configuration
ansible-config dump --only-changed
DEFAULT_BECOME(/home/kjames/Ansible/ansible.cfg) = True
DEFAULT_BECOME_ASK_PASS(/home/kjames/Ansible/ansible.cfg) = False
DEFAULT_BECOME_METHOD(/home/kjames/Ansible/ansible.cfg) = sudo
DEFAULT_BECOME_USER(/home/kjames/Ansible/ansible.cfg) = root
DEFAULT_HOST_LIST(/home/kjames/Ansible/ansible.cfg) = [u'/home/kjames/Ansible/static-ini-inventory']
DEFAULT_REMOTE_USER(/home/kjames/Ansible/ansible.cfg) = kjames

OS / Environment
CentOS Linux release 7.9.2009 (Core)

$ ansible-playbook ansible-playbooks/create-user.yml

PLAY [node1] *********************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************
ok: [node1]

TASK [create-user : Adding new user] *********************************************************************************************************
ok: [node1]

TASK [create-user : Adding user to sudoers] **************************************************************************************************
fatal: [node1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'user_name' is undefined\n\nThe error appears to be in '/home/kjames/Ansible/ansible-playbooks/roles/create-user/tasks/main.yml': line 7, 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: Adding user to sudoers\n  ^ here\n"}

PLAY RECAP ***********************************************************************************************************************************
node1                      : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0 

Rowe, Walter P. (Fed)

unread,
Jan 30, 2023, 10:44:00 AM1/30/23
to ansible...@googlegroups.com
We would need to see the playbook and role included.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/43ed5b64-1ca7-469b-9154-3e7104af32c2n%40googlegroups.com.

Keith Mills

unread,
Jan 30, 2023, 10:51:10 AM1/30/23
to Ansible Project
Forgot to add!

Playbook:
#
#About: This will create-user
#
---
- hosts: node1
  become: yes
  roles:
  - create-user

Role:
---
- name: Adding new user
  user: name=keith shell=/bin/bash append=yes generate_ssh_key=yes ssh_key_file=.ssh/id_rsa
  tags:
    - create-user


- name: Adding user to sudoers
  lineinfile: dest=/etc/sudoers state=present regexp='^%{{user_name}}' line='%{{user_name}} ALL=(ALL) NOPASSWD:ALL' validate='visudo -cf %s'
  tags:
    - create-user-sudo

- name: Adding public key to authorized_keys
  file: src=/home/{{user_name}}/.ssh/id_rsa.pub dest=/home/{{user_name}}/.ssh/authorized_keys state=link
  tags:
    - create-user-authorized-keys

- name: Get the private key
  shell: cat /home/{{user_name}}/.ssh/id_rsa
  register: ssh_key
  tags:
    - create-user-display-keys

- debug: var=ssh_key
  tags:
    - create-user-display-keys

Rowe, Walter P. (Fed)

unread,
Jan 30, 2023, 11:20:41 AM1/30/23
to ansible...@googlegroups.com
Are you setting a value for user_name? See red below.


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Keith Mills

unread,
Jan 30, 2023, 11:57:36 AM1/30/23
to Ansible Project
How do I set a value?

Rowe, Walter P. (Fed)

unread,
Jan 30, 2023, 1:28:38 PM1/30/23
to ansible...@googlegroups.com
It is your playbook. You have to figure that out.


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
Reply all
Reply to author
Forward
0 new messages