First ansible playbook help (Error Checking) - ansible

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 == ""

Related

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

Getting error when try to list all users on the hosts

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

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 - Check if PermitRootLogin is equal to no

i'm new in Ansible
I would like to know if the value of PermitRootLogin = no in / etc / ssh / sshd_config
- hosts: RH7
tasks:
- name: read File
shell: cat /etc/ssh/sshd_config
register: PermitRootLogin no
help me pls
You can use something like this :
- hosts: RH7
tasks:
- name: read File
shell: awk '/#PermitRootLogin/ {print $2}' /etc/ssh/sshd_config
register: PermitRootLogin
- debug: msg="{{ PermitRootLogin.stdout }}"
cat /etc/ssh/sshd_config | awk '/#PermitRootLogin/ {print $2}' : This command will give you the output of PermitRootLogin from the file /etc/ssh/sshd_config.
We will save the value in PermitRootLogin variable and can see it using the debug command.

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

Resources