Ansible playbook put find exhaust to text file with sed - ansible

How to start a playbook when pulling a playbook and insert it into the config? for example in /etc/nginx/sites-available/default
need to insert this
fastcgi_pass unix: /var/run/php/php7.0-fpm.sock;
How to insert for example 7.1 or 7.2 instead of 7.0 if the PHP versions will differ?
Partially found the solution using find, but how now to insert the result of find into the file?
I use the command: find /var/run/php/ -name *.sock
exhaust such: /var/run/php/php7.2-fpm.sock
Now this case should be inserted into the file by sed

An option would be to use lineinfile
- shell: "php -n -v | head -n 1 | cut -d ' ' -f 2 | cut -d '.' -f 1,2"
register: php_version
- lineinfile:
path: /etc/nginx/sites-available/default
regexp: "^fastcgi_pass unix:"
line: "fastcgi_pass unix: /var/run/php/php{{ php_version.stdout }}-fpm.sock;"

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}'"

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

How to extract string of command result and use it in a loop

Running a nx affected:apps command gives me this output:
> NX NOTE Affected criteria defaulted to --base=master --head=HEAD
> NX Affected apps:
- app-backend
- app-frontend
- app-something
- app-anything
I need to get all the application names and use them again for a command call.
So I started with that
output=$(nx affected:apps)
echo "$output" | grep -E "^\W+app-(\w+)"
This gives me
- app-backend
- app-frontend
- app-something
- app-anything
But I need to get the names only instead to run foo --name={appname} four times.
Also not quite sure how to use it in a loop. Quite new to bash scripting :-(
You may use -o (show matches only) with -P (perl regex moode) in gnu-grep:
nx affected:apps |
grep -oP "^\W+app-\K\w+" |
xargs -I {} docker build -t {} .
If gnu-grep isn't available then use this awk command:
nx affected:apps |
awk -F- '/app-/{print $3}' |
xargs -I {} docker build -t {} .
I don't have nx command here but you can try using xargs:
nx affected:apps | grep '^ -' | cut -d' ' -f4 | xargs -I{} echo docker build -t {} ./dist/{}
Remove echo to actually run the command.
You can use the --plain option:
nx affected:apps --plain
the command should return all the affected apps with space as a divider. You can then store those to a bash array and cycle through them in a for loop, running the command you need:
#!/bin/bash
AFFECTED=($(./node_modules/.bin/nx affected:apps --plain))
for t in ${AFFECTED[#]}; do
echo $t
done

Ansible: module command

I want to execute this command with Ansible:
grep -v -P "^#" /etc/snort/community-sid-msg.map > /etc/snort/community-sid-msg-no-comments.map
I want to know what characters I have to include in my Ansible code:
- name: create file
command: "grep -v -P "^#" /etc/snort/community-sid-msg.map > /etc/snort/community-sid-msg-no-comments.map"
Wouldn't be better to use the copy and replace modules?
- copy:
src: /etc/snort/community-sid-msg.map
dest: etc/snort/community-sid-msg-no-comments.map
- replace:
path: etc/snort/community-sid-msg-no-comments.map
regexp: "^#[^\n]*\n"

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