'Ansible not changing into directory to run command(s)
I'm using Ansible 2.8.4-1.el7
in order to automate some tasks. Since I'm starting learning the tool, I'm going through the official documentation most of the times (more specifically, I'm following these examples).
I'm just trying to execute a script that manages the Tomcat instance(s):
- name: Stop service
command: "{{ script_manager }} stop"
args:
chdir: "{{ path_base }}/bin/"
I'm setting those variables beforehand:
path_base: /some/path/tomcat9_dev/
script_manager: tomcat9_dev
...but I'm still getting this error message:
TASK [tomcat : Stop service] ***************************************************
fatal: [tld.server.name]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "cmd": "tomcat9_dev stop", "msg": "[Errno 2] No such file or directory", "rc": 2}
It looks like it's not changing into that directory first to execute the command. Any clues?
I know the location exists, I can SSH into the server(s) and
cd
into it.
UPDATE: If I do "{{ path_base }}/bin/{{ script_manager }} stop"
it works fine. In any case, what's a better approach?
Solution 1:[1]
The current directory is not part of the PATH in *nix OS. Changing dir to the correct location is not enough.
If you really want to call your script like this, you have to add {{ path_base }}/bin/
to your user's PATH.
Meanwhile, you can take advantage of chdir
by calling your script relative to the current dir:
- name: Stop service
command: "./{{ script_manager }} stop"
args:
chdir: "{{ path_base }}/bin/"
Furtermore, you are IMO on the wrong track: tomcat should be a registered service on the machine and you should manipulate it with the relevant modules: either the generic service
or one of the specific systemd
, runit
,sysvinit
...
Example with service:
- name: Make sure tomcat_dev is stopped
service:
name: tomcat_dev
state: stopped
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |