variable not defined

126 views
Skip to first unread message

Kenady Inampudi

unread,
Aug 24, 2022, 5:17:38 AM8/24/22
to Ansible Project
Here is my play book, i am trying to check the id's using wildcards like ora****
I am getting variable not defined 

---
- hosts: node1.example.com
  gather_facts: no
  ignore_unreachable: yes
  tasks:
    - shell: "cat /etc/passwd|awk -F : '{print $1}'|grep -i '^ora.*$'"
      register: userid
      ignore_errors: true
    - shell: id "{{ item }}"
      register: id
      with_items: "{{ userid.stdout.split('\n') }}"
    - debug:
        var: id.stdout_lines


----------------------------
OUTPUT


PLAY [node1.example.com] ************************************************************************************************************************************************************************************************

TASK [shell] ******************************************************************************************************************************************************************************************************
changed: [node1.example.com]

TASK [shell] ******************************************************************************************************************************************************************************************************
changed: [node1.example.com] => (item=oraabc)
changed: [node1.example.com] => (item=orapq3)
changed: [node1.example.com] => (item=orapqj)
changed: [node1.example.com] => (item=oracle)

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [node1.example.com] => {
    "id.stdout_lines": "VARIABLE IS NOT DEFINED!"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
node1.example.com                : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Vladimir Botka

unread,
Aug 24, 2022, 6:38:12 AM8/24/22
to Kenady Inampudi, ansible...@googlegroups.com
On Wed, 24 Aug 2022 02:17:38 -0700 (PDT)
Kenady Inampudi <ken...@cis-in.com> wrote:

> - shell: id "{{ item }}"
> register: id
> with_items: "{{ userid.stdout.split('\n') }}"
> - debug:
> var: id.stdout_lines

The variable *id* was registered in a loop. You need the attribute
*results*. (Take a look at *id*.)

Try

- debug:
msg: "{{ id.results|map(attribute='stdout')|list }}"


Notes:

* Use *getent* instead of reading /etc/passwd* on your own
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/getent_module.html

* Use *command* instead of *shell*, *loop* instead of *with_items*,
and *userid.stdout_lines* instead of *userid.stdout.split('\n')*

- command: "id {{ item }}"
register: id
loop: "{{ userid.stdout_lines }}"

--
Vladimir Botka

Rowe, Walter P. (Fed)

unread,
Aug 24, 2022, 7:21:58 AM8/24/22
to ansible...@googlegroups.com
You can make your shell command much more efficient. No need to use cat and grep.

awk -F: '$1 ~ /^ora/ { print $1 }' /etc/passwd

This will find and print all usernames that begin "ora".

    - shell: "awk -F: '$1 ~ /^ora/ { print $1 }' /etc/passwd"
      register: userid
      ignore_errors: true

Then you can look at userid.results.stdout_lines (as a list) to see the list of usernames.

    - debug: var=userid.results.stdout_lines

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/20220824123753.15430d86%40gmail.com.

Kenady Inampudi

unread,
Aug 24, 2022, 8:05:14 AM8/24/22
to Ansible Project
Thank you  Vladimir Botka,

This worked like a charm.

- debug:
msg: "{{ id.results|map(attribute='stdout')|list }}"

------------
i was looking at the suggestion   * Use *getent* instead of reading /etc/passwd* on your own
here i am trying to find all the oracle related ids for which i am using the wildcard  grep -i '^ora.*$'"

but when i use getent i get the below error, ------------can i use delimiters like ora*** to get oracle related ID's


---
- hosts: node1.example.com
  gather_facts: no
  ignore_unreachable: yes
  tasks:
    - getent:
        database: passwd
        key: ^ora.*$'
      register: userid
      ignore_errors: true

    - command: "id {{ item }}"
      register: id
      loop: "{{ userid.stdout_lines }}"
    - debug:
        msg: "{{ id.results|map(attribute='stdout')|list }}"

****************************OUTPUT*****************
PLAY [node1.example.com] ************************************************************************************************************************************************************************************************

TASK [getent] *****************************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host node1.example.com is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
fatal: [node1.example.com]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "One or more supplied key could not be found in the database."}
...ignoring

TASK [command] ****************************************************************************************************************************************************************************************************
fatal: [node1.example.com]: FAILED! => {"msg": "'dict object' has no attribute 'stdout_lines'"}

PLAY RECAP ********************************************************************************************************************************************************************************************************
node1.example.com                : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=1


Vladimir Botka

unread,
Aug 24, 2022, 8:32:54 AM8/24/22
to Kenady Inampudi, ansible...@googlegroups.com
On Wed, 24 Aug 2022 05:05:14 -0700 (PDT)
Kenady Inampudi <ken...@cis-in.com> wrote:

> tasks:
> - getent:
> database: passwd
> key: ^ora.*$'
> register: userid
> ignore_errors: true
> - command: "id {{ item }}"
> register: id
> loop: "{{ userid.stdout_lines }}"
> - debug:
> msg: "{{ id.results|map(attribute='stdout')|list }}"

Try

- hosts: node1.example.com
gather_facts: false
vars:
users: "{{ getent_passwd.keys()|list }}"
tasks:
- getent:
database: passwd
- command: "id {{ item }}"
register: id
loop: "{{ users|select('match', '^user.*$') }}"
- debug:
msg: "{{ id.results|map(attribute='stdout')|list }}"

Notes:

* The module *getent* stores the data automagically. In the case of
*passwd* the dictionary will be *getent_passwd*

* Put the declaration of *users* into vars

* Fit the regex to your needs and iterate the selected users

* The debug of the results is the same as before

* Take a look at *getent_passwd*

* The same way you can get the content of /etc/group in the
dictionary *getent_group*

* You can create any structure you like when you have both
dictionaries *getent_passwd* and *getent_group*


--
Vladimir Botka

Rowe, Walter P. (Fed)

unread,
Aug 24, 2022, 8:57:05 AM8/24/22
to ansible...@googlegroups.com
You all are making this way too hard. You don't even have to ignore errors with this task.

    - shell: "awk -F: '$1 ~ /^ora/ { print $1 }' /etc/passwd
"
      register: userids

    - debug: var=userids.results.stdout_lines
      when: userid.results.stdout_lines

Then 'when' condition will be true only when the list is longer than zero length.

Rowe, Walter P. (Fed)

unread,
Aug 24, 2022, 9:04:37 AM8/24/22
to ansible...@googlegroups.com


++++++
---
- hosts: localhost
  become: false
  gather_facts: false
  tasks:
    - shell: "awk -F: '$1 ~ /^{{ user }}/ { print $1 }' /etc/passwd"
      register: userid

    - debug: var=userid.stdout_lines
      when: userid.stdout_lines
++++++


++++++

% ansible-playbook foo.yml -e user=_snt

PLAY [localhost] *******************************************************************************************************

TASK [shell] ***********************************************************************************************************
changed: [localhost]

TASK [debug] ***********************************************************************************************************
ok: [localhost] => {
    "userid.stdout_lines": [
        "_sntpd"
    ]
}

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



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-project+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/43E048F6-1D8F-42A0-BC1B-22CB92961E96%40nist.gov.

Reply all
Reply to author
Forward
0 new messages