Frequently Asked Questions
How do i install Docker modules for Ansible?
Run the following command on the Ansible control node:ansible-galaxy collection install community.docker
How do i install Docker using Ansible?
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
How do i install Kubernetes using Ansible?
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
How do i integrate Ansible Tower (AWX) with Jenkins?
- 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' )
How do i log in to private Docker registry using Ansible?
- 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
How do i pass Jenkins environment variables to Ansible?
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}"'
How do i pull Docker image using Ansible?
- name: Pull Docker Image
hosts: all
tasks:
- name: Pull Nginx image
community.docker.docker_image:
name: nginx
source: pull
How do i push Docker image to Docker Hub using Ansible?
- 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
How do i run an Ansible playbook?
ansible-playbook playbook.yml
How do i run Ansible playbook from Jenkins job?
Method 1: Using the "Ansible" Plugin
- In Jenkins, create a Freestyle Project.
- Under Build Steps, select Invoke Ansible Playbook.
- Provide the playbook path (
/path/to/playbook.yml
). - Add any extra parameters (e.g.,
-i inventory.ini
).
Method 2: Using a Shell Command
- Create a Freestyle Project.
- 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'
}
}
}
}