Interview › Infrastructure as Code (Terraform, Ansible)
How does Ansible connect to managed hosts?
Infrastructure as Code (Terraform, Ansible) · Intermediate level
Answer
Ansible connects to managed hosts using connection plugins, most commonly SSH for Linux/Unix and WinRM for Windows. Inventory defines the hosts and variables, and Ansible authenticates using SSH keys, passwords, Kerberos, cloud identity, or other supported mechanisms.
Technical explanation
Connection details can be inventory variables such as ansible_host, ansible_user, ansible_port, and ansible_connection.
become enables privilege escalation where tasks need root or administrator rights.
For cloud fleets, dynamic inventory can populate connection metadata automatically.
Prefer idempotent modules over shell so repeated runs are safe and change reporting is meaningful.
Separate reusable role logic from inventory-specific variables so the same automation works across environments.
Run lint, syntax checks, check mode where useful, and staged rollouts before production-wide changes.
Hands-on example
1. Create a minimal Ansible control workflow for: How does Ansible connect to managed hosts?
2. Inventory example:
[web]
web1 ansible_host=10.0.1.10 ansible_user=ec2-user
web2 ansible_host=10.0.1.11 ansible_user=ec2-user
[web:vars]
ansible_become=true
3. Playbook example:
---
- name: Configure web hosts
hosts: web
become: true
tasks:
- name: Ensure nginx is installed
ansible.builtin.package:
name: nginx
state: present
- name: Ensure nginx is running
ansible.builtin.service:
name: nginx
state: started
enabled: true
4. Run ansible -m ping web first, then ansible-playbook site.yml --check --diff, then the real run.
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Infrastructure as Code (Terraform, Ansible) interview questions
- What is Infrastructure as Code, and what problems does it solve over click-ops?
- What is the difference between declarative and imperative IaC, and where do Terraform and Ansible fall?
- What is the difference between configuration management and provisioning?
- What is Terraform, and what is the core plan/apply workflow?
- What does terraform init do?
- What is the Terraform state file, and why is it critical?
- Why should state be stored remotely, and what backend would you use on AWS?
- What is state locking, and why does it matter for teams?