• Jenkins Pipeline With Ansible Playbook


    Jenkins Pipeline With Ansible Playbook

    Jenkins 页面

    新建项目 – 流水线项目
    安装插件 – Ansible plugin
    Ansible plugin 版本1.1
    Invoke Ansible Ad-Hoc commands and playbooks.
    Report an issue with this plugin
    
    • 1
    • 2
    • 3
    配置 Jenkins 全局工具 – Ansible
    # Ansible Name
    ansible_2.9.27
    
    # Path to ansible executables directory
    /usr/bin
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 编写 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'
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    Jenkins 主机

    安装 Ansible 软件
    [root@jenkins_master ~]# yum -y install ansible-2.9.27-1.el7
    
    • 1
    验证 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)]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    创建构建目录
    [root@jenkins_master ~]# mkdir /usr/local/jenkins/build
    
    • 1
    赋权 Jenkins 构建目录
    [root@jenkins_master ~]# chown -R jenkins.jenkins /usr/local/jenkins/build
    
    • 1
    • 配置 Ansible Inventory 文件
    [root@jenkins_master ~]# tail -6 /etc/ansible/hosts 
    [master]
    10.10.200.235
    
    [build]
    10.10.200.236
    10.10.200.238
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    编写 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!!"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    构建项目

    查看输出结果
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    参考

  • 相关阅读:
    担忧CentOS停服?KeyarchOS系统来支撑
    一种简易的Nor flash存储数据机制
    智能配电房监控系统:实现配电智能化管理
    springboot集成Elasticsearch7.16,使用https方式连接并忽略SSL证书
    线程状态及线程之间通信
    基于Qt的Live2D模型显示以及控制
    母婴进销存管理软件怎么选①
    “互联网寒冬”来袭,软件测试人员该如何度过这次危机?
    浅谈大数据之Flink
    Qt实现手动切换多种布局
  • 原文地址:https://blog.csdn.net/u010766726/article/details/126162319