Ansible "until" not able to match the string - ansible

I am trying to display the output of the command using "until",
- name: Waiting for stack to Deploy...
shell: '{{ creds }} heat event-list {{stack_name}}'
register: waiting_for_stack
until: waiting_for_stack.stdout.find("Stack CREATE completed successfully") or waiting_for_stack.stdout.find("CREATE_FAILED")!= -1
retries: 10
delay: 3
- debug: var=waiting_for_stack.stdout_lines
I am looping the shell command to get the entire event list of stack. But looping does not work. It is exiting from the first iteration without matching the string provided in conditions. Event list out-out looks like this.
ok: [10.206.41.150] => {
"waiting_for_stack.stdout_lines": [
"+---------------+--------------------------------------+------------------------+--------------------+----------------------+",
"| resource_name | id | resource_status_reason | resource_status | event_time |",
"+---------------+--------------------------------------+------------------------+--------------------+----------------------+",
"| stack01 | 924498f6-6498-4560-947c-fec5149b9775 | Stack CREATE started | CREATE_IN_PROGRESS | 2016-05-12T02:55:18Z |",
"| MCM2 | 1e6fe65d-971b-49c6-9441-dd77edd47d29 | state changed | CREATE_IN_PROGRESS | 2016-05-12T02:55:18Z |",
"+---------------+--------------------------------------+------------------------+--------------------+----------------------+"
]
}
Am I doing something wrong, or is there a better way to execute this? I am using ansible version 1.7 .

until: ("Stack CREATE completed successfully" in waiting_for_stack.stdout) or ("CREATE_FAILED" in waiting_for_stack.stdout)
Did the job for me.

Related

yaml - Print key and value, if value meets consitions

Given the following yaml:
charts:
# repository with Helm charts for creation namespaces
path: ns
pathMonitoringPrometheus: prom
namespaces:
first:
description: "Description of first"
enabled: false
branch: master
bootstrapChart: bootstrap
syncAccessGroups: []
namespace:
role: k8s-role-of-first
istio: disabled
public: view
sources: []
second:
description: "Description of second"
enabled: false
branch: HEAD
bootstrapChart: bootstrap
namespace:
role: k8s-role-of-second
istio: 1-13-2
labels:
label: second
sources:
- http://url.of.second
How could we get a list of namespaces and their istio value if it is different to "disabled".
We are trying to use "yq" tool, but I guess any approach would be ok, although "yq" would be a preferred approach.
second, 1-13-2
Using kislyuk/yq you can base your filter on jq.
to_entries splits up the object into an array of key-value pairs
select selects those items matching your criteria
String interpolation in combination with the -r option puts together your desired output
yq -r '
.namespaces
| to_entries[]
| select(.value.namespace.istio != "disabled")
| "\(.key), \(.value.namespace.istio)"
'
second, 1-13-2
Using mikefarah/yq the filter is quite similar.
to_entries[] has to be split up to_entries | .[]
String interpolation is replaced using join and an array
yq '
.namespaces
| to_entries | .[]
| select(.value.namespace.istio != "disabled")
| [.key, .value.namespace.istio] | join(", ")
'
second, 1-13-2
this will do:
cat /path/tp/your.yaml |yq -r '.namespaces | to_entries[] | "\(.key) \(.value.namespace.istio)"'`
will result:
first disabled
second 1-13-2

Laravel | Return rows in order based off pivot

I have the following setup:
stages:
| id | name | order |
commands:
| id | name | body |
------------------------------=
| 1 | test | phpunit |
| 2 | style | echo "style" |
| 3 | deploy | deploy |
command_stage:
| command_id | stage_id | order |
---------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 1 | 2 |
Basically, I would like to create a method on the stage model which allows me to get all of the commands back based off of the order, but commands have a specific order and so do the stages.
So each command is stored under a stage so we know which part to run but each command also has an order in that stage. Now I know I can do something like the following:
$inOrder = collect();
Stage::get()->each(function ($stage) {
$commands = $stage->commands()->orderByPivot('order')->get();
$inOrder->push($commands);
});
return $inOrder;
But I was wondering if there is a nicer way to do this? Or even a way to do this solely on one database hit?
Pre-load and sort the relationship:
$stages = Stage::with(['commands' => function ($subQuery) {
$subQuery->orderBy('command_stage', 'order');
}])->get();
foreach($stages as $stage) {
$inOrder->push($stage->commands);
}
This method of Eager-Loading will reduce the N+1 query issue of doing $stage->commands() inside the foreach() loop.

How to ssh into and issue command to list of ip addresses in a txt file Jenkins

I have a list of ip addresses in a text file that I wish to use in a script.
Here is the code outputting the ip addresses in the text file
openstack server list | grep agent | awk '{print \$9}' >> ${STACK}_list.txt
I would like to retrieve the ip addresses and use in a loop by ssh'ing in to them but not sure how to do that
Please refer this post.
script to read a file with IP addresses and login
Might be helpful for you.
Thanks.
Subhadeep
You can use a regex to filter all ip addresses from the server list-output:
openstack server list | grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*'
You could pipe this output into a file if you need or to use this within a bash-script you could make something like this, without writing it into a file:
#!/bin/bash
#
ADDRESSES=$(openstack server list | grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*')
for ADDRESS in $ADDRESSES
do
echo "ip: $ADDRESS"
done
It reads all ip-addresses from ther server-list output and iterate within the for-loop over this output and prints each ip separate on the terminal. Instead of the echo you could insert your ssh-command.
Example-server on my deployment:
root#m1r1:~# openstack server list
+--------------------------------------+-----------------------+--------+--------------------------+----------------+--------+
| ID | Name | Status | Networks | Image | Flavor |
+--------------------------------------+-----------------------+--------+--------------------------+----------------+--------+
| 46d04a77-4d33-4bb3-8214-b1444eed33a3 | server1 | ACTIVE | l2-network=192.168.4.131 | cirros | XS |
| e9489aca-00c3-4fc9-afc5-515c08b17406 | server2 | ACTIVE | l2-network=192.168.4.61 | | XS |
| ea8cec6a-a8d5-4bbb-970e-aaf65d7374b2 | server3 | ACTIVE | l2-network=192.168.4.163 | cirros | S |
| 7d934ec4-1d53-467b-9220-d67b4b68a832 | server4 | ACTIVE | l2-network=192.168.4.184 | | XS |
| 74d3036e-372a-4566-8ba2-10a0760c5562 | server5 | ACTIVE | l2-network=192.168.4.232 | cirros | XS |
| e08e1637-f4df-478d-a478-6578d038cb22 | server6 | ACTIVE | l2-network=192.168.4.190 | | XS |
| 8307a481-679e-4df0-a64e-3a497b13ac81 | server7 | ACTIVE | l2-network=192.168.4.202 | | XS |
| 38d10b12-daa5-483e-b9a5-9a16ba14d841 | server8 | ACTIVE | l2-network=192.168.4.250 | cirros | XS |
+--------------------------------------+-----------------------+--------+--------------------------+----------------+--------+
Output of this example:
ip: 192.168.4.131
ip: 192.168.4.61
ip: 192.168.4.163
ip: 192.168.4.184
ip: 192.168.4.232
ip: 192.168.4.190
ip: 192.168.4.202
ip: 192.168.4.250
#!/bin/sh
openstack server list | grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' > stack
while $(wc -l stack | cut -d' ' -f1) -gt 0 ]
do
ipnumber=$(sed -n '1p' stack)
echo "${ipnumber}"
sed -i '1d' stack
done
The echo command there is just a placeholder. You can replace it with ssh, or whatever else you want to do, with the IP number in the variable.

Shell script to wait till the command execute and status change

I am creating a shell script to Backup openstack cinder volume to glance image as like below.
test1 is volume name in the script.
#!/bin/bash
DATE=`date +%Y-%m-%d`
BACKUPDIR=/mnt/osbk/
declare -a VMS=(
test1
)
source /root/admin-openrc
echo $DATE `date` Starting Backup process of images
for vmname in "${VMS[#]}"
do
echo Backing up $vmname
echo cinder upload-to-image ${vmname} ${vmname}-vol-bkp --disk-format qcow2 --container-format bare --force True
cinder upload-to-image ${vmname} ${vmname}-vol-bkp --disk-format qcow2 --container-format bare --force True
echo glance image-download ${vmname}-vol-bkp --file $BACKUPDIR/${vmname}-vol-bkp-${DATE}.qcow2
glance --os-image-api-version 1 image-download ${vmname}-vol-bkp --file $BACKUPDIR/${vmname}-vol-bkp-${DATE}.qcow2
done
Output looks like this:
2018-12-29 Sat Dec 29 16:37:45 IST 2018 Starting Backup process of images
Backing up test1
cinder upload-to-image test1 test1-vol-bkp --disk-format qcow2 --container-format bare --force True
+---------------------+--------------------------------------+
| Property | Value |
+---------------------+--------------------------------------+
| container_format | bare |
| disk_format | qcow2 |
| display_description | |
| id | 26c90209-8151-4136-b5de-f2ad7419b100 |
| image_id | 01e88175-a3fa-4354-8c0f-e4fafd9c9fc3 |
| image_name | test1-vol-bkp |
| is_public | False |
| protected | False |
| size | 2 |
| status | uploading |
| updated_at | 2018-12-29T11:07:00.000000 |
| volume_type | None |
+---------------------+--------------------------------------+
glance image-download test1-vol-bkp --file /mnt/osbk//test1-vol-bkp-2018-12-29.qcow2
404 Not Found
The resource could not be found.
Image 01e88175-a3fa-4354-8c0f-e4fafd9c9fc3 is not active (HTTP 404)
From above output the status is uploading...
I need to hold my script to wait or check the status of volume change to Active, then only the glance image download command has to run.
What am I doing wrong?

Retrieving Collection from mLab

I am using a MongoDB on mLab to store a basic collection of boardgames which I wish to show in my Ruby app. I have completed a tutorial that uses Mongoid to implement this locally, but so far I can't get it working with the mLab instance of the DB.
I add this to my mongoid.yml file
development:
clients:
default:
uri: 'mongodb://user:password#ds141232.mlab.com:41232/boardgame_banter'
The other options automatically generated in the config file, I have left blank (as default).
I want to understand these 2 lines from the Terminal:
MONGODB | ds141232-a.mlab.com:41232 | boardgame_banter.find | STARTED | {"find"=>"boardgames", "filter"=>{}}
MONGODB | ds141232-a.mlab.com:41232 | boardgame_banter.find | SUCCEEDED | 0.037816999999999996s
I get no errors, but also no documents returned and the generated index.html is blank...
Can anyone explain the first of the two lines MONGODB | ... to me, or at least confirm if my assumptions below are correct? Particularly the last part of the chain, is this telling me that the filtered results are empty?
MONGODB | <<hostname>> | <<database.find()>> | <<STATUS>> | {"find"=><<collection>>, "filter"=>{<<no results??>>}}
UPDATE after suggestion from #tfogo in the comments
In my controller:
# GET /boardgames
# GET /boardgames.json
def index
#boardgames = Boardgame.all
#log = Boardgame.all.to_a
puts "LOG: #{#log}"
end
Which produces the following empty Log statement in the console:
Started GET "/boardgames" for 127.0.0.1 at 2018-03-02 11:25:00 +0100
Processing by BoardgamesController#index as HTML
D, [2018-03-02T11:25:00.186878 #12983] DEBUG -- : MONGODB | ds141232-a.mlab.com:41232 | boardgame_banter.find | STARTED | {"find"=>"boardgames", "filter"=>{}}
D, [2018-03-02T11:25:00.223330 #12983] DEBUG -- : MONGODB | ds141232-a.mlab.com:41232 | boardgame_banter.find | SUCCEEDED | 0.035911000000000005s
LOG: [#<Boardgame _id: 5a984b439de90b3769420f2d, name: nil, rating: nil, minplayer: nil, maxplayer: nil, duration: nil, owner: nil>]
Rendering boardgames/index.html.erb within layouts/application
D, [2018-03-02T11:25:00.235908 #12983] DEBUG -- : MONGODB | ds141232-a.mlab.com:41232 | boardgame_banter.find | STARTED | {"find"=>"boardgames", "filter"=>{}}
D, [2018-03-02T11:25:00.274734 #12983] DEBUG -- : MONGODB | ds141232-a.mlab.com:41232 | boardgame_banter.find | SUCCEEDED | 0.038311s
Rendered boardgames/index.html.erb within layouts/application (42.3ms)
Completed 200 OK in 127ms (Views: 76.9ms)

Resources