Shawn
Shawn

Reputation: 2819

Running Ansible from Jenkins as [user]

I have a running Ansible environment (with environment variable, dynamic inventory) and multiple scripts. Recently I added Jenkins to this same server in order to make the automation easier.

When I run the plays from within Jenkins however I noticed that the ansible playbooks are now run as 'jenkins' instead of the 'ansible' user.

The 'jenkins' user does not have full access to ansible environment variables or dynamic inventory (which was written for 'ansible' user). I would like to run all playbooks as 'ansible' as originally coded.

Does such a concept exist in Jenkins, and if so how?

The following URL gives a way around it. But this looks more like a hack. https://issues.jenkins-ci.org/browse/JENKINS-37063

I dont want to change any environment variables or move files around.

Upvotes: 3

Views: 3498

Answers (3)

Claudio Lopez
Claudio Lopez

Reputation: 131

I know this post is too old however I installed ansible in jenkins server using the jenkins user as: sudo su - jenkins -s /bin/bash to swith to this user first then installed ansible with sudo yum install ansible so jenkins user has permissions over ansible installation, configuration and execution of ansible commands.

The same concept applies for ansible-galaxy and ssh-genkey for ssh passwordless connection

. In this way jenkins can use the jenkins user to trigger calls to ansible during pipeline execution. I hope it works for someone new looking for a workaround.

Upvotes: 0

Shawn
Shawn

Reputation: 2819

I found a way of running the Jenkins as my original Ansible user:

Edit the /etc/default/jenkins file and change the

JENKINS_USER=ansible 
JENKINS_GROUP=ansible 

variables to your preferred name. I have used ansible as my user to run all my ansible scripts. Save the file and change the folder owner as follows.

chown -R ansible:ansible /var/lib/jenkins 
chown -R ansible:ansible /var/cache/jenkins
chown -R ansible:ansible /var/log/jenkins

After restarting the service, Jenkins will launch as your new user.

DO NOT change the NAME variable as that's deeply linked with Jenkins folder/configuration structures.

I was then able to launch all my Ansible scripts through Jenkins interface which ran on the same machine.

Upvotes: 3

Mehdi
Mehdi

Reputation: 356

I think on that server you can create a linux group and add both Jenkins and Ansible users to it.

This may give Jenkins user access to run ansible playbooks.

But regarding this scenario, I think the best way is to keep Jenkins and Ansible in different servers, and then you can Add Ansible as a slave to Jenkins, thereafter you can configure jenkins to run plabooks using ansible user.

Upvotes: 1

Related Questions