How to empty a directory using ansible - ansible

I have some directories and files inside a directory called dir1
I am trying to empty that directory using the below code
- name: Clean path
file:
state: absent
path: "/home/location/dir1"
But it is deleting the dir1 itself, i would like to empty it and keep this dir1, what am i missing here, any help on this would be appreciated.

You can try to use a command instead.
- name: Clean path
command: rm -r /home/location/dir1/*

To empty a directory you can use the shell module to run rm with a fileglob:
- name: empty directory
shell: rm -r /home/location/dir1/*
Note that if your directory contains hidden dotfiles, you'll need to explicitly remove those as well:
- name: empty directory containing hidden files
shell: rm -r /home/location/dir1/* /home/location/dir1/.*
Your provided task isn't doing what you want because you're declaring that the directory should be absent, so it gets completely removed rather than emptied.
The command module won't work for the rm because it will not expand file globs, resulting in an error like:
rm: cannot remove '/home/location/dir1/*': No such file or directory

Why not remove the directory with the same task you're using, and re-add it immediately after using the same module? Seems like the most straightforward way to me. You could do some stuff with fileglob and with_items but that seems like over complicating a simple task.
- name: Remove folder
file:
state: absent
path: "/home/location/dir1"
- name: Re-make folder
file:
state: directory
path: "/home/location/dir1"

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"
(...)

Jenkinsfile stash subdirectory and files exclude parent directory

I have got a directory structure that I want to stash using Jenkinsfile. The directory structure is build/demo/**/*. I want to stash just the sub directory and all files/folders underneath it, i.e that is demo/**/*.
So when I unstash. I would want to get demo as parent folder and all the folders and files underneath it.
I have tried the following ant-style include patterns. But I got the same undesirable result every time.
stash includes: "**/demo/**", name: 'demoBuild'
stash includes: "build/demo/**", name: 'demoBuild'
Results from above pattern:
build/demo/**/*
This might get you what you want:
stash includes: "build/demo/**", name: 'demoBuild'
...
unstash name: 'demoBuild'
sh "mv build/demo . && rmdir build/"

How to extract folder name as a variable in bash

I have directory
MainProject/src/
My script which I am calling test.sh run at /MainProject/ and here is some part of the sript:
dotnet restore src/*.sln
dotnet msbuild -t:publish src/*.sln -p:Configuration=Release
For this command, I want MainProject.Test as variable VAR:
dotnet vstest VAR/bin/Release/netcoreapp1.1/VAR.dll
or something like this:
dotnet vstest {src/*.Test}/bin/Release/netcoreapp1.1/{src/*Test}.dll
Which contains these files and folders:
Files:
project.sln
somescript.sh
Folders:
MainProject.Test
MainProject.Host
What I need to do is fetch MainProject.Test folder name and set it to a variable, but I also need it to be templatized where I can set it to a variable using something like *.Test
The reason for this is that I need the script parametrized because there are
MainProject2
MainProject3
MainProjectx
using the same naming convention.
The current directory is in $PWD. That's fully-qualified; to remove everything from the beginning up to the last / would be ${PWD##*/}, using a parameter expansion.
Thus, to extract the last piece of the current working directory and add .Test as a suffix:
result=${PWD##*/}.Test

scp, inconsistency for file structure preservation

My task: collect log files from several servers.
Server file structure: "/remote/path/dir/sub-dirs/files.log", which
is the same on all servers. (All servers have the same set of
"sub-dirs", absence could happen, and of course "files.log" names
differ)
Local file structure: "/local/path/logs"
After copy I would like to have
"/local/path/logs/dir/sub-dirs/files.log"
Method (in a whlile loop for servers): scp -r
$SERVERS:/remote/path/dir /local/path/logs
Problem: For reasons I don't understand, the first scp command
ignores the "dir" folder, I get "/local/path/logs/sub-dirs/files.log"
But following scp commands gives me what I intended
"/local/path/logs/dir/sub-dirs/files.log"
Why is this happening and how should I fix/get around it?
Thanks!
Why is this happening [...]
In the command scp -r path/to/source dest:
If dest doesn't exist, the dest directory will be created, and path/to/source/* will be copied into it. For example if you have path/to/source/X then dest/X will be created.
If dest is a directory, then dest/source will be created, and the path/to/source/* will be copied into it. For example if you have path/to/source/X then dest/source/X will be created.
[...] and how should I fix/get around it?
Create dest in advance, for example:
mkdir -p /local/path/logs
scp -r $SERVERS:/remote/path/dir /local/path/logs

Resources