Jenkins Pipeline With Ansible Playbook

Jenkins Pipeline With Ansible Playbook

文章目录

  • Jenkins Pipeline With Ansible Playbook
      • Jenkins 页面
          • 新建项目 -- 流水线项目
          • 安装插件 -- Ansible plugin
          • 配置 Jenkins 全局工具 -- Ansible
      • Jenkins 主机
          • 安装 Ansible 软件
          • 验证 Ansible 安装结果
          • 创建构建目录
          • 赋权 Jenkins 构建目录
          • 编写 Ansible Playbook
      • 构建项目
          • 查看输出结果
      • 参考

Jenkins 页面

新建项目 – 流水线项目
安装插件 – Ansible plugin
Ansible plugin 版本1.1
Invoke Ansible Ad-Hoc commands and playbooks.
Report an issue with this plugin
配置 Jenkins 全局工具 – Ansible
# Ansible Name
ansible_2.9.27

# Path to ansible executables directory
/usr/bin
  • 编写 Jenkins Pipeline
pipeline {
	agent {
		node {
			label "Master Node"
			customWorkspace "/usr/local/jenkins/build"
		}
	}

	options { 
		timestamps()
	}
	
	stages {
	    stage('Ansible PlayBook') {
            steps {
                script {
                  	ansiblePlaybook disableHostKeyChecking: true, 
                  	    installation: 'ansible_2.9.27', 
                  	    inventory: '/etc/ansible/hosts', 
                  	    playbook: '/usr/local/jenkins/playbook/jenkins_ansible_test.yml'
                }
            }
        }
    }
}

Jenkins 主机

安装 Ansible 软件
[root@jenkins_master ~]# yum -y install ansible-2.9.27-1.el7
验证 Ansible 安装结果
[root@jenkins_master ~]# ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
创建构建目录
[root@jenkins_master ~]# mkdir /usr/local/jenkins/build
赋权 Jenkins 构建目录
[root@jenkins_master ~]# chown -R jenkins.jenkins /usr/local/jenkins/build
  • 配置 Ansible Inventory 文件
[root@jenkins_master ~]# tail -6 /etc/ansible/hosts 
[master]
10.10.200.235

[build]
10.10.200.236
10.10.200.238
编写 Ansible Playbook
[root@jenkins_master ~]# cat /usr/local/jenkins/playbook/jenkins_ansible_test.yml 
---

- name: Run the test playbook
  gather_facts: false
  hosts: localhost
  tasks:
    - name: Get the username running this playbook
      become: false
      local_action: command whoami
      register: username_on_the_host

    - name: Display the User name
      ansible.builtin.debug:
        msg: "Hello User {{ username_on_the_host.stdout }} from Jenkins and Ansible!!"

构建项目

查看输出结果
Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /usr/local/jenkins/workspace/Jenkins Pipeline With Ansible Playbook
[Pipeline] {
[Pipeline] ws
Running in /usr/local/jenkins/build
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Ansible PlayBook)
[Pipeline] script
[Pipeline] {
[Pipeline] ansiblePlaybook
16:14:22  [build] $ /usr/bin/ansible-playbook /usr/local/jenkins/playbook/jenkins_ansible_test.yml -i /etc/ansible/hosts
16:14:23  
16:14:23  PLAY [Run the test playbook] ***************************************************
16:14:23  
16:14:23  TASK [Get the username running this playbook] **********************************
16:14:23  changed: [localhost -> localhost]
16:14:23  
16:14:23  TASK [Display the User name] ***************************************************
16:14:23  ok: [localhost] => {
16:14:23      "msg": "Hello User jenkins from Jenkins and Ansible!!"
16:14:23  }
16:14:23  
16:14:23  PLAY RECAP *********************************************************************
16:14:23  localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
16:14:23  
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // ws
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

参考

  • From Zero to Code: Using Ansible in Jenkins Pipelines
  • Ansible Playbook with Jenkins Pipeline
  • 使用 Jenkins 和 Ansible 实现 CI/CD

你可能感兴趣的:(Jenkins,服务器配置,jenkins,运维)