jenkins与ansible集成
应该需要在Jenkins上安装一下
[root@mcw15 plugins]# yum install -y ansible
打开配置
[root@mcw15 plugins]# vim /etc/ansible/ansible.cfg [root@mcw15 plugins]# grep host_key_checking /etc/ansible/ansible.cfg host_key_checking = False [root@mcw15 plugins]#
也可以添加工具,添加不同的ansible版本,然后不同的项目用不同的ansible
不通:
[root@mcw15 mcwansible]# [root@mcw15 mcwansible]# ansible -i hosts example -m ping 10.0.0.13 | UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).", "unreachable": true } [root@mcw15 mcwansible]#
分发公钥
[root@mcw15 mcwansible]# ssh-copy-id -i /root/.ssh/id_rsa.pub 10.0.0.13 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@10.0.0.13's password: Number of key(s) added: 1Now try logging into the machine, with: "ssh '10.0.0.13'" and check to make sure that only the key(s) you wanted were added.[root@mcw15 mcwansible]# ls fenfa.sh hosts [root@mcw15 mcwansible]# cat hosts [example] 10.0.0.13 [root@mcw15 mcwansible]# ansible -i hosts example -m ping 10.0.0.13 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong" } [root@mcw15 mcwansible]#
example2的先不让通
[root@mcw15 mcwansible]# vim hosts [root@mcw15 mcwansible]# cat hosts [example] 10.0.0.13 [example2] 10.0.0.22 [root@mcw15 mcwansible]# ansible -i hosts example2 -m ping 10.0.0.22 | UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '10.0.0.22' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).", "unreachable": true } [root@mcw15 mcwansible]#
加个root作为凭证
暂时用这个项目做pipeline吧
新增两个文件
hosts
[example] 10.0.0.13 [example2] 10.0.0.22
playbook.yml
- hosts: example2tasks:- debug: msg="{{ lookup('env','BUILD_TAG') }}"
Jenkins file
pipeline {agent anystages {stage('deploy') {steps {ansiblePlaybook(playbook: "${env.WORKSPACE}/playbook.yml",inventory: "${env.WORKSPACE}/hosts",credentialsId: 'vagrant')}}} }
手动触发构建,失败
没这个步骤,步骤列表可以看到没有这个,现在有了docker了,之前安装了插件之后
java.lang.NoSuchMethodError: No such DSL method 'ansiblePlaybook' found among steps [
安装ansible插件:https://plugins.jenkins.io/ansible
安装之后,重新构建。报错,权限。可能是我们在Jenkins上加的凭证vagrant没有起到效果吧
把2去掉,用example,这个已经用密钥做了免密登录的
构建结果一样
此时在机器上是正常执行命令的
好吧,凭证填写了但是没有确定
新增之后再次触发
这次可以看到,已经成功了
并且从在yml中用lookup获取到了Jenkins任务运行的环境变量
此时改为example2,这个机器和Jenkins本身没有免密,执行命令的时候也会出现权限问题。看这里通过Jenkins凭证,是否可以正常执行
可以看到,example2的也正常可以运行剧本