How to run jenkins build, in multiple servers with ssh-key

Trying to figure out how can I run a Jenkins build on multiple servers with my ssh-key connection.

For now I used SSH remote host plugin, but this plugin is good for 1,2,3,4.. servers and not for 100+.

I googleit and I found that many people offer a solution to use Ansible but i have no experience in ansible so if anyone has some examples for this process to run build on multiple servers with ansible or with any other way, I'll be happy to hear.

I'm running Jenkins-Master on windows and Jenkins-Slave on Linux(ubuntu).

This is a pretty simplified example, but for a freestyle job, I think it breaks down into the following steps:

  1. install ansible on the jenkins-agent. pip install ansible or apt install ansible

  2. Load your ssh key into Jenkins credentials store. I assume here that you use the credential store plugin (but you could also load it into an ssh-agent, or use some other method to expose the key on the jenkins-agents.)

i) Manage jenkins -> Manage Credentials
ii) add credentials -> "SSH username with private key"
iii) paste in they key and give it a name like "SSH_KEY_ANSIBLE"

  1. Create a new Jenkins freestyle job and add credentials (SSH_KEY_ANSIBLE)

i) Go to Build Environment section of job configuration
ii) check "Use secret text(s) or file(s)"
iii) add binding item "SSH User Private Key" to SSH_KEY_ANSIBLE
iv) set the Username variable to SSH_USER, set the key file to SSH_KEY

  1. add your list of hosts to your ansible inventory file on jenkins-agent: /etc/ansible/hosts
 [jenkins_job_hosts]  
 myhost1  
 myhost2  
 ...  
 myhost101
  1. Create a playbook on the jenkins agent, for example /home/jenkins/ansible/my_first_playbook.yml:
---
- hosts: jenkins_job_hosts
  tasks:
    - command: date

    - debug:
        msg: add other ansible tasks here....
  1. Go to build section in jenkins job.

i) add an "Execute shell" build step.
ii) use the following to run the ansible against all your hosts

ansible-playbook --private-key ${SSH_KEY} \
          -u ${SSH_USER} \
          -i /etc/ansible/hosts \
           /home/jenkins/ansible/my_first_playbook.yml

Extra steps

  1. You can put your inventory file and playbook into git, and check them out into the job

  2. Also you can look at pipeline jobs in groovy syntax in a Jenkinsfile, which simplifies managing the job.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值