Ansible Basics Flashcards

1
Q

What are important characteristics of Ansible?

A
  • Agentless
  • Idempotent
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What language is Ansible written in?

A

Python

Thats why Ansible requires all nodes to have Python installed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is Ansible agentless?

A

One of the key things about Ansible is that it is agentless. This means that there is no need to install any extra software on the managed nodes. The only requirements for the managed nodes are Python and the ability to accept SSH connections from the control node.

The control node connects to the nodes via SSH and pushes small Ansible Modules to them, which will be implementing the tasks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is Ansible idempotent?

A

You can run the same playbook multiple times and it will result in the same state of your managed hosts (servers, machines). After the first run of the playbook, subsequent runs will not make any changes if the desired state as described in the playbook is already reached.

For example, if you have a playbook that installs a package on a server, the first time you run it, Ansible will install the package. But if you run the same playbook again, Ansible will see that the package is already installed and will not attempt to install it again.

This idempotence is a very powerful feature of Ansible, because it allows you to be confident that running your playbooks will not have unexpected side effects. You can run them repeatedly without worrying about things getting messed up.

It’s important to note, however, that not all Ansible modules are idempotent. While Ansible’s core modules are generally idempotent, some community-provided modules or custom scripts may not be.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the Architecture of Ansible?

A

You have one control node and one or more managed nodes.

On the control node you install Ansbile. It then connects to the managed nodes via SSH and pushes Ansible modules to them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an inventory File?

A

Ansible needs to know about the nodes it will manage. It does this through an inventory file where all the managed nodes are listed, usually grouped logically based on user requirements.
Example:
———————–
[webservers]
webserver1.example.com
webserver2.example.com

[dbservers]
dbserver1.example.com
dbserver2.example.com
————————–

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Playbook?

A

The primary mechanism by which Ansible accomplishes tasks is through a construct called a playbook. Playbooks are like scripts in Ansible’s own language, which are human-readable and describe a policy you want your remote systems to enforce or a set of steps in a general IT process.

Each Playbook contains at least one Play.

Each Play contains at least one Task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would a Playbook to install a LAMP stack look like?

A
  • name: Install LAMP stack
    hosts: webservers
    become: yes
    tasks:
    • name: Install Apache
      apt:
      name: apache2
      state: present
      update_cache: yes
    • name: Install MySQL
      apt:
      name: mysql-server
      state: present
      update_cache: yes
    • name: Install PHP
      apt:
      name:
      - php
      - php-mysql
      state: present
      update_cache: yes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the steps to use Ansible?

A
  1. Install Ansible
    Use sudo apt install ansible or pip install ansible
  2. Setup an inventory
  3. Write a Playbook
  4. Run the Playbook
    ansible-playbook -i <your-inventory-file> lamp.yml
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Is Ansible declaritive or imperitive?

A

In general Ansible is considered declaritive.

When you’re defining an Ansible playbook, you’re defining a list of tasks, which often implies a certain order of execution. This part can be seen as more imperative in nature, as you’re giving a series of commands to be executed in a certain order.

However, each individual task is still declarative in nature. For example, if you have a task to install a package, you’re not telling Ansible the commands to run to install the package; you’re declaring that the package should be installed and letting Ansible figure out the steps.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does ansible achieve idempotency?

A

For each task, Ansible checks the current state of the target system before deciding whether changes are necessary. If the current state matches the desired state, no changes are made. For example, if a task is supposed to ensure a specific package is installed, Ansible first checks whether the package is already installed. If it is, Ansible skips the task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are alternatives to Ansible?

A

Puppet and Chef

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would a Playbook look like which adds sudo users to a server?

A
  • name: Add user, set password, and grant sudo access
    hosts: servers
    become: yes
    vars:
    username: newuser
    password: “{{ ‘password’ | password_hash(‘sha512’) }}”
    tasks:
    • name: Ensure user exists
      ansible.builtin.user:
      name: “{{ username }}”
      password: “{{ password }}”
      update_password: on_create
    • name: Add user to sudoers
      ansible.builtin.lineinfile:
      path: /etc/sudoers
      line: “{{ username }} ALL=(ALL) NOPASSWD:ALL”
      validate: ‘visudo -cf %s’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you use variables in Playbooks?

A

Ansible uses the Jinja2 templating language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are some benefits of using Ansible?

A

If you need to manage multiple servers, ssh-ing into each server and repeating all the steps manually is time consuming and error prone.

With Ansible:

  1. Execute all tasks from one machine
  2. Configure all steps in a single yaml file
  3. Reuse the file multiple times
  4. More reliable and less error prone

Also Ansible is declarative with a simple YAML language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a Module?

A

A module is small piece of code which gets pushed to managed nodes.
Modules are very granular and only do a single task like installing via apt (the apt module), or copying/removing a files (the file module).
After finishing the task, the modules are removed from the node.

Ansible has hundreds of modules for all kinds of IT tasks.

17
Q

How does a task look like?

A

A task has a name, a module which is called, and arguments to that module.

  • name: Rename table foo to bar
    postgresql_table:
    table: foo
    rename: bar
18
Q

What is Ansible Galaxy?

A

Ansible Galaxy also serves as a place to share and discover Ansible collections. Collections are a distribution format for Ansible content that can include playbooks, roles, modules, and plugins.

19
Q

What is a role in Ansible?

A

In Ansible, a role is a way of organizing tasks and related files into a coherent unit of automation. When you automate IT environments with Ansible, you might find yourself writing lots of tasks and playbooks. A role in Ansible provides a framework for fully independent, reusable collections of variables, tasks, files, templates, and modules which can be automatically loaded into a playbook.

A role conatins tasks, defaults, variables, metadata and files.