Jenisha_T
(Jenisha T)
1
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”: "anil500: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]
Jenisha_T
(Jenisha T)
3
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]}’
racke
(Stefan Hornburg)
6
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
vbotka
(Vladimir Botka)
7
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
Karl_Auer
(Karl Auer)
10
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] }}”
Jenisha_T
(Jenisha T)
12
Thank you, everyone, grep followed by cut gave me desired output
Jenisha_T
(Jenisha T)
13
This logic works. Thankyou
How do we incorporate if-else in set_fact ?