Need to get miniconda basebath from mac using ansible - 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}}"

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

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 module lineinfile with variable path

I need to use the Ansible lineinfile module in such a way that it operates on a variable path. (This is for Ansible 2.5.2.) In this example the filename should depend on the version of PostgreSQL that is actually installed on a remote host (instead of a hardwired version 9.6):
- lineinfile:
path: /etc/postgresql/9.6/main/postgresql.conf
regexp: '^#?\s*log_connections\s*='
line: 'log_connections = on'
state: present
In bash I would use e.g. this expression for obtaining the version and the path:
/etc/postgresq/$(pg_lsclusters -h | awk '{print $1}' | head -n 1)/main/postgresql.conf
It apparently does not work verbatim as parameter path to Ansible's lineinfile module:
FAILED! => {"changed": false, "msg": "Destination
/etc/postgresq/$(pg_lsclusters -h | awk '{print $1}' | head -n
1)/main/postgresql.conf does not exist !", "rc": 257}
So my question is this: How can I form a variable path with Ansible in this use case?
This seems to work fine:
- name: Got it!
command: bash -c "pg_lsclusters -h | awk '{print $1; exit}'"
register: version
- set_fact: version='{{version.stdout}}'
- lineinfile:
path: "/etc/postgresql/{{version}}/main/postgresql.conf"
regexp: '^#?\s*log_connections\s*='
line: 'log_connections = on'
state: present

Resources