Ansible

On the Jenkins server, install Ansible:
sudo apt install ansible  # Ubuntu/Debian
sudo yum install ansible  # RHEL/CentOS
brew install ansible      # macOS
Jenkins can trigger Ansible in multiple ways:
  • Using the "Ansible" plugin.
  • Running Ansible as a shell command (ansible-playbook playbook.yml).
  • Using a Jenkins Pipeline Script (sh 'ansible-playbook playbook.yml').
Yes, using the remote-exec or local-exec provisioner:
provisioner "remote-exec" {
  inline = [
    "sudo apt update",
    "sudo apt install nginx -y"
  ]
}
Terraform remote state can be accessed in Ansible using the terraform_state module:
- name: Fetch Terraform state
  terraform_state:
    state: path/to/terraform.tfstate
  register: tf_state
Write a Terraform script to create resources:
resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
output "public_ip" {
  value = aws_instance.web.public_ip
}
Export the output for Ansible:
terraform output -json > terraform_outputs.json
Use Ansible to configure the instance:
ansible-playbook -i inventory.ini playbook.yml
  • Use Terraform to create cloud resources (VMs, networks, databases).
  • Use Terraform output variables to store resource details (IP addresses, SSH keys).
  • Use Ansible to configure and manage the provisioned infrastructure.
Use dry-run mode:
ansible-playbook playbook.yml --check
Debug Ansible errors:
ansible-playbook playbook.yml -vvv
Ansible Galaxy is a community repository for sharing Ansible roles.
ansible-galaxy install geerlingguy.nginx
ansible-playbook playbook.yml
Handlers are tasks triggered only if notified.
- name: Restart Apache
  service:
    name: apache2
    state: restarted
  listen: "restart_apache"