반응형

jenkins 서버에 ansible 설치 및 설정

jenkins credential 추가

  • 위 설정 과정에서 생성된 id_rsa 값을 credential로 등록
  • credentialId : ansible-remote-servers

jenkins plugin 설치

  • SSH Agent Plugin
  • AnsiColor Plugin
  • Ansible Plugin

jenkinsfile 프로젝트 세팅

  • Jenkinsfile 환경 세팅
  • 프로젝트에 ansible 디렉토리 생성 후 nginx playbook 세팅
  • 프로젝트 루트에 Jenkinsfile-ansible.groovy 코드 추가
    @Library("commons") _
    
    pipeline {
        agent any
    
        environment {
          CREDENTIAL_ID = "ansible-remote-servers"
        }
    
        stages {
            stage("prepare") {
                steps {
                    script {
                        def spec = readYaml(text: utils.getProject().description)
    
                        spec.each { item ->
                            item.tags.each { tag ->
                                stage("${item.host} - ${item.phase} - ${item.playbook} - ${tag}") {
                                    sshagent(credentials: [CREDENTIAL_ID]) {
                                        ansiColor("xterm") {
                                            dir("ansible") {
                                                ansiblePlaybook(
                                                    inventory: "inventories/${item.phase}",
                                                    playbook: "${item.playbook}.yml",
                                                    tags: tag,
                                                    colorized: true
                                                )
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

jenkins pipeline 빌드 item 생성 및 실행

  • description
    - host: webserver
      phase: dev
      playbook: nginx
      tags: [ install, config, restart ]
  • Script Path : Jenkinsfile-ansible.groovy
반응형

'Development > Jenkins' 카테고리의 다른 글

[Jenkins] docker container 무중단 배포  (0) 2023.01.23
[Jenkins] docker image 빌드 설정  (0) 2023.01.18
[Jenkins] Jenkins Pipeline  (0) 2020.12.28
[Jenkins] Jenkinsfile  (0) 2020.12.28
[Jenkins] Selenium 테스트 설정  (0) 2019.11.24

+ Recent posts