Fetching current path of the file using Ansible - path

In Ansible, i need to take a parent directory or current path of file automatically, in some of chef framework we have chef::config,it ll take automatically file path.
e.g:
/tasks/main.yml:
name: execute cmd
command: python file.py
If I have my "file.py" in a tasks folder, I need to execute my playbook from that path only like: /home/playbook/roles/sample/tasks ,then only it taking a script file and running.Suppose when i run from root path i mean /home/playbook/ path,it shows "No such file or directory"
I have tried lookup(env,HOME), It just takes /home alone but not related to that file path.
So to take a current file path how to give in ansible???
Thanks

Answer from comments
script module will copy the file first and then run it. if you use roles, you should place scripts into roles/my_role/files folder.

Related

How to move a file 'to a directory that's on your PATH'?

Im trying to install a JSON formatter for Cucumber but am having trouble configuring it. The steps (listed here) go like this:
1. Download cucumber-json-formatter-darwin-amd64 and rename it to cucumber-json-formatter
2. Move it to a directory that's on your PATH
3. Make it executable with chmod +x cucumber-json-formatter
4. Verify that you can run it: cucumber-json-formatter --help
I have the file downloaded and renamed correctly. However, I am stuck on the second step of moving it to a directory thats on my PATH.
Doing some research, I know what the folder structure looks like but I'm not sure exactly what the step is instructing. How would I achieve this step? Can it be in ANY directory on my PATH? I am currently using a Mac if that makes any difference for the solution.
Move it to a directory that's on your PATH
PATH refers to the machine's environment variable named PATH. Any time the OS is asked to execute something PATH is searched.
On Windows open System Properties dialog, click Environment Variables button and Path is listed there. You can add a new entry for the location of cucumber-json-formatter or you can move it to an existing Path entry.

Dockerfile: COPY a file relative to local home

In a Dockerfile I want to copy a file relative to my local home inside the images's home.
So I have tried many variations of:
COPY "~/.m2/settings.xml" "$HOME/.m2/settings2.xml"
But I get errors like
COPY failed: stat /var/lib/docker/tmp/docker-builder635958043/~/.m2/settings.xml: no such file or directory
How can I copy a file relative to my local home inside the image?
The source for the COPY command is the build context. The build context is included in the last argument to the docker build command, often a . which means the current directory. This location is sent to the docker engine before running any steps of the Dockerfile, using a tar file, in the default/classic builder. Therefore, to keep builds running fast, you want to keep this directory small by not sending over the entire hard drive contents. This is even more important when building locally since you could potentially start sending files recursively if you were to include docker's temporary directory in the folders being sent.
All this means you should move any files you want to include in the COPY source parameter to be inside the build context, typically the same location as your Dockerfile.

Running container can't find the file that it has created to /home/user/ directory

I hope you are having a great day!
I'm new to docker. I think my problem is related to docker's directory tree
My app writes to a file to /home/user directory and then after some time reads that file again.
I got this error from my app.
[error] a.a.OneForOneStrategy - /home/user/bkjw_eqvfohygvkaxoxc-small.jpg
java.nio.file.NoSuchFileException: /home/user/bkjw_eqvfohygvkaxoxc-small.jpg
My dockerized app is unable to create the file and read. I'm thinking that the Docker considers the directory /home/user/ as a absolute directory of host.
I thought that the container would write to /home/user directory within the container's directory tree.
So the question is :
How can I specify the path to write the file inside the containers directory tree?
Your understanding about the directory tree is correct. Application running inside a docker container would write to /home/user/ in the container's directory tree.
Your issue seems to be with permissions, your java application probably doesn't have the rights to write to /home/user/ within the container. Either you should change the ownership/rights of the directory you're wanting to write in, or a simple solution I did in such case was to create the directory I wanted to write in, within the java code.
like:
// Create volume directories explicitly so that they are created with correct owner
Files.createDirectories(Paths.get(dirPath));
You can set dirPath String to something like /home/user/mydir IF your requirement is not to write in /home/user/ specifically.

dot sourcing with relative paths in powershell

I am trying to figure out how to use relative paths for Powershell scripts. I have dot sourced with absolute paths, but the scripts that I am writing may end up in a different base directory so I need to make sure the path is relative so it can be picked up. How can I do that?
So far I have tried:
. .\scripts\variables.ps1
That always throws this exception:
The term '.\scripts\variables.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program...
That lets me know it can't find my script? So, what am I doing wrong?
You can use : . $PSScriptRoot\scripts\variables.ps1
Here $PSScriptRoot is the path of directory of the running script.
This is not what the OP asked for but may be useful for others who are searching:
If you need to traverse up, you can use . $PSScriptRoot\..\scripts\variables.ps1
This works for structures such as:
root
scripts/shared directory
directory your script is executing in
If you know that your script directory structure is going to remain the same, you could use $PWD; eg:
. "$PWD\scripts\variables.ps1"
The above assumes that your script (the calling script) is in the same directory that contains the scripts directory.
Also, the assumption made here is that you're checking out/downloading all your scripts in the same structure, but as you put it, they may end up being in a different base directory.

Relative paths on Xcode scripts

I'm experimenting running scripts with Xcode and have got a couple of questions:
1) Xcode says to drag n drop the script into the run script section but that creates an absolute path: /Users/Me/Desktop/Project/etc. which is obviously no use if somebody else or a CI machine checks out the code. How to specify a relative path?
2) There's a permission denied error during the build when the script gets executed.
(I'm using scripts off here to experiment with https://gist.github.com/sekati/3172554)
"$SRCROOT" gives the project folder:
i.e: Users/yourUserName/MyProject
but if you have a workspace folder with multiple projects inside:
i.e: Users/yourUserName/MyWorkspace/MyProject
And you need just the workspace folder, use 2 dots:
"../SomeFodler"
While someFolder will be created in the workspace folder.
Simple as that.
1) Edit your script with the following:
Root of the project: ${SRCROOT}
Root of the build: ${CONFIGURATION_BUILD_DIR}
2) Press ⌘+8, click Build and read the error.
1) In an Xcode project of mine, I have the following script that generates the source code documentation. As you can see each line of the script uses a relative path. I don't even need to use ${SRCROOT}.
# change directory because Doxyfile is configured with a relative input path ".."
cd doxygen
# clean the directory
rm -rf html
# generate docs
/opt/local/bin/doxygen Doxyfile
# open the html documentation
open html/index.html
2) The reason for the "permission denied" error may be that you have not set the executable bit on the script. On the console, type this command to set the executable bit, then try again to run the script.
chmod +x /path/to/xcode-build-bump.sh

Resources