Running Ansible playbook with ant commands in Jenkins
up vote
0
down vote
favorite
I'm making a playbook to automatize a environment installation. At the moment I'm testing some scripts made by a co-worker and I just want jenkins to run ant commands to check if the scripts structure are correct. This scripts are located in the target machine, since that we just want to test them and there is no need to copy files from a machine to another if the scripts are not correct.
In my Jenkins I configured the jdk and ant version recomended to deploy the requested environment after create a parameterized fresstyle job. My task to run is:
---
- name: compile all
shell: ant all
args:
chdir: " env_home /Project/scripts"
- name: Create project tables
shell: ant create-project-tables
args:
chdir: " env_home /Project/scripts"
This is the first role containing at least one ant command to run on target. Ho ever jenkins always returns
TASK [build_all : compile all] *****************************************
task path: /opt/testes/env-deployment/roles/build_all/tasks/main.yml:2
[WARNING]: when statements should not include jinja2 templating delimiters such as or % %. Found: buildPrepaidAll
fatal: [srv1]: FAILED! => "changed": true, "cmd": "ant all", "delta": "0:00:00.003675", "end": "2018-11-07 15:55:15.917745", "msg": "non-zero return code", "rc": 127, "start": "2018-11-07 15:55:15.914070", "stderr": "/bin/sh: ant: command not found", "stderr_lines": ["/bin/sh: ant: command not found"], "stdout": "", "stdout_lines":
Jenkins does not complain about anything else, just ant command not found and if I type ant all in the same folder the command runs
jenkins ant ansible jenkins-plugins
add a comment |
up vote
0
down vote
favorite
I'm making a playbook to automatize a environment installation. At the moment I'm testing some scripts made by a co-worker and I just want jenkins to run ant commands to check if the scripts structure are correct. This scripts are located in the target machine, since that we just want to test them and there is no need to copy files from a machine to another if the scripts are not correct.
In my Jenkins I configured the jdk and ant version recomended to deploy the requested environment after create a parameterized fresstyle job. My task to run is:
---
- name: compile all
shell: ant all
args:
chdir: " env_home /Project/scripts"
- name: Create project tables
shell: ant create-project-tables
args:
chdir: " env_home /Project/scripts"
This is the first role containing at least one ant command to run on target. Ho ever jenkins always returns
TASK [build_all : compile all] *****************************************
task path: /opt/testes/env-deployment/roles/build_all/tasks/main.yml:2
[WARNING]: when statements should not include jinja2 templating delimiters such as or % %. Found: buildPrepaidAll
fatal: [srv1]: FAILED! => "changed": true, "cmd": "ant all", "delta": "0:00:00.003675", "end": "2018-11-07 15:55:15.917745", "msg": "non-zero return code", "rc": 127, "start": "2018-11-07 15:55:15.914070", "stderr": "/bin/sh: ant: command not found", "stderr_lines": ["/bin/sh: ant: command not found"], "stdout": "", "stdout_lines":
Jenkins does not complain about anything else, just ant command not found and if I type ant all in the same folder the command runs
jenkins ant ansible jenkins-plugins
1
If I understand the problem correctly, your Jenkins job is configured to execute an ansible-playbook which shells out ant calls? Is there a reason to not be using the Jenkins ant plugin (e.g. invoke ant as a build step)? The error is explicit, ant is not available in the PATH for ansible.
– masseyb
Nov 8 at 17:37
But ant is there. If I runantin my target machine ant would give a error because there was no build.xml on the folder, but would work nontheless. And ant env variable is set and also available in PATH. But you gave me an idea. /if the target machine has all set including ant, env var and PATH, then might be the source of the bash_profile. Gonna test it and will give feedback
– Fábio Caramelo
Nov 9 at 10:26
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm making a playbook to automatize a environment installation. At the moment I'm testing some scripts made by a co-worker and I just want jenkins to run ant commands to check if the scripts structure are correct. This scripts are located in the target machine, since that we just want to test them and there is no need to copy files from a machine to another if the scripts are not correct.
In my Jenkins I configured the jdk and ant version recomended to deploy the requested environment after create a parameterized fresstyle job. My task to run is:
---
- name: compile all
shell: ant all
args:
chdir: " env_home /Project/scripts"
- name: Create project tables
shell: ant create-project-tables
args:
chdir: " env_home /Project/scripts"
This is the first role containing at least one ant command to run on target. Ho ever jenkins always returns
TASK [build_all : compile all] *****************************************
task path: /opt/testes/env-deployment/roles/build_all/tasks/main.yml:2
[WARNING]: when statements should not include jinja2 templating delimiters such as or % %. Found: buildPrepaidAll
fatal: [srv1]: FAILED! => "changed": true, "cmd": "ant all", "delta": "0:00:00.003675", "end": "2018-11-07 15:55:15.917745", "msg": "non-zero return code", "rc": 127, "start": "2018-11-07 15:55:15.914070", "stderr": "/bin/sh: ant: command not found", "stderr_lines": ["/bin/sh: ant: command not found"], "stdout": "", "stdout_lines":
Jenkins does not complain about anything else, just ant command not found and if I type ant all in the same folder the command runs
jenkins ant ansible jenkins-plugins
I'm making a playbook to automatize a environment installation. At the moment I'm testing some scripts made by a co-worker and I just want jenkins to run ant commands to check if the scripts structure are correct. This scripts are located in the target machine, since that we just want to test them and there is no need to copy files from a machine to another if the scripts are not correct.
In my Jenkins I configured the jdk and ant version recomended to deploy the requested environment after create a parameterized fresstyle job. My task to run is:
---
- name: compile all
shell: ant all
args:
chdir: " env_home /Project/scripts"
- name: Create project tables
shell: ant create-project-tables
args:
chdir: " env_home /Project/scripts"
This is the first role containing at least one ant command to run on target. Ho ever jenkins always returns
TASK [build_all : compile all] *****************************************
task path: /opt/testes/env-deployment/roles/build_all/tasks/main.yml:2
[WARNING]: when statements should not include jinja2 templating delimiters such as or % %. Found: buildPrepaidAll
fatal: [srv1]: FAILED! => "changed": true, "cmd": "ant all", "delta": "0:00:00.003675", "end": "2018-11-07 15:55:15.917745", "msg": "non-zero return code", "rc": 127, "start": "2018-11-07 15:55:15.914070", "stderr": "/bin/sh: ant: command not found", "stderr_lines": ["/bin/sh: ant: command not found"], "stdout": "", "stdout_lines":
Jenkins does not complain about anything else, just ant command not found and if I type ant all in the same folder the command runs
jenkins ant ansible jenkins-plugins
jenkins ant ansible jenkins-plugins
asked Nov 8 at 12:23
Fábio Caramelo
277
277
1
If I understand the problem correctly, your Jenkins job is configured to execute an ansible-playbook which shells out ant calls? Is there a reason to not be using the Jenkins ant plugin (e.g. invoke ant as a build step)? The error is explicit, ant is not available in the PATH for ansible.
– masseyb
Nov 8 at 17:37
But ant is there. If I runantin my target machine ant would give a error because there was no build.xml on the folder, but would work nontheless. And ant env variable is set and also available in PATH. But you gave me an idea. /if the target machine has all set including ant, env var and PATH, then might be the source of the bash_profile. Gonna test it and will give feedback
– Fábio Caramelo
Nov 9 at 10:26
add a comment |
1
If I understand the problem correctly, your Jenkins job is configured to execute an ansible-playbook which shells out ant calls? Is there a reason to not be using the Jenkins ant plugin (e.g. invoke ant as a build step)? The error is explicit, ant is not available in the PATH for ansible.
– masseyb
Nov 8 at 17:37
But ant is there. If I runantin my target machine ant would give a error because there was no build.xml on the folder, but would work nontheless. And ant env variable is set and also available in PATH. But you gave me an idea. /if the target machine has all set including ant, env var and PATH, then might be the source of the bash_profile. Gonna test it and will give feedback
– Fábio Caramelo
Nov 9 at 10:26
1
1
If I understand the problem correctly, your Jenkins job is configured to execute an ansible-playbook which shells out ant calls? Is there a reason to not be using the Jenkins ant plugin (e.g. invoke ant as a build step)? The error is explicit, ant is not available in the PATH for ansible.
– masseyb
Nov 8 at 17:37
If I understand the problem correctly, your Jenkins job is configured to execute an ansible-playbook which shells out ant calls? Is there a reason to not be using the Jenkins ant plugin (e.g. invoke ant as a build step)? The error is explicit, ant is not available in the PATH for ansible.
– masseyb
Nov 8 at 17:37
But ant is there. If I run
ant in my target machine ant would give a error because there was no build.xml on the folder, but would work nontheless. And ant env variable is set and also available in PATH. But you gave me an idea. /if the target machine has all set including ant, env var and PATH, then might be the source of the bash_profile. Gonna test it and will give feedback– Fábio Caramelo
Nov 9 at 10:26
But ant is there. If I run
ant in my target machine ant would give a error because there was no build.xml on the folder, but would work nontheless. And ant env variable is set and also available in PATH. But you gave me an idea. /if the target machine has all set including ant, env var and PATH, then might be the source of the bash_profile. Gonna test it and will give feedback– Fábio Caramelo
Nov 9 at 10:26
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
TL;DR
Use the Ant Plugin or make sure that the ant executable is available in the ansible runtime $PATH.
Ant Plugin solution
Add Invoke Ant as a Build step of in your jobs configuration (Jenkins Ant Plugin):



Note: this example is running on master. The ant executable is that which is defined in the Global Tool Configuration. ant is not set in the jenkins users $PATH, jenkins uses the ant executable configured for the job.

If Jenkins is configured to build on remote nodes and if the ant installation is not available in the same location defined in the Jenkins Global Tool Configuration you need to override the ant executable location in the Node Properties > Tool Locations of the node configuration ...


Ansible solution
To understand this solution you need to understand the error. Ref. your (redacted) comment:
But ant is there. If I run ant in my target machine ant would give a error because there was no build.xml on the folder, but would work nontheless. And ant env variable is set and also available in PATH. But you gave me an idea. /if the target machine has all set including ant, env var and PATH, then might be the source of the bash_profile. ...
Ansible connections are non-interactive, you can see this in the debug trace when using -vvvv with your ansible commands, a redacted example:
<172.28.128.13> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/bmassey/.ansible/cp/881b67b3d5 172.28.128.13 '/bin/sh -c '...'
.bash_profile commands are not executed during non-interactive logins. From man bash:
An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.
...
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
You need to ensure that the ant executable is available in the ansible executables $PATH, how you do this is irrelevant, i.e. editing a profile from which commands are executed during non-interactive shell logins or setting the environment with ansible (https://docs.ansible.com/ansible/devel/user_guide/playbooks_environment.html)
- hosts: localhost
environment:
PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
Without ant in the ansible executables $PATH:
With ant in the ansible executables $PATH set using ansible's environment keyword:
---
- hosts: localhost
environment:
- PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
tasks:
- name: execute ant build
local_action:
module: shell
args: ant
chdir: " ansible_env.HOME /workspace/example"
register: ant_build
- debug: var=ant_build.stdout_lines

Slightly modified ansible-playbook executed through Jenkins with ant in the ansible executables $PATH set using ansible's environment keyword:
---
- hosts: localhost
environment:
- PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
tasks:
- name: execute ant build
local_action:
module: shell
args: ant
chdir: " lookup('env', 'WORKSPACE') "
register: ant_build
...

If its a playbook, invoking or configuring ant will not work. I tried to do it and still not working. The ansible solution is the one suitable for me. if you dont want to use the ansible var to load the env vars then set the ant home in the global environment vars and sending the commandant allwill work
– Fábio Caramelo
Nov 9 at 15:18
@FábioCaramelo not quite sure I understand your comment. I updated my answer with an end-to-end example if that can help.
– masseyb
Nov 9 at 16:38
Just check it. Thanks for your help :)
– Fábio Caramelo
Nov 12 at 16:27
add a comment |
up vote
0
down vote
Thankfully to masseyb I found the problem. In my target machine I have ant installed and created the ANT_HOME and added the ANT_HOME/bin to the PATH.
However because this is declared on bash_profile jenkins does not recognize the command because it does not make the source of bash profile, so I added source ansible_env.HOME /.bash_profile && before the ant command so jenkins user can source the environment variables necessary to run the commands
It is not jenkins that does not recognize theantcommand rather it isansiblethat does not recognize theantcommand. The execution order is jenkins -> ansible -> ant. Provided that the location of your tools are configured in the Global Tools Configuration then jenkins will have no problem finding eitherantoransiblehowever this does not imply that the$PATHset by jenkins is the same$PATHavailable to processes spawned by jenkins. The issue in this case is the connection types established byansible(non-interactive shells) do not execute.bash_profilecommands.
– masseyb
Nov 9 at 13:03
You can remove Jenkins from the equation and the error would persist as it'sansibleand operating system configuration related. With that said, if you're going to use Jenkins then I would suggest using the Ant Plugin rather than using the Ansible Plugin to shell outantexecutions - keep a clear distinction betweenant's role andansible's role in your build process.
– masseyb
Nov 9 at 13:37
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
TL;DR
Use the Ant Plugin or make sure that the ant executable is available in the ansible runtime $PATH.
Ant Plugin solution
Add Invoke Ant as a Build step of in your jobs configuration (Jenkins Ant Plugin):



Note: this example is running on master. The ant executable is that which is defined in the Global Tool Configuration. ant is not set in the jenkins users $PATH, jenkins uses the ant executable configured for the job.

If Jenkins is configured to build on remote nodes and if the ant installation is not available in the same location defined in the Jenkins Global Tool Configuration you need to override the ant executable location in the Node Properties > Tool Locations of the node configuration ...


Ansible solution
To understand this solution you need to understand the error. Ref. your (redacted) comment:
But ant is there. If I run ant in my target machine ant would give a error because there was no build.xml on the folder, but would work nontheless. And ant env variable is set and also available in PATH. But you gave me an idea. /if the target machine has all set including ant, env var and PATH, then might be the source of the bash_profile. ...
Ansible connections are non-interactive, you can see this in the debug trace when using -vvvv with your ansible commands, a redacted example:
<172.28.128.13> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/bmassey/.ansible/cp/881b67b3d5 172.28.128.13 '/bin/sh -c '...'
.bash_profile commands are not executed during non-interactive logins. From man bash:
An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.
...
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
You need to ensure that the ant executable is available in the ansible executables $PATH, how you do this is irrelevant, i.e. editing a profile from which commands are executed during non-interactive shell logins or setting the environment with ansible (https://docs.ansible.com/ansible/devel/user_guide/playbooks_environment.html)
- hosts: localhost
environment:
PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
Without ant in the ansible executables $PATH:
With ant in the ansible executables $PATH set using ansible's environment keyword:
---
- hosts: localhost
environment:
- PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
tasks:
- name: execute ant build
local_action:
module: shell
args: ant
chdir: " ansible_env.HOME /workspace/example"
register: ant_build
- debug: var=ant_build.stdout_lines

Slightly modified ansible-playbook executed through Jenkins with ant in the ansible executables $PATH set using ansible's environment keyword:
---
- hosts: localhost
environment:
- PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
tasks:
- name: execute ant build
local_action:
module: shell
args: ant
chdir: " lookup('env', 'WORKSPACE') "
register: ant_build
...

If its a playbook, invoking or configuring ant will not work. I tried to do it and still not working. The ansible solution is the one suitable for me. if you dont want to use the ansible var to load the env vars then set the ant home in the global environment vars and sending the commandant allwill work
– Fábio Caramelo
Nov 9 at 15:18
@FábioCaramelo not quite sure I understand your comment. I updated my answer with an end-to-end example if that can help.
– masseyb
Nov 9 at 16:38
Just check it. Thanks for your help :)
– Fábio Caramelo
Nov 12 at 16:27
add a comment |
up vote
1
down vote
accepted
TL;DR
Use the Ant Plugin or make sure that the ant executable is available in the ansible runtime $PATH.
Ant Plugin solution
Add Invoke Ant as a Build step of in your jobs configuration (Jenkins Ant Plugin):



Note: this example is running on master. The ant executable is that which is defined in the Global Tool Configuration. ant is not set in the jenkins users $PATH, jenkins uses the ant executable configured for the job.

If Jenkins is configured to build on remote nodes and if the ant installation is not available in the same location defined in the Jenkins Global Tool Configuration you need to override the ant executable location in the Node Properties > Tool Locations of the node configuration ...


Ansible solution
To understand this solution you need to understand the error. Ref. your (redacted) comment:
But ant is there. If I run ant in my target machine ant would give a error because there was no build.xml on the folder, but would work nontheless. And ant env variable is set and also available in PATH. But you gave me an idea. /if the target machine has all set including ant, env var and PATH, then might be the source of the bash_profile. ...
Ansible connections are non-interactive, you can see this in the debug trace when using -vvvv with your ansible commands, a redacted example:
<172.28.128.13> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/bmassey/.ansible/cp/881b67b3d5 172.28.128.13 '/bin/sh -c '...'
.bash_profile commands are not executed during non-interactive logins. From man bash:
An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.
...
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
You need to ensure that the ant executable is available in the ansible executables $PATH, how you do this is irrelevant, i.e. editing a profile from which commands are executed during non-interactive shell logins or setting the environment with ansible (https://docs.ansible.com/ansible/devel/user_guide/playbooks_environment.html)
- hosts: localhost
environment:
PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
Without ant in the ansible executables $PATH:
With ant in the ansible executables $PATH set using ansible's environment keyword:
---
- hosts: localhost
environment:
- PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
tasks:
- name: execute ant build
local_action:
module: shell
args: ant
chdir: " ansible_env.HOME /workspace/example"
register: ant_build
- debug: var=ant_build.stdout_lines

Slightly modified ansible-playbook executed through Jenkins with ant in the ansible executables $PATH set using ansible's environment keyword:
---
- hosts: localhost
environment:
- PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
tasks:
- name: execute ant build
local_action:
module: shell
args: ant
chdir: " lookup('env', 'WORKSPACE') "
register: ant_build
...

If its a playbook, invoking or configuring ant will not work. I tried to do it and still not working. The ansible solution is the one suitable for me. if you dont want to use the ansible var to load the env vars then set the ant home in the global environment vars and sending the commandant allwill work
– Fábio Caramelo
Nov 9 at 15:18
@FábioCaramelo not quite sure I understand your comment. I updated my answer with an end-to-end example if that can help.
– masseyb
Nov 9 at 16:38
Just check it. Thanks for your help :)
– Fábio Caramelo
Nov 12 at 16:27
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
TL;DR
Use the Ant Plugin or make sure that the ant executable is available in the ansible runtime $PATH.
Ant Plugin solution
Add Invoke Ant as a Build step of in your jobs configuration (Jenkins Ant Plugin):



Note: this example is running on master. The ant executable is that which is defined in the Global Tool Configuration. ant is not set in the jenkins users $PATH, jenkins uses the ant executable configured for the job.

If Jenkins is configured to build on remote nodes and if the ant installation is not available in the same location defined in the Jenkins Global Tool Configuration you need to override the ant executable location in the Node Properties > Tool Locations of the node configuration ...


Ansible solution
To understand this solution you need to understand the error. Ref. your (redacted) comment:
But ant is there. If I run ant in my target machine ant would give a error because there was no build.xml on the folder, but would work nontheless. And ant env variable is set and also available in PATH. But you gave me an idea. /if the target machine has all set including ant, env var and PATH, then might be the source of the bash_profile. ...
Ansible connections are non-interactive, you can see this in the debug trace when using -vvvv with your ansible commands, a redacted example:
<172.28.128.13> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/bmassey/.ansible/cp/881b67b3d5 172.28.128.13 '/bin/sh -c '...'
.bash_profile commands are not executed during non-interactive logins. From man bash:
An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.
...
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
You need to ensure that the ant executable is available in the ansible executables $PATH, how you do this is irrelevant, i.e. editing a profile from which commands are executed during non-interactive shell logins or setting the environment with ansible (https://docs.ansible.com/ansible/devel/user_guide/playbooks_environment.html)
- hosts: localhost
environment:
PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
Without ant in the ansible executables $PATH:
With ant in the ansible executables $PATH set using ansible's environment keyword:
---
- hosts: localhost
environment:
- PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
tasks:
- name: execute ant build
local_action:
module: shell
args: ant
chdir: " ansible_env.HOME /workspace/example"
register: ant_build
- debug: var=ant_build.stdout_lines

Slightly modified ansible-playbook executed through Jenkins with ant in the ansible executables $PATH set using ansible's environment keyword:
---
- hosts: localhost
environment:
- PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
tasks:
- name: execute ant build
local_action:
module: shell
args: ant
chdir: " lookup('env', 'WORKSPACE') "
register: ant_build
...

TL;DR
Use the Ant Plugin or make sure that the ant executable is available in the ansible runtime $PATH.
Ant Plugin solution
Add Invoke Ant as a Build step of in your jobs configuration (Jenkins Ant Plugin):



Note: this example is running on master. The ant executable is that which is defined in the Global Tool Configuration. ant is not set in the jenkins users $PATH, jenkins uses the ant executable configured for the job.

If Jenkins is configured to build on remote nodes and if the ant installation is not available in the same location defined in the Jenkins Global Tool Configuration you need to override the ant executable location in the Node Properties > Tool Locations of the node configuration ...


Ansible solution
To understand this solution you need to understand the error. Ref. your (redacted) comment:
But ant is there. If I run ant in my target machine ant would give a error because there was no build.xml on the folder, but would work nontheless. And ant env variable is set and also available in PATH. But you gave me an idea. /if the target machine has all set including ant, env var and PATH, then might be the source of the bash_profile. ...
Ansible connections are non-interactive, you can see this in the debug trace when using -vvvv with your ansible commands, a redacted example:
<172.28.128.13> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/bmassey/.ansible/cp/881b67b3d5 172.28.128.13 '/bin/sh -c '...'
.bash_profile commands are not executed during non-interactive logins. From man bash:
An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.
...
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
You need to ensure that the ant executable is available in the ansible executables $PATH, how you do this is irrelevant, i.e. editing a profile from which commands are executed during non-interactive shell logins or setting the environment with ansible (https://docs.ansible.com/ansible/devel/user_guide/playbooks_environment.html)
- hosts: localhost
environment:
PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
Without ant in the ansible executables $PATH:
With ant in the ansible executables $PATH set using ansible's environment keyword:
---
- hosts: localhost
environment:
- PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
tasks:
- name: execute ant build
local_action:
module: shell
args: ant
chdir: " ansible_env.HOME /workspace/example"
register: ant_build
- debug: var=ant_build.stdout_lines

Slightly modified ansible-playbook executed through Jenkins with ant in the ansible executables $PATH set using ansible's environment keyword:
---
- hosts: localhost
environment:
- PATH: '/opt/ant/apache-ant-1.10.5/bin: ansible_env.PATH '
tasks:
- name: execute ant build
local_action:
module: shell
args: ant
chdir: " lookup('env', 'WORKSPACE') "
register: ant_build
...

edited Nov 9 at 17:02
answered Nov 9 at 12:47
masseyb
36718
36718
If its a playbook, invoking or configuring ant will not work. I tried to do it and still not working. The ansible solution is the one suitable for me. if you dont want to use the ansible var to load the env vars then set the ant home in the global environment vars and sending the commandant allwill work
– Fábio Caramelo
Nov 9 at 15:18
@FábioCaramelo not quite sure I understand your comment. I updated my answer with an end-to-end example if that can help.
– masseyb
Nov 9 at 16:38
Just check it. Thanks for your help :)
– Fábio Caramelo
Nov 12 at 16:27
add a comment |
If its a playbook, invoking or configuring ant will not work. I tried to do it and still not working. The ansible solution is the one suitable for me. if you dont want to use the ansible var to load the env vars then set the ant home in the global environment vars and sending the commandant allwill work
– Fábio Caramelo
Nov 9 at 15:18
@FábioCaramelo not quite sure I understand your comment. I updated my answer with an end-to-end example if that can help.
– masseyb
Nov 9 at 16:38
Just check it. Thanks for your help :)
– Fábio Caramelo
Nov 12 at 16:27
If its a playbook, invoking or configuring ant will not work. I tried to do it and still not working. The ansible solution is the one suitable for me. if you dont want to use the ansible var to load the env vars then set the ant home in the global environment vars and sending the command
ant all will work– Fábio Caramelo
Nov 9 at 15:18
If its a playbook, invoking or configuring ant will not work. I tried to do it and still not working. The ansible solution is the one suitable for me. if you dont want to use the ansible var to load the env vars then set the ant home in the global environment vars and sending the command
ant all will work– Fábio Caramelo
Nov 9 at 15:18
@FábioCaramelo not quite sure I understand your comment. I updated my answer with an end-to-end example if that can help.
– masseyb
Nov 9 at 16:38
@FábioCaramelo not quite sure I understand your comment. I updated my answer with an end-to-end example if that can help.
– masseyb
Nov 9 at 16:38
Just check it. Thanks for your help :)
– Fábio Caramelo
Nov 12 at 16:27
Just check it. Thanks for your help :)
– Fábio Caramelo
Nov 12 at 16:27
add a comment |
up vote
0
down vote
Thankfully to masseyb I found the problem. In my target machine I have ant installed and created the ANT_HOME and added the ANT_HOME/bin to the PATH.
However because this is declared on bash_profile jenkins does not recognize the command because it does not make the source of bash profile, so I added source ansible_env.HOME /.bash_profile && before the ant command so jenkins user can source the environment variables necessary to run the commands
It is not jenkins that does not recognize theantcommand rather it isansiblethat does not recognize theantcommand. The execution order is jenkins -> ansible -> ant. Provided that the location of your tools are configured in the Global Tools Configuration then jenkins will have no problem finding eitherantoransiblehowever this does not imply that the$PATHset by jenkins is the same$PATHavailable to processes spawned by jenkins. The issue in this case is the connection types established byansible(non-interactive shells) do not execute.bash_profilecommands.
– masseyb
Nov 9 at 13:03
You can remove Jenkins from the equation and the error would persist as it'sansibleand operating system configuration related. With that said, if you're going to use Jenkins then I would suggest using the Ant Plugin rather than using the Ansible Plugin to shell outantexecutions - keep a clear distinction betweenant's role andansible's role in your build process.
– masseyb
Nov 9 at 13:37
add a comment |
up vote
0
down vote
Thankfully to masseyb I found the problem. In my target machine I have ant installed and created the ANT_HOME and added the ANT_HOME/bin to the PATH.
However because this is declared on bash_profile jenkins does not recognize the command because it does not make the source of bash profile, so I added source ansible_env.HOME /.bash_profile && before the ant command so jenkins user can source the environment variables necessary to run the commands
It is not jenkins that does not recognize theantcommand rather it isansiblethat does not recognize theantcommand. The execution order is jenkins -> ansible -> ant. Provided that the location of your tools are configured in the Global Tools Configuration then jenkins will have no problem finding eitherantoransiblehowever this does not imply that the$PATHset by jenkins is the same$PATHavailable to processes spawned by jenkins. The issue in this case is the connection types established byansible(non-interactive shells) do not execute.bash_profilecommands.
– masseyb
Nov 9 at 13:03
You can remove Jenkins from the equation and the error would persist as it'sansibleand operating system configuration related. With that said, if you're going to use Jenkins then I would suggest using the Ant Plugin rather than using the Ansible Plugin to shell outantexecutions - keep a clear distinction betweenant's role andansible's role in your build process.
– masseyb
Nov 9 at 13:37
add a comment |
up vote
0
down vote
up vote
0
down vote
Thankfully to masseyb I found the problem. In my target machine I have ant installed and created the ANT_HOME and added the ANT_HOME/bin to the PATH.
However because this is declared on bash_profile jenkins does not recognize the command because it does not make the source of bash profile, so I added source ansible_env.HOME /.bash_profile && before the ant command so jenkins user can source the environment variables necessary to run the commands
Thankfully to masseyb I found the problem. In my target machine I have ant installed and created the ANT_HOME and added the ANT_HOME/bin to the PATH.
However because this is declared on bash_profile jenkins does not recognize the command because it does not make the source of bash profile, so I added source ansible_env.HOME /.bash_profile && before the ant command so jenkins user can source the environment variables necessary to run the commands
answered Nov 9 at 12:26
Fábio Caramelo
277
277
It is not jenkins that does not recognize theantcommand rather it isansiblethat does not recognize theantcommand. The execution order is jenkins -> ansible -> ant. Provided that the location of your tools are configured in the Global Tools Configuration then jenkins will have no problem finding eitherantoransiblehowever this does not imply that the$PATHset by jenkins is the same$PATHavailable to processes spawned by jenkins. The issue in this case is the connection types established byansible(non-interactive shells) do not execute.bash_profilecommands.
– masseyb
Nov 9 at 13:03
You can remove Jenkins from the equation and the error would persist as it'sansibleand operating system configuration related. With that said, if you're going to use Jenkins then I would suggest using the Ant Plugin rather than using the Ansible Plugin to shell outantexecutions - keep a clear distinction betweenant's role andansible's role in your build process.
– masseyb
Nov 9 at 13:37
add a comment |
It is not jenkins that does not recognize theantcommand rather it isansiblethat does not recognize theantcommand. The execution order is jenkins -> ansible -> ant. Provided that the location of your tools are configured in the Global Tools Configuration then jenkins will have no problem finding eitherantoransiblehowever this does not imply that the$PATHset by jenkins is the same$PATHavailable to processes spawned by jenkins. The issue in this case is the connection types established byansible(non-interactive shells) do not execute.bash_profilecommands.
– masseyb
Nov 9 at 13:03
You can remove Jenkins from the equation and the error would persist as it'sansibleand operating system configuration related. With that said, if you're going to use Jenkins then I would suggest using the Ant Plugin rather than using the Ansible Plugin to shell outantexecutions - keep a clear distinction betweenant's role andansible's role in your build process.
– masseyb
Nov 9 at 13:37
It is not jenkins that does not recognize the
ant command rather it is ansible that does not recognize the ant command. The execution order is jenkins -> ansible -> ant. Provided that the location of your tools are configured in the Global Tools Configuration then jenkins will have no problem finding either ant or ansible however this does not imply that the $PATH set by jenkins is the same $PATH available to processes spawned by jenkins. The issue in this case is the connection types established by ansible (non-interactive shells) do not execute .bash_profile commands.– masseyb
Nov 9 at 13:03
It is not jenkins that does not recognize the
ant command rather it is ansible that does not recognize the ant command. The execution order is jenkins -> ansible -> ant. Provided that the location of your tools are configured in the Global Tools Configuration then jenkins will have no problem finding either ant or ansible however this does not imply that the $PATH set by jenkins is the same $PATH available to processes spawned by jenkins. The issue in this case is the connection types established by ansible (non-interactive shells) do not execute .bash_profile commands.– masseyb
Nov 9 at 13:03
You can remove Jenkins from the equation and the error would persist as it's
ansible and operating system configuration related. With that said, if you're going to use Jenkins then I would suggest using the Ant Plugin rather than using the Ansible Plugin to shell out ant executions - keep a clear distinction between ant's role and ansible's role in your build process.– masseyb
Nov 9 at 13:37
You can remove Jenkins from the equation and the error would persist as it's
ansible and operating system configuration related. With that said, if you're going to use Jenkins then I would suggest using the Ant Plugin rather than using the Ansible Plugin to shell out ant executions - keep a clear distinction between ant's role and ansible's role in your build process.– masseyb
Nov 9 at 13:37
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53207693%2frunning-ansible-playbook-with-ant-commands-in-jenkins%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
If I understand the problem correctly, your Jenkins job is configured to execute an ansible-playbook which shells out ant calls? Is there a reason to not be using the Jenkins ant plugin (e.g. invoke ant as a build step)? The error is explicit, ant is not available in the PATH for ansible.
– masseyb
Nov 8 at 17:37
But ant is there. If I run
antin my target machine ant would give a error because there was no build.xml on the folder, but would work nontheless. And ant env variable is set and also available in PATH. But you gave me an idea. /if the target machine has all set including ant, env var and PATH, then might be the source of the bash_profile. Gonna test it and will give feedback– Fábio Caramelo
Nov 9 at 10:26