AllTechGurukul


Its Naveen's Wiki

Ansible and Chef Basic and Advanced QA

Ansible Basic

  • Q: What is Ansible, and what are its key benefits?

    • A: Ansible is an open-source IT automation engine that automates software provisioning, configuration management, application deployment, and intra-service orchestration. Key benefits include:
      • Simplicity: Uses a human-readable language (YAML) and requires minimal agent installation.
      • Agentless: Communicates with managed nodes over SSH (or WinRM for Windows), eliminating the need for agents on the target systems.
      • Idempotency: Ensures that tasks can be run multiple times with the same result, preventing unintended changes.
      • Powerful: Handles complex automation tasks, including multi-tier deployments and orchestration.
  • Q: What is an Ansible Playbook?

    • A: A playbook is a YAML file that defines a set of tasks to be executed on managed nodes. It's the core of Ansible's automation. Playbooks can contain multiple plays, each targeting different groups of hosts and performing specific actions.
  • Q: What are Ansible Modules?

    • A: Modules are the building blocks of Ansible. They represent specific actions that can be performed on managed nodes, such as installing packages, managing services, or copying files. Ansible has a vast library of built-in modules, and you can also create custom modules.

Ansible  Advanced

  • Q: How do you handle secrets in Ansible?

    • A: Ansible Vault is used to encrypt sensitive data, such as passwords and API keys. You encrypt the data using a password, and Ansible Vault decrypts it when the playbook is executed. It's crucial to not store the vault password in the repository itself.
  • Q: Explain Ansible Roles and their importance.

    • A: Roles provide a way to organize and reuse Ansible playbooks. A role is a directory structure containing playbooks, tasks, handlers, templates, and variables related to a specific functionality (e.g., setting up a web server). Roles promote modularity and make playbooks more maintainable.
  • Q: How can you implement zero-downtime deployments with Ansible?

    • A: Several strategies can be used, including:
      • Rolling Updates: Updating a subset of servers at a time, ensuring that the application remains available. Ansible's serial keyword in playbooks helps with this.
      • Load Balancers: Using a load balancer to direct traffic away from servers being updated.
      • Blue/Green Deployments: Deploying the new version alongside the old one and then switching traffic.

Chef Basic

  • Q: What is Chef, and what is its primary use case?

    • A: Chef is a configuration management tool that automates the process of configuring and maintaining servers. It uses a Ruby-based DSL (Domain Specific Language) to define the desired state of a system. Its primary use case is infrastructure as code.
  • Q: What are Cookbooks, Recipes, and Resources in Chef?

    • A:
      • Cookbooks: The fundamental unit of configuration in Chef. They contain recipes and other files that define a specific configuration.
      • Recipes: Define the steps required to configure a system. They are written in Ruby and specify the resources to be managed.
      • Resources: Represent the components of a system that Chef can manage, such as packages, files, services, and users. Examples include the package, file, service resources.
  • Q: What is Chef Client?

    • A: The Chef Client is a program that runs on the nodes being managed. It retrieves the desired state configuration from the Chef Server (or Chef Solo) and applies it to the node.

Chef Advanced

  • Q: Explain the difference between Chef Solo and Chef Server.

    • A:
      • Chef Solo: A standalone version of Chef that doesn't require a central server. It's suitable for smaller environments or testing. Cookbooks are typically stored locally.
      • Chef Server: A centralized server that stores cookbooks, node metadata, and other configuration information. Chef Clients connect to the server to retrieve the configurations. This is preferred for larger, more complex infrastructures.
  • Q: How do you manage node attributes in Chef?

    • A: Attributes are used to store data about nodes. They can be set at different levels (e.g., node, environment, role) and are used by recipes to configure the system. node.default, node.override, and node.normal are important distinctions.
  • Q: What are Chef Environments?

    • A: Environments allow you to manage different stages of your infrastructure (e.g., development, staging, production). You can assign different attributes and cookbook versions to each environment. This allows you to test changes before deploying to production.

============================================================

Here are some basic and advanced interview-style questions and answers for Ansible and Chef:


🛠 Ansible - Basic Questions

1. What is Ansible?

Answer:
Ansible is an open-source configuration management, automation, and orchestration tool that allows system administrators to automate IT infrastructure using YAML-based playbooks.


2. What are the key components of Ansible?

Answer:

  • Inventory – Defines the hosts or groups of servers.
  • Modules – Small programs that perform tasks like installing packages, copying files, etc.
  • Playbooks – YAML-based files that define automation workflows.
  • Tasks – A sequence of commands executed on remote hosts.
  • Handlers – Special tasks triggered only when notified.
  • Roles – A collection of tasks, variables, templates, and handlers for modular automation.
  • Facts – System information collected dynamically.

3. How do you install Ansible on RHEL 8?

Answer:

sudo dnf install ansible -y

Verify the installation:

ansible --version

4. What is an Ansible Playbook?

Answer:
A playbook is a YAML file that contains a set of tasks to be executed on remote hosts.
Example:

- name: Install Apache
  hosts: webservers
  become: yes
  tasks:
    - name: Install httpd
      yum:
        name: httpd
        state: present

This installs Apache (httpd) on all hosts in the webservers group.


5. How do you test an Ansible playbook before running it?

Answer:
Use the --check mode to simulate the execution without making actual changes:

ansible-playbook site.yml --check

🛠 Ansible - Advanced Questions

6. What is an Ansible Role?

Answer:
An Ansible Role is a way to organize playbooks into reusable and modular components.
A role typically contains:

  • tasks/ – Task definitions.
  • handlers/ – Event-driven handlers.
  • templates/ – Jinja2 templates.
  • files/ – Static files to be copied.
  • vars/ – Role-specific variables.

Create a role using:

ansible-galaxy init myrole

7. How do you use Ansible Vault?

Answer:
Ansible Vault encrypts sensitive data (passwords, API keys) in playbooks.
To create an encrypted file:

ansible-vault create secrets.yml

To edit:

ansible-vault edit secrets.yml

To use it in a playbook:

vars_files:
  - secrets.yml

To run the playbook:

ansible-playbook site.yml --ask-vault-pass

8. How does Ansible differ from other configuration management tools like Chef or Puppet?

Feature Ansible Chef Puppet
Agentless? ✅ Yes ❌ No ❌ No
Language YAML (Declarative) Ruby (Imperative) DSL (Declarative)
Ease of Use Simple Moderate Complex
Push/Pull Push Pull Pull

9. What is the difference between ansible and ansible-playbook?

Answer:

  • ansible → Runs ad-hoc commands on remote hosts.
  • ansible-playbook → Runs playbooks (YAML-based automation scripts).

Example:

# Ad-hoc command
ansible all -m ping

# Running a playbook
ansible-playbook site.yml

🛠 Chef - Basic Questions

10. What is Chef?

Answer:
Chef is a powerful configuration management tool used for infrastructure automation. It follows a client-server architecture and uses Ruby DSL to define configurations.


11. What are the components of Chef?

Answer:

  1. Chef Workstation – Where configurations are developed.
  2. Chef Server – The central repository that stores cookbooks.
  3. Chef Client (Node) – Runs on managed servers to apply configurations.
  4. Cookbooks – Collections of recipes.
  5. Recipes – Instructions to configure a system.

12. How do you install Chef Workstation on RHEL 8?

Answer:

sudo dnf install chef-workstation -y

Verify installation:

chef --version

13. What is a Chef Recipe?

Answer:
A recipe is a Ruby script that defines system configurations.
Example:

package 'httpd' do
  action :install
end

service 'httpd' do
  action [:enable, :start]
end

14. What is a Chef Cookbook?

Answer:
A cookbook is a collection of recipes, attributes, templates, and other resources used to configure systems.

Create a cookbook:

chef generate cookbook my_cookbook

🛠 Chef - Advanced Questions

15. What is the difference between a Recipe and a Cookbook in Chef?

Feature Recipe Cookbook
What is it? A single file with configurations A collection of recipes and other resources
Scope Defines specific system configurations Manages entire configurations
Format .rb file Directory with multiple files

16. What is a Chef Attribute?

Answer:
Chef attributes store values that can be used in recipes.
Example:

default['apache']['port'] = 80

Use in a recipe:

template "/etc/httpd/conf/httpd.conf" do
  variables(port: node['apache']['port'])
end

17. What are Chef Roles?

Answer:
Roles define a set of recipes and attributes to apply to a group of nodes.

Create a role:

chef generate role webserver

Example webserver.json:

{
  "name": "webserver",
  "run_list": ["recipe[apache]"]
}

Apply to a node:

knife node run_list add mynode "role[webserver]"

18. What is Chef Databag?

Answer:
A Databag is a JSON-based key-value store to store global configuration data like passwords or API keys.

Create a databag:

knife data bag create users

Add an entry:

knife data bag create users admin --json-file admin.json

19. What is the difference between Chef and Ansible?

Feature Chef Ansible
Agent-based? ✅ Yes ❌ No
Language Ruby DSL YAML
Architecture Client-Server Agentless (SSH)
Configuration Pull/Push Pull Push

Conclusion

  • Ansible is easier to set up and agentless.
  • Chef is more powerful but has a steeper learning curve.
  • Both tools are widely used for automation and configuration management.

Would you like help with writing playbooks or cookbooks? 🚀

Post a Comment

Previous Post Next Post