Execute environment file in ansible - ansible

I am trying to setup a playbook which will execute the env file of a service before running the command to stop the service.
Note that stop or start service command will only work after execute that env file.
The command to execute the env file is . ./.env_file_name or ksh .env_file_name
I am not able to execute the file using the above command in command module and shell module.
How to execute the above env file in ansible ?
How to run the stop command after executing the env file ?

If env file contains some environment variables that you need to set before running stop, then you can read the file, store results in a variable, then use environment keyword to provide them to the task, that actually stops the service.
Alternatively, you can add multiple commands in shell module at once, like so:
- name: Example for a shell module
shell:
cmd: |
./.env_file_name
./stop.sh (insert command to stop the service here)

Related

Run a remote script on a remote node

Easy question.
I have a Python3 script on my remote node in the path ~/setup/inventory.py, and I am trying to execute it remotly.
So I took a look at the script resource, which is searching on my local machine for the script.
How do I get Ansible to run my remote Python3 script. Executing it normally would look like os python3 /setup/inventory.py ansible vscode bash
Use command or shell modules, depending on your case.
Something like
- name: execute my script
command: python3 /setup/inventory.py ansible vscode bash

How to Set docker container ip to environment variable dynamically on startup?

I want to export docker container hostname as an environment variable which I can later use in my app. In my docker file I call my script "run" as last command
CMD run
The run file is executable and works fine with rest of commands I perform but before them I want to export container hostname to an env. variable as follows
"run" File Try 1
#!/bin/bash
export DOCKER_MACHINE_IP=`hostname -i`
my_other_commands
exec tail -f /dev/null
But when I enter docker container and check, the variable is not set. If I use
echo $DOCKER_MACHINE_IP
in run file after exporting, it shows ip on console when I try
docker logs
I also tried sourcing another script from "run" file as follows
"run" File Try 2
#!/bin/bash
source ./bin/script
my_other_commands
exec tail -f /dev/null
and the script again contains the export command. But this also does not set the environment variable. What I am doing wrong?
When you execute a script, any environment variable set by that script will be lost when the script exits.
But for both the cases you've posted above the environment variable should be accessible for the commands in your scripts, but when you enter the docker container via docker run you will get a new shell, which does not contain your variable.
tl;dr Your exported environment variable will only be available to sub shells of the shell which set the variable. And if you need it when logging in you should source the ./bin/script file.

Docker Ubuntu environment variables

During the build stage of my docker images, i would like to set some environment variables automatically for every subsequent "RUN" command.
However, I would like to set these variables from within the docker conatiner, because setting them depends on some internal logic.
Using the dockerfile "ENV" command is not good, because that cannot rely on internal logic. (It cannot rely on a command run inside the docker container)
Normally (if this were not docker) I would set my ~/.profile file. However, docker does not load this file in non-interactive shells.
So at them moment I have to run each docker RUN command with:
RUN bash -c "source ~/.profile && do_something_here"
However, this is very tedious (and unclean) when I have to repeat this every time I want to run a bash command. Is there some other "profile" file I can use instead.
you can try setting the arg as env like this
ARG my_env
ENV my_env=${my_env}
in Dockerfile,
and pass the 'my_env=prod' in build-args so that you can use the set env for subsequent RUN commands
you can also use env_file: option in docker compose yml file in case of a stack deploy
I had a similar problem and couldn't find a satisfactory solution. What I did was creating a script that would source the variables, then do the operation. I would then rewrite the RUN commands in the Dockerfile to use that script instead.
In your case, if you need to run multiple commands, you could create a wrapper that loads the variables, runs the command given as argument, and include that script in the docker image.

ansible script to set gradle home?

How to set gradle home using ansible script. I have a directory gradle_user_home in RHEL. I'm trying to set gradle home to this directory with ansible script. But ansible script says no such file or directory! Below is my script.
- name: Set gradle environment variable
command: export GRADLE_USER_HOME={{gradle_home}}
My vars file:
gradle_home: "/data1/deployment/gradle_user_home"
First of all, export is not a command (ie there is no /bin/export executable). It is a bash built-in. That's why you get a no such file or directory. You can use it in a shell task, not in a command task.
Secondly, this is not the proper way to set environment variables with Ansible.
You can set environment at the play level like this:
---
- hosts: all
environment:
GRADLE_USER_HOME: "{{gradle_home}}"
Or at the task level like this:
- name: Execute gradle build
shell: gradle build
environment:
GRADLE_USER_HOME: "{{gradle_home}}"
You can not export an environment variable in a single command task, because each command task is run in its own shell. After the command finishes the environment variable is lost.
You have to use the shell action to
set the environment variable and
run the command.
Example:
- name: Run the command
shell: >-
export GRADLE_USER_HOME={{gradle_home}}
my_command_which_requires_gradle_user_home

File not created in ansible

I have installed ansible on one machine and trying to execute commands on another (remote) machinge.
Ansible successfully installed
Able to reach all hosts (local and remote). Tested with
ansible all -m ping
This was successful
Trying to execute a simple command again
ansible all -a 'echo "hello world" > ~/test'
Executed successfuly. But the file test is not created.
Cannot find the reason why?
Executing a command via ansible -a is equivalent to the command module, see
command module.
It is not processed via shell, therefore, >> (as well as other redirection operators) and $HOME are not available
In your case I would use
ansible -m 'shell' --args 'echo "hello world">>/home/ansibleremoteuser/test' all
In this case you would use the shell module which allows redirections.

Resources