Ansible Q&A

 

1) How to create an empty file with Ansible?

To create an empty file, Ansible uses a file module. For this, we need to set up two parameters.

Path - This place represents the location where the file gets created, either the relative or an absolute path. Also, the name of the file includes here.

State - For creating a new file, this parameter should be set to touch.


2) How will you set the environment variable or any path for a task or entire playbook?

To set the environment variables, we use the environment keyword. We'll use it at the task or other levels in the play:

environment:

  PATH: "{{ ansible_env.PATH }}:/thingy/bin"

  SOME: value


3) How do you access Shell Environment Variables?

Accessing the value of Home environment variable on the management machine:

local_home:”{{lookup(‘env’,’HOME’)}}”


4) Is it possible to increase the Ansible reboot module to more than 600 seconds?

Yes, it is possible to increase the Ansible reboot module to specific values using the below syntax:

- name: Reboot a Linux system 

  reboot:

    reboot_timeout: 1000


5) How can you see all the variables specific to my host?

To see all the host-specific variables, that include all facts and other resources are:

Ansible - m debug- a “var=hostvars[‘hostname’]” localhost


6) How do you access a variable name programmatically?

By adding strings together, the variables names are built programmatically like below format:

{{ hostvars[inventory_hostname]['ansible_' + which_interface]['ipv4']['address'] }}


7) What is idempotency?

Idempotence is an essential feature of Ansible, which helps you to execute one or more tasks on a server as many times as needed, but without changing the result beyond the initial application.


8) How will you get access to the ansible host when I delegate a task?

We can access it through host variables and even works for all the overridden variables like ansible_port, ansible_user, etc.

original_host: "{{ hostvars[inventory_hostname]['ansible_host'] }}"


9) How can you filter out tasks in tags?

Use –tags or –skip-tags options on the command line

Use the TAGS_RUN and TAGS_SKIP options, If you're in Ansible configuration settings.


10) How do you use Ansible to create encrypted files?

To create an encrypted file, use the ‘ansible-vault create’ command.

$ ansible-vault create filename.yaml

You will get a prompt to create a password, and then to type it again for confirmation. You will now have access to a new file, where you can add and edit data.


11) What are “facts” in the context of Ansible?

Facts are newly discovered and known system variables, found in the playbooks, used mostly for implementing conditionals executions. Additionally, they gather ad-hoc system information.

You can get all the facts by using this command:

$ ansible all- m setup


12) Explain what an ask_pass module is.

It’s a playbook control module used to control a password prompt. It’s set to True by default.


13) What’s an ad hoc command?

Users initiate ad hoc commands to initiate actions on a host without using a playbook. Consider it a one-shot command.


14) Explain the difference between a playbook and a play?

A play is a set of tasks that run on one or more managed hosts. Plays consist of one or more tasks. A playbook consists of one or more plays.


15) What are tags?

When there’s an extensive playbook involved, sometimes it’s more expedient to run just a part of it as opposed to the entire thing. That’s what tags are for.


16) What’s a handler?

In Ansible, a handler is similar to a regular task in a playbook, but it will only run if a task alerts the handler. Handlers are automatically loaded by roles/<role_name>/handlers/main.yaml. Handlers will run once, after all of the tasks are completed in a particular play.


17) How do you keep data secret in a playbook?

If you want to keep secret data but still be able to share it publicly, then use Vault in playbooks. But if you’re using –v (verbose) mode and don’t want anyone to see the results, then use:

name: secret task

shell: /usr/bin/do_something --value={{ secret_value }}

no_log: True


Comments

Popular posts from this blog

SRE/DevOps Syllabus

AWS Code Commit - CI/CD Series Part 1

Docker - Preventing IP overlapping