Getting error when try to list all users on the hosts - bash

I am trying to get all the users I created on the hosts machine. When I run the following command on terminal, I get all the users on the machine.
sudo getent passwd {1000..6000} | cut -d":" -f1
However when I try to run it using ansible, I get an error. I tried all like enquoting in double quotes, escaping the brackets, piping the output to cat etc but nothing is working.
---
- name: "run commands"
become: true
gather_facts: no
hosts: all
tasks:
- name: list all users
shell: getent passwd {1000..6000} | cut -d":" -f1
register: getent
- debug: var=getent.stdout_lines

Note that, per default, Ansible is using /bin/sh, as pointed in the synopsis of the command.
It is almost exactly like the ansible.builtin.command module but runs the command through a shell (/bin/sh) on the remote node.
Source: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html#synopsis
But sh won't interpret a sequence construct like {0..10}.
There are two ways you can overcome this:
Using seq rather:
- shell: getent passwd $(seq 1000 6000) | cut -d":" -f1
register: getent
Specifying to the shell task that you want it executed via bash:
- shell: getent passwd {1000..6000} | cut -d":" -f1
register: getent
args:
executable: /bin/bash

Related

ansible grep plus awk with shell/command directive

I am trying run this ansible playbook
- name: Network Getting Started First Playbook Extended
gather_facts: false
hosts: localhost
tasks:
- name: Disable wolverine
shell: 'kubectl -n testns exec dashmpp-head-0 -c container -- bash -c "list versions | grep -w 20220928025228 | awk '{print $9}'"'
register: target_db2_version
failed_when: target_db2_version.stdout == "" or target_db2_version.stderr != ""
This keeps failing with:
shell: 'kubectl -n testns exec dashmpp-head-0 -c container -- bash -c "list versions | grep -w 20220928025228 | awk '{print $9}'"'
^ here
Problem here is with awk '{print $9}' part. If I remove that the command works fine.
Here is what all I have tried already
Tried running the command manually on shell prompt and it works fine
awk \'{print $9}\'
awk '{print \$9}'
awk \'\{print \$9\}\'
Tries using command directive instead of shell
You should use two single quotes to denote a literal single quote within a single-quoted string in YAML:
shell: 'kubectl -n testns exec dashmpp-head-0 -c container -- bash -c "list versions | grep -w 20220928025228 | awk ''{print $9}''"'
or in this case, there is no need to enclose the string in single quotes to begin with:
shell: kubectl -n testns exec dashmpp-head-0 -c container -- bash -c "list versions | grep -w 20220928025228 | awk '{print $9}'"

stdout issue on Generate Thread dump playbook

I want to create thread dump files on target host using ansible for this purpose I need to get PID and UID of process I need to Generate Thread dump for.
I try run the following lines but instead to get one value (PID) I get 2 values ,
one - PID (2229) and second - I think it PID of Ansible process that running remotely.
I get: "pid.stdout": "2229\n2456601"
that actually 2229 value I need to store as pid.
- name: Generate treaddump.
hosts: all
gather_facts: true
tasks:
- name: get PID.
shell: "ps aux | grep -i jira | pgrep -i java | awk -F '[ ]*' '{print $2}'"
register: pid
- debug:
var: pid.stdout
- name: get User.
shell: "ps aux | grep -i jira | grep -i java | awk -F '[ ]*' '{print $1}'"
register: user
any idea how to get only the PID ?
what I'm doing wrong ?
thank you
I tried to run the playbook as command - I got same output

First ansible playbook help (Error Checking)

I am writing my first Ansible playbook and I am a bit lost, hoping someone can point me in the right direction.
Here is my code:
---
- hosts: all
become: yes
tasks:
- name: check /var/log/messages
shell: "cat /var/log/messages | grep error | mailx -r xxx#xxx.com xxx#gmail.com"
ignore_errors: yes
- name: check /var/log/secure
shell: "cat /var/log/secure | grep denied | mailx -r xxx#xxx.com xxx#gmail.com"
ignore_errors: yes
I am getting a separate email for each server, and even when there is nothing, I get a blank one. I read some stuff about handlers, would one of those be good to say if the output is null, dont do anything.
you can use following snippet. Worked on ansible 2.7+ .
What is doing?
first task is running shell command and storing output in results. "results" is standard variable provided by ansible. you can refer return values in ansible here. here
second task will be : Based on results is empty or not, it will send output over mail
- hosts: all
become: yes
tasks:
- name: check /var/log/messages
shell: "cat /var/log/messages | grep error"
register: results
ignore_errors: yes
- name: Send mail
shell: " echo {{ results.stdout }} | mailx -r xxx#xxx.com xxx#gmail.com"
when: result.stdout == ""

Ansible error on shell command returning zero

Ansible doesn't seem to be able to handle the result '0' for shell commands. This
- name: Check if swap exists
shell: "swapon -s | grep -ci dev"
register: swap_exists
Returns an error
"msg": "non-zero return code"
But when I replace "dev" with "type", which actually always occurs and gives a count of at least 1, then the command is successful and no error is thrown.
I also tried with command: instead of shell: - it doesn't give an error, but then the command is also not executed.
since you want to run a sequence of commands that involve pipe, ansible states you should use shell and not command, as you are doing.
So, the problem is the fact that grep returns 1 (didnt find a match on the swapon output), and ansible considers this a failure. Since you are well sure there is no issue, just add a ignore_errors: true and be done with it.
- name: Check if swap exists
shell: "swapon -s | grep -ci non_existent_string"
register: swap_exists
ignore_errors: true
OR:
if you want to narrow it down to return codes 0 and 1, instruct ansible to not consider failures those 2 rcs:
- name: Check if swap exists
shell: "swapon -s | grep -ci non_existent_string"
register: swap_exists
# ignore_errors: true
failed_when: swap_exists.rc != 1 and swap_exists.rc != 0
I found a better way. if you only need to know the record number this works:
- name: Check if swap exists
shell: "swapon -s | grep -i dev|wc -l"
register: swap_exists
Another way is to always use cat at the end of the pipe. See Ansible shell module returns error when grep results are empty
- name: Check if swap exists
shell: "swapon -s | grep -i dev|cat"
register: swap_exists
You can also parse the grep count result in awk and return your custom output. This will avoid the ignore_errors module.
- name: Check if swap exists
shell: "swapon -s | grep -ci dev" | awk '{ r = $0 == 0 ? "false":"true"; print r }'
register: swap_exists

ansible shell: how to show grep result the same as run at local

- name: check process is running or not
shell: ps -ef |grep abcd |grep defg
register: result
ignore_errors: yes
- debug: msg="{{ result.stdout }}"
the result shows:
TASK [debug] ***********
ok: [52.35.61.9] => {
"msg": "ec2-user 28932 28931 0 17:42 pts/0 00:00:00 /bin/sh -c ps
-ef |grep abcd |grep defg"
}
but if I login to that machine and directly run:
ps -ef |grep abcd |grep defg,
I see nothing because the process has stopped.
in ansible I need to check whether the process is already run. and then run it only if it is not. That is why I use shell and ps (command does not support pipe so I have to use shell) . but calling shell from ansible always shows sth. even the process is not run.
how to make it not show anything, same as when run ps locally?
Try enclosing a character in square brackets like this:
shell: ps -ef |grep [a]bcd |grep defg
This is just to make grep ignore that line in ps output.

Resources