How Dynamic Processes Enhance CI/CD Pipelines

Prem ChoudharyPrem Choudhary
2 min read

Now the idea is that in organizations, we do not have one type of task every time, sometimes we have to only search things in instances files, sometimes we have to only restart the services like varnish, nginx, php, etc. sometimes we have to git clone and deploy that code.

So due to dynamisms in the tasks we need multiple ansible playbook.

These work cannot be done in a single inventory also, because in industry there are multiple instance for feed, blogs, website, etc. So these all we will categories in different tags for better deployment.

Multiple tags =equals to= Multiple inventories

But in our previous blog we have hardcoded it in the Jenkinsfile that will not give us the option of dynamisms.
ansible-playbook -i ./ansible/inventory.aws_ec2.yml -e 'host_group=tag_git_gitaction' ./ansible/playbooks/nginx-setup.yml -u ubuntu --private-key $EC2_SSH_KEY

For having dynamic inventory and dynamic playbook, Jenkins give us option of using parameters.
You can learn about Jenkins parameters from: jenkin_parameter_docs

We will use Choice parameters as we want Choice in the playbook, inventory and host group as it is used in playbook.
ansible-playbook -i ./ansible/inventory.aws_ec2.yml -e 'host_group=tag_git_gitaction' ./ansible/playbooks/nginx-setup.yml -u ubuntu --private-key $EC2_SSH_KEY

Now, let say we had made 3 playbooks:-
1. deploy.yml - For deploying the code on servers.
2. restart.yml - For restart services.
3. search.yml - searching for files.
and 3 Inventories:-
1. Feed.aws_ec2.yml : All the EC2 instance for feeds.
2. blog.aws_ec2.yml : All the EC2 instance for blogs.
3. website.aws_ec2.yml : All the EC2 instance for website.
and 3 Tags for these inventories:-
1. tag_git_gitaction
2. tag_feed_feedaction
3. tag_blog_blogaction

Here the Updated Jenkinsfile.

pipeline {
    agent any

    parameters {
        choice(name: 'INVENTORY', choices: ['Feed.aws_ec2.yml', 'blog.aws_ec2.yml', 'website.aws_ec2.yml'], description: 'Choose the environment for deployment')
        choice(name: 'PLAYBOOK', choices: ['deploy.yml', 'restart.yml', 'search.yml'], description: 'Choose the Ansible playbook to execute')
        choice(name: 'TAG', choices: ['tag_git_gitaction', 'tag_feed_feedaction', 'tag_blog_blogaction'], description: 'Choose the Ansible inventory tag to execute')

    }


    environment {
        // Using SSH key stored in Jenkins credentials
        EC2_SSH_KEY = credentials('EC2_SSH')
        ANSIBLE_HOST_KEY_CHECKING = 'False'

    }

    stages {
        stage('Checkout') {
            steps {
                 checkout scm
            }
        }

        stage('Run Ansible Playbook') {
            steps {
                script {
                    sh '''
                        echo "Using host group: ${TAG}"
                        ansible-playbook -i ./ansible/${INVENTORY} -e "host_group=${TAG}"  ./ansible/playbooks/${PLAYBOOK} -u ubuntu --private-key $EC2_SSH_KEY
                    '''
                }
            }
        }
    }
}
0
Subscribe to my newsletter

Read articles from Prem Choudhary directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prem Choudhary
Prem Choudhary

DEVOPS ENGINEER | AWS | Java | Linux | Python | Git | Github | Docker | Spring Boot | Ansible | Jenkins | Algorithmic Trader | Pine Editior