stdout issue on Generate Thread dump playbook - ansible

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

Related

Need to get miniconda basebath from mac using ansible

Hi I am trying to get miniconda base path, to set the PATH but fail to get it, tried with shell and command , output getting as same input : "conda info | grep -i 'conda av data dir'|awk '{print $6}'"
-name: get_conda_path
shell: echo $(conda info | grep -i 'conda av data dir'|awk '{print $6}')
register: conda_path
- set_fact:
path: "{{conda_path.stdout}}"
Attached output image for the refrenceenter image description here
That's simply because of echo used as part of shell module.
When you remove echo, ansible executes conda info command & then applies the grep followed by awk..
- name: get_conda_path
shell: "conda info | grep -i 'conda av data dir'|awk '{print $6}'"
register: conda_path
- set_fact:
path: "{{conda_path.stdout}}"

Ansible find if the server has websphere running on it or not

I'm in needing of help. I currently have to find if the server has websphere running on it or not. I can do it through ps -ef|grep websphere. Problem i'm facing is we can use only "raw" module as other modules wont run on old linux OS. I'm thinking of doing using the below code but not sure how to take the output of it and pass it in a file that gives server name and 0 or 1 where 0 is false and 1 is true.
---
- name: To find the websphere servers
hosts: websphere
tasks:
- name:
raw: "if [[ $(ps aux | grep cron | grep -vc grep) > 0 ]] ; then echo 1; else echo 0 ; fi"
Please try the below approach.
- name: Getting process IDs of the process
pids:
name: cron
register: cron_pids
- name: Printing the process IDs obtained
debug:
msg: "Process IDs of cron:{{cron_pids.pids|join(',')}}"
(OR)
You can use shell module to retrieve the info using registered variable
- name: Get running processes list from remote host
shell: "ps -aux --no-headers | grep cron | awk '{print $2}'"
register: running_processes
- debug:
msg: "{{running_process}}"

Ansible shell module does not return as expected

Background
I'm writing an Ansible playbook to detect a running process on a remote node using Ansible's shell module.
Running ps aux and pgrep as root via Ansible (through AWX) suggests the process is not running
Running ps aux and pgrep commands elevated to root while logged into the remote node via ssh suggests the process is running (expected result)
Question
Why does running ps aux and pgrep through Ansible produce different results than running those commands while sshed into the remote node?
Also, why does running ps aux | grep running_process through Ansible show each line twice, but with a different ID?
I'm mostly concerned with understanding what's going on with Ansible/shell here (although if I can find a solution for detecting the running process that's great too).
Playbook Tasks
- name: grep for running_process
shell: ps aux | grep running_process
ignore_errors: yes
become: yes
check_mode: no
register: grep_result
- name: pgrep for running_process
shell: pgrep running_process
ignore_errors: yes
become: yes
check_mode: no
register: pgrep_result
Playbook Grep Result
"stdout_lines": [
"root 18225 ... /bin/sh -c ps aux | grep running_process",
"root 18226 ... /bin/sh -c ps aux | grep running_process",
"root 18232 ... grep running_process",
"root 18233 ... grep running_process"
]
Playbook Pgrep Result
"rc": 1,
"stdout": "",
SSH Results
[root#host user]# /bin/sh -c "ps aux | grep running_process"
nobody ... running_process ...
root ... /bin/sh -c ps aux | grep running_process
root ... grep running_process
[root#host user]# /bin/sh -c "pgrep running_process"
12345

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