InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
How can we delegate tasks in Ansible? |
|
Answer» Task delegation is an important feature of Ansible since there MIGHT be use cases where we would want to perform a task on one host with reference to other hosts. We can do this using the delegate_to keyword. For example, if we want to MANAGE NODES in a load balancer pool we can do: - hosts: webservers serial: 5 tasks:- name: Take machine out of ELB pool ansible.builtin.command: /usr/bin/take_out_of_pool {{ inventory_hostname }} delegate_to: 127.0.0.1 - name: Actual steps would go here ansible.builtin.yum: name: acme-web-stack state: latest - name: Add machine back to ELB pool ansible.builtin.command: /usr/bin/add_back_to_pool {{ inventory_hostname }} delegate_to: 127.0.0.1We are also defining serial to control the number of hosts executing at one time. There is another shorthand syntax called local_action which can be used instead of delegate_to. ...tasks: - name: Take machine out of ELB pool local_action: ansible.builtin.command /usr/bin/take_out_of_pool {{ inventory_hostname }}...But there are few exceptions also such as include, add_host, and debug tasks that cannot be delegated. |
|
| 2. |
Explain Ansible register. |
|
Answer» ANSIBLE register is used to store the output from TASK execution in a VARIABLE. This is useful when we have different outputs from each remote host. The register VALUE is valid throughout the playbook execution so we can make use of set_fact to MANIPULATE the data and provide input to other tasks accordingly. - hosts: all tasks:name: find all txt files in /home shell: "find /home -name *.txt" register: find_txt_filesdebug:var: find_txt_filesIn the above example, we are searching for all .txt files in the remote host’s home folder and then capturing it in find_txt_files and displaying that variable. |
|
| 3. |
When is it unsafe to bulk-set task arguments from a variable? |
|
Answer» All of the TASK's ARGUMENTS can be dictionary-typed variables which can be useful in some dynamic EXECUTION scenarios also. However, Ansible issues a warning SINCE it introduces a security risk. vars: usermod_args:name: testuserstate: presentupdate_password: alwaystasks:- user: '{{ usermod_args }}'In the above example, the values passed to the variable usermod_args could be overwritten by some other malicious values in the HOST facts on a compromised target machine. To avoid this
|
|
| 4. |
How is the Ansible set_fact module different from vars, vars_file, or include_var? |
|
Answer» In Ansible, set_fact is used to SET new variable values on a host-by-host basis which is just like ansible facts, discovered by the setup module. These variables are AVAILABLE to subsequent plays in a playbook. In the case of vars, vars_file, or include_var we know the value beforehand whereas when using set_fact, we can STORE the value after preparing it on the fly using certain tasks like using FILTERS or taking subparts of another variable. We can also set a fact cache over it. set_fact variable assignment is done by using key-pair values where the key is the variable name and the value is the assignment to it. A simple example will be like below - set_fact:one_fact: value1second_fact:value2 |
|
| 5. |
How does the Ansible firewalld module work? |
|
Answer» Ansible firewalld is used to manage firewall rules on host machines. This works just as Linux firewalld daemon for allowing/blocking services from the port. It is split into two major concepts
Few examples of setting up firewalld are - name: permit traffic in default ZONE for HTTPS SERVICE ansible.posix.firewalld: service: https permanent: yes state: enabled - name: do not permit traffic in default zone on port 8081/tcp ansible.posix.firewalld: port: 8081/tcp permanent: yes state: disabled |
|
| 6. |
How does Ansible synchronize module works? |
|
Answer» Ansible synchronize is a module similar to rsync in Linux machines which we can use in playbooks. The features are similar to rsync such as archive, compress, delete, etc but there are few limitations also such as
An example of synchronize module is ---- hosts: host-remote tasks:- name: sync from sync_foldersynchronize:src: /var/tmp/sync_folder dest: /var/tmp/Here we are transferring files of /var/tmp/sync_folder folder to remote machine’s /var/tmp folder |
|