Capture file existance status and send output to csv file in ansible - azure

Here is Ansible playbook to check the config file exist and capture that output using register and send that output to csv file.If file exist It should get 1 in csv file.But am getting error "The task includes an option with an undefined variable. The error was : 'dict object' has no attribute 'stdout' \n\n The error appeares to be in ..../..../../main.yml , but may \n be elsewhere in the file depending n the exact syntax problem. \n \n The offending line appears to be \n\n\n"
---
- hosts: all
tasks:
- name : Gather the metadata
shell : curl -H Metadata:true "http://169.254.169.254/metadata/instance"
register : vm_medtadata
- name : Assign the meta json to variable
set_facts:
meta : "{{vm_metadata.stdout }}"
- name : setting the facts for csv
set_fact:
vm_resourcegroup: "{{meta.compute.resourceGroupName }}"
- name : check config file exist
stat:
path: /etc/example.config
register: file_status
- name: create local file with file existance status
local_action : copy content = "{{vm_resourcegroup}} {{ansible_hostname}} {{file_status.stdout}}" \n dest= "{{build_source_dir}}/src/ansible/ansible_file_status{{anisible_hostname}}.csv "
...

local_action: copy content = "{{vm_resourcegroup}} {{ansible_hostname}} {{file_status.stdout}}" \n dest= "{{build_source_dir}}/src/ansible/ansible_file_status{{anisible_hostname}}.csv "
You have a misunderstanding about stat: -- it does not have a .stdout property, but rather an .stat property with several sub-fields
Also, your local_action appears to have a stray \n in it, perhaps you meant to include that inside the double-quotes?

Related

Codeception doesn't override parameter in Gitlab

I have this issue
*********** codeception.yml ***************
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
#amount customer per product
amountPerProduct: 1
wishCountry: 1
wishProduct: 0
I am using the param like this:
$countryIndex = Configuration::config()['wishCountry'];
but on the console I calling the test like this:
codecept run tests/acceptance/ChangeCest.php --env chrome --xml --html -o "wishProduct:55"
I get this error:
enter image description here
QUESTION: how can I override this config in Gitlab?
Thank you
Not entirely sure if this solves your problem but for Codeception v5 (also my current local version v4.2.1) the option is exploded with : , i.e. with an appended space. I notice in your screenshot the exception does show the key/value without the space in between, but you didn't specify which version of Codeception you're using.
From Config.php:
foreach ($configOptions as $option) {
$keys = explode(': ', $option);
// ^ notice the space here
if (count($keys) < 2) {
throw new \InvalidArgumentException('--override should have config passed as "key: value"');
}
https://github.com/Codeception/Codeception/blob/5.0/src/Codeception/Command/Shared/ConfigTrait.php
i find the answer to my problem.
I store the parameters in a new file params.yml
params.yml
parameters:
WISH_PRODUCT: 55
codeception.yml
params:
- params.yml
acceptance.suite.yml
config:
custom_params:
wishProduct: %WISH_PRODUCT%
AcceptanceTester.php
so in AcceptanceTester I can read the values like this
$custom_params = Configuration::suiteSettings('acceptance', Configuration::config())['modules']['custom_params'];

Python Glob - Get Full Filenames, but no directory-only names

This code works, but it's returning directory names and filenames. I haven't found a parameter that tells it to return only files or only directories.
Can glob.glob do this, or do I have to call os.something to test if I have a directory or file. In my case, my files all end with .csv, but I would like to know for more general knowledge as well.
In the loop, I'm reading each file, so currently bombing when it tries to open a directory name as a filename.
files = sorted(glob.glob(input_watch_directory + "/**", recursive=True))
for loop_full_filename in files:
print(loop_full_filename)
Results:
c:\Demo\WatchDir\
c:\Demo\WatchDir\2202
c:\Demo\WatchDir\2202\07
c:\Demo\WatchDir\2202\07\01
c:\Demo\WatchDir\2202\07\01\polygonData_2022_07_01__15_51.csv
c:\Demo\WatchDir\2202\07\01\polygonData_2022_07_01__15_52.csv
c:\Demo\WatchDir\2202\07\01\polygonData_2022_07_01__15_53.csv
c:\Demo\WatchDir\2202\07\01\polygonData_2022_07_01__15_54.csv
c:\Demo\WatchDir\2202\07\01\polygonData_2022_07_01__15_55.csv
c:\Demo\WatchDir\2202\07\05
c:\Demo\WatchDir\2202\07\05\polygonData_2022_07_05__12_00.csv
c:\Demo\WatchDir\2202\07\05\polygonData_2022_07_05__12_01.csv
Results needed:
c:\Demo\WatchDir\2202\07\01\polygonData_2022_07_01__15_51.csv
c:\Demo\WatchDir\2202\07\01\polygonData_2022_07_01__15_52.csv
c:\Demo\WatchDir\2202\07\01\polygonData_2022_07_01__15_53.csv
c:\Demo\WatchDir\2202\07\01\polygonData_2022_07_01__15_54.csv
c:\Demo\WatchDir\2202\07\01\polygonData_2022_07_01__15_55.csv
c:\Demo\WatchDir\2202\07\05\polygonData_2022_07_05__12_00.csv
c:\Demo\WatchDir\2202\07\05\polygonData_2022_07_05__12_01.csv
For this specific program, I can just check if the file name contains.csv, but I would like to know in general for future reference.
Line:
files = sorted(glob.glob(input_watch_directory + "/**", recursive=True))
replace with the line:
files = sorted(glob.glob(input_watch_directory + "/**/*.*", recursive=True))

Finding and replacing special chars in a file

I'm trying to find and replace some special chars in a file encoded in ISO-8859-1, then write the result to a new file encoded in UTF-8:
package inv
class MigrationScript {
static main(args) {
new MigrationScript().doStuff();
}
void doStuff() {
def dumpfile = "path to input file";
def newfileP = "path to output file"
def file = new File(dumpfile)
def newfile = new File(newfileP)
def x = [
"þ":"ş",
"ý":"ı",
"Þ":"Ş",
"ð":"ğ",
"Ý":"İ",
"Ð":"Ğ"
]
def r = file.newReader("ISO-8859-1")
def w = newfile.newWriter("UTF-8")
r.eachLine{
line ->
x.each {
key, value ->
if(line.find(key)) println "found a special char!"
line = line.replaceAll(key, value);
}
w << line + System.lineSeparator();
}
w.close()
}
}
My input file content is:
"þ": "ý": "Þ":" "ð":" "Ý":" "Ð":"
Problem is my code never finds the specified characters. The groovy script file itself is encoded in UTF-8. I'm guessing that may be the cause of the problem, but then I can't encode it in ISO-8859-1 because then I can't write "Ş" "Ğ" etc in it.
I took your code sample, run it with an input file encoded with charset ISO-8859-1 and it worked as expected. Can you double check if your input file is actually encoded with ISO-8859-1? Here is what I did:
I took file content from your question and saved it (using SublimeText) to a file /tmp/test.txt using Save -> Save with Encoding -> Western (ISO 8859-1)
I checked file encoding with following Linux command:
file -i /tmp/test.txt
/tmp/test.txt: text/plain; charset=iso-8859-1
I set up dumpfile variable with /tmp/test.txt file and newfile variable to /tmp/test_2.txt
I run your code and I saw in the console:
found a special char!
found a special char!
found a special char!
found a special char!
found a special char!
found a special char!
I checked encoding of the Groovy file in IntelliJ IDEA - it was UTF-8
I checked encoding of the output file:
file -i /tmp/test_2.txt
/tmp/test_2.txt: text/plain; charset=utf-8
I checked the content of the output file:
cat /tmp/test_2.txt
"ş": "ı": "Ş":" "ğ":" "İ":" "Ğ":"
I don't think it matters, but I have used the most recent Groovy 2.4.13
I'm guessing that your input file is not encoded properly. Do double check what is the encoding of the file - when I save the same content but with UTF-8 encoding, your program does not work as expected and I don't see any found a special char! entry in the console. When I display contents of ISO-8859-1 file I see something like that:
cat /tmp/test.txt
"�": "�": "�":" "�":" "�":" "�":"%
If I save the same content with UTF-8, I see the readable content of the file:
cat /tmp/test.txt
"þ": "ý": "Þ":" "ð":" "Ý":" "Ð":"%
Hope it helps in finding source of the problem.

Not able to get a value from yml file in groovy

I have stored an entire yaml file into a Map yamlConfig which i am able to print and check.
The Output when I run the code: yamlConfig.each{ k, v -> println "${k}:${v}" } is:
Host:localhost
Port:10000
application:[name:ABC, preferences:[UUID:d3f3278e, server:localhost:2222]]
services:[[name:XYZ, instances:1, start:true]]
dataSets:[[name:ABC], [name:XYZ]]
Now, I am trying to fetch a value from Map using following code:
println yamlConfig.get("services").getAt("name")
However, I am getting the value: [XYZ]. Instead I need the result as XYZ, without square brackets.
Yml file I am using:
Host: localhost
Port: 10000
application:
name: ABC
preferences:
UUID: d3f3278e
server: localhost:2222
services:
- name: XYZ
instances: 1
start: true
data:
- name: ABC
- name: XYZ
This is because of the - character placed before your name property. It makes the yaml parser treat what's inside the services section as an array.
When you ask for the name property doing yamlConfig['services']['name'] groovy gives you all the macthing properties of array items in the services array, and it can only return them in another array.
So either remove the - or use yamlConfig['services'][0]['name'].
yamlConfig.get("services")
return a list but not a service, therefore when you apply getAt to the returned list of services it returns a list of names.
yamlConfig.get("services").getAt('name')
actually does
yaml['services'].collect { it['name'] }
so in order to get a name of a certain service you need to do something like this:
println yaml['services'][0]['name']

fread error? invalid file identifier

while executing, I have this error ?? Error using ==> fread
Invalid file identifier. Use fopen to generate a valid file identifier. fid is equal to -1 but the file does exist.
what shall I do?
seq=dir('C:\Windows\system32\config\systemprofile\Desktop\pfe\code final version 1\nor\info');
N=[];
for i = 3 : length(seq)
disp(seq(i).name)
cd 'C:\Windows\system32\config\systemprofile\Desktop\pfe\code final version 1\nor\info'
fin = fopen('seq(i).name','r');
[x,count]=fread(fin,'char=>char');
cd 'C:\Windows\system32\config\systemprofile\Desktop\pfe\code final version 1'
M=fichier(fin,x);
N=[N;M];
end
xlswrite('info.xls',N);
As you put '' around the name, you are trying to open a file called seq(i).name, remove the '' and you are using the variable named seq(i).name
fin = fopen(seq(i).name,'r');

Resources