How to split value from list

Hi
Could you pls help me with the splitting value from the list. I need to extract first value anil before (‘’:')

grep_atm.stdout_lines output below

ok: [localhost] => {
“msg”: "anil:x:500:500:Anil:/home/anil:/bin/bash
"
}

I tried,

  • set_fact:
    user_name: “{{ grep_atm.stdout_lines }}”
    register: user_name.split(‘:’)[0]
  • debug:
    var: user_name.split(‘:’)[0]

Based on your data, look at this module:
https://docs.ansible.com/ansible/latest/modules/getent_module.html

It supports only for the database. My output in register

you should either use set_fact, or register. Both doesn't make sense.

Hello Jenisha,

Assuming that you are getting this value from /etc/passwd., try to change you command

tail -1 /etc/passwd | awk ‘{split($0,a,“:”); print a[1]}’

Hello Jenisha,

Assuming that you are getting this value from /etc/passwd., try to change you command

tail -1 /etc/passwd | awk '{split($0,a,":"); print a[1]}'

cut -d : -f 1 is more concise and readable than your awk command.

Otherwise I would really like to know why Jenisha resorts to reading the password file.

Regards
         racke

user_name: "{{ my_string.split(':')[0] }}"

This is the correct expression, given the data is in the variable "my_string",
wherever it might come from.

Hi Team
I get below error when I use split directly,

The error was: ‘list object’ has no attribute ‘split’

How to unlist in ansible

In my case cut doesn’t work. I m searching for a particular user from a password.
$ grep anil /etc/passwd

so what’s wrong with:

USR=“jenisha”
FOUND=grep $USR /etc/passwd | cut -d: -f1
if [ -z “$FOUND” ] ; then
echo “No such user: $USR”
else

do whatever…

fi

?

Regards, K.

Did you try this ?

user_name: “{{ my_string[0].split(‘:’)[0] }}”

Thank you, everyone, grep followed by cut gave me desired output

This logic works. Thankyou

How do we incorporate if-else in set_fact ?