Line not updating in a file using win_lineinfile - ansible

I'm trying to update an appsettings.json file on each one of my nodes. When I run my playbook, I'm getting a changed state for all my nodes but when I check the node I don't see any changes to the line. I'm also trying to update the string with the hostname but that doesn't seem to be working very well.
Here is an example of what I'm trying to do:
- name: Replace line in appsettings.json
win_lineinfile:
path: C:\BatchJobs\appsettings.json
regex: '"SrcDir:"\s*:\s*"[^"]*",'
line: '"SrcDir:" "\\\\<ip-address>\\D$\\ETLs\\{{ansible_hostname}}\\",'
Here is a snippet of the appsettings:
},
"DirectorySettings": {
"FileSettingsConfigFile": "\\\\10.34.0.202\\D$\\Config\\<config>",
"SrcDir": "\\\\<ip-Address>\\D$\\ETLs\\ETL01\\",
"DestinationDir": "D:\\DestinationDir\\",
"ShipDir": "D:\\ShipDir\\",
"FailedDir": "\\\\10.34.0.202\\D$\\FailedDir\\",
"DBQueriesBaseFolder": "./config/JobQueries/",
"FileStatJobConfig": "./config/FileStat/"

The issue was because of the documentation: https://docs.ansible.com/ansible/latest/modules/win_lineinfile_module.html#examples
I was able to get it working by changing to:
- name: Replace line in appsettings.json
win_lineinfile:
path: C:\BatchJobs\appsettings.json
regexp: '"SrcDir:"\s*:\s*"[^"]*",'
line: '"SrcDir:" "\\\\<ip-address>\\D$\\ETLs\\{{ansible_hostname}}\\",'

Related

Rename file using ansible playbook on windows [duplicate]

This question already has answers here:
How to rename/move a file on a remote windows host with ansible?
(4 answers)
Closed last month.
Looking at rename file using ansible playbook on windows with below option, but is leading to below error.
Tried few mix match with " and ' but no luck in getting it sorted.
Kindly suggest correct way of handling on windows machine.
- name: Rename foo.bar
win_command: 'cmd.exe /c rename 'C:\windows\some\path\foo.bar' foo.zzz'
Error its leading to is per below :
^ here This one looks easy to fix. There seems to be an extra unquoted colon in the line and this is confusing the parser. It was only expecting to find one free colon. The solution is just add some quotes around the colon, or quote the entire line after the first colon.
For instance, if the original line was:
copy: src=file.txt dest=/path/filename:with_colon.txt
It can be written as:
copy: src=file.txt dest='/path/filename:with_colon.txt'
Or:
copy: 'src=file.txt dest=/path/filename:with_colon.txt'
Looks like there is no viable option with copy on local machine using win_command.
Ended up implementing same with copy to different file name with win_copy & removing original file with win_file to achieve effective result of renaming file!
- name: Rename foo.bar
win_copy:
src: C:\windows\some\path\foo.bar
dest: C:\windows\some\path\foo.zzz
remote_src: yes
- win_file:
path: C:\windows\some\path\foo.bar
state: absent

Fetching a file in a shell script within a concourse task gives file not found error

My concourse task is something like this:
name: test
plan:
- get: my-repo
- task: my-task
config:
inputs:
- name: my-repo
run:
path: sh
args: [my-repo/examples/run-this.sh]
And the shell script tries to fetch a file in so manner:
CONFIG_FILE=./$name.cfg
When I run the task, concourse throws this error
my-repo/examples/run-this.sh: line xx: can't open name.cfg: no such file
The location of the run-this.sh and name.cfg file are the same. Any pointers will be appreciated!
Even though the two files share the same directory, the dot in ./name.cfg uses current working directory as a reference point - so it's the directory from which the script is called, not the directory in which the script is stored.
The best option to get this to work seems to be to add some intelligence to run-this.sh to locate the name.cfg file based on its own relative location, like this:
#!/bin/bash
SCRIPT_DIR="$(dirname $0)"
CONFIG_FILE="${SCRIPT_DIR}/name.cfg"
(...)

Use Gitlab CI/CD variables in YAML file

I have got a YAML configuration file in which I need to store an API key. As I do not want to commit my API Key to my Repo I figured a Gitlab CI/CD variable would be a good option. I have configured the variable in the Gitlab UI to be:
TOKEN = "123"
My .gitlab.ci.yml file contains:
image:
name: xxx
variables:
P_TOKEN: ${TOKEN}
And my YAML file has:
spec:
command: test.sh
env_vars:
- TOKEN=${P_TOKEN}
But it just sets TOKEN in the YAML file to ${P_TOKEN} instead of the contents of ${P_TOKEN}. If I echo out my variables in my CI/CD pipeline it is set correctly.
So your YAML file is actually a template for a YAML file. You'd need to run some sort of template engine on top of it to have your ${P_TOKEN} placeholder replaced.
A very simple example using sed that might suffice for your use case:
sed -i "s/\${P_TOKEN}/$TOKEN/" your_file.yaml

Modifying PATH variable via Ansible

Below is the bash profile of the root user in my system.
[root#newvm-2 ~]# cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
I want to write a ansible code that will add the below path at the end of PATH in the bash_profile of root user:
/usr/edb/efm
eg: the new PATH will look like
PATH=$PATH:$HOME/bin:/usr/edb/efm
- lineinfile:
regexp: '^(PATH=.*)$'
backrefs: yes
line: '\1:/user/edb/efm'
path: '/root/.bashrc'
lineinfile manipulates individual lines in files.
regexp: '^(PATH=.*)$' says: Find the last line beginning with PATH= and remember the line. This also specifies which line will be changed.
backrefs: yes means that I can use that remembered line in the line I set.
line: '\1:/user/edb/efm' means take the first group the regex matched (the entire line in the () ) and write :/user/edb/efm behind it.
path: '/root/.bashrc' just specifies which file is manipulated, I guessed here, you probably know the right one.
This is just a single task, if you want a whole play for this just prepend
- hosts: all
tasks:
and indent everything once

ansible before insert not creating duplicate record in the second run?

in ansible I'm trying to add a line before '' infile. iam using insert before. it is working as expected. but when ever i ran second time its not creating the duplicate entry of " '" this is also as expected but my concern is i havent mentioned any where not to duplicate in second run why is noot adding the line again
- name: Change the Log Level for log in path/logback.xml
lineinfile:
dest: "path/logback.xml"
line: ' <logger name="org.log" level="DEBUG"/>'
insertbefore: '</configuration>'
backup: yes
lineinfile module doesn't add duplicates, it ensures that specified line is present in the file.
If line is not there, Ansible will add it before insertbefore pattern.
So, if the line is added on the first run, Ansible will do nothing on the second run – it's idempotence in action.

Resources