FAQs

Frequently Asked Questions


Run the following command on the Ansible control node:
ansible-galaxy collection install community.docker
Create a playbook to install Docker on a remote machine:
- name: Install Docker
  hosts: all
  tasks:
    - name: Install dependencies
      apt:
        name: ['apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common']
        state: present

    - name: Add Docker GPG key
      shell: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

    - name: Add Docker repository
      shell: add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

    - name: Install Docker
      apt:
        name: docker-ce
        state: present
Run it with:
ansible-playbook -i inventory.ini install_docker.yml
You can use Ansible to install a Kubernetes cluster on remote nodes. Example playbook to install Kubernetes on Ubuntu:
- name: Install Kubernetes Cluster
  hosts: all
  tasks:
    - name: Install dependencies
      apt:
        name: ['apt-transport-https', 'ca-certificates', 'curl']
        state: present

    - name: Add Kubernetes GPG key
      shell: curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -

    - name: Add Kubernetes repository
      shell: echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | tee /etc/apt/sources.list.d/kubernetes.list

    - name: Install Kubernetes packages
      apt:
        name: ['kubelet', 'kubeadm', 'kubectl']
        state: present
Run with:
ansible-playbook -i inventory.ini install_k8s.yml
  • Install the "Ansible Tower Plugin" in Jenkins.
  • Configure Tower URL and credentials under Manage Jenkins → Configure System.
  • In a Pipeline, trigger an Ansible Tower job:
    ansibleTower(
        towerServer: 'AnsibleTower',
        jobTemplate: 'Deploy App',
        inventory: 'Production'
    )
- name: Login to a private registry
  hosts: all
  tasks:
    - name: Authenticate Docker
      community.docker.docker_login:
        registry_url: "https://registry.example.com"
        username: my_user
        password: my_password
You can use extra-vars (-e) in Ansible to pass Jenkins variables:
ansible-playbook -i inventory.ini playbook.yml -e "version=${BUILD_NUMBER}"
Or inside a Jenkins pipeline:
sh 'ansible-playbook -i inventory.ini playbook.yml -e "branch=${GIT_BRANCH}"'
- name: Pull Docker Image
  hosts: all
  tasks:
    - name: Pull Nginx image
      community.docker.docker_image:
        name: nginx
        source: pull
- name: Push Docker Image
  hosts: all
  tasks:
    - name: Push image to Docker Hub
      community.docker.docker_image:
        name: my_dockerhub_user/my_app
        push: yes
        source: local
        repository: my_dockerhub_user/my_app
ansible-playbook playbook.yml

Method 1: Using the "Ansible" Plugin

  1. In Jenkins, create a Freestyle Project.
  2. Under Build Steps, select Invoke Ansible Playbook.
  3. Provide the playbook path (/path/to/playbook.yml).
  4. Add any extra parameters (e.g., -i inventory.ini).

Method 2: Using a Shell Command

  1. Create a Freestyle Project.
  2. Add a Build Step → Execute Shell and run:
ansible-playbook -i inventory.ini playbook.yml

Method 3: Using a Jenkins Pipeline Script

pipeline {
    agent any
    stages {
        stage('Run Ansible') {
            steps {
                sh 'ansible-playbook -i inventory.ini playbook.yml'
            }
        }
    }
}