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 does dot notation and array notation of variables are different? |
|
Answer» Dot notation works fine UNLESS we stump upon few SPECIAL cases such as |
|
| 2. |
How to generate encrypted passwords for a user module? |
|
Answer» Ansible has a very simple ad-hoc command for this ansible all -i localhost, -m DEBUG -a "msg={{ 'mypassword' | password_hash('sha512', 'mysecretsalt') }}"We can also use the Passlib LIBRARY of Python, e.g python -c "from passlib.HASH IMPORT sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"On top of this, we should also avoid storing raw passwords in playbook or host_vars, INSTEAD, we should use integrated methods to generate a hash version of a password. |
|
| 3. |
What are handlers? |
|
Answer» Handlers are LIKE SPECIAL tasks which only run if the Task contains a “notify” directive. tasks: - NAME: install nginx apt: pkg=nginx state=installed update_cache=true notify: - start nginx handlers: - name: start nginx service: name=nginx state=startedIn the above example after INSTALLING NGINX we are starting the server USING a `start nginx` handler. |
|
| 4. |
What is the best way to make Content Reusable/ Redistributable? |
|
Answer» To MAKE content REUSABLE and redistributable Ansible roles can be used. Ansible roles are basically a LEVEL of abstraction to organize playbooks. For example, if we need to execute 10 tasks on 5 systems, writing all of them in the playbook might LEAD to blunders and CONFUSION. Instead we create 10 roles and call them inside the playbook. |
|
| 5. |
Explain how you will copy files recursively onto a target host? |
|
Answer» There’s a copy module that has a recursive PARAMETER in it but there’s SOMETHING CALLED synchronize which is more efficient for large numbers of files. For EXAMPLE: - synchronize: src: /first/absolute/path dest: /second/absolute/path delegate_to: "{{ inventory_hostname }}" |
|
| 6. |
What is Ansible Tower and what are its features? |
|
Answer» Ansible Tower is an enterprise-level solution by RedHat. It provides a web-based console and REST API to manage Ansible across teams in an organization. There are many features such as
There are other features also such as Job Scheduling, NOTIFICATION INTEGRATION, CLI, etc. |
|
| 7. |
What is the difference between Ansible and Puppet? |
|
Answer» Management and SCHEDULING: In Ansible, the server pushes the configuration to the nodes on the other hand in puppet, the client pulls the configuration from the server. Also for scheduling, the puppet has an agent who POLLS EVERY 30mins(default settings) to make sure all nodes are in a desirable state. Ansible doesn’t have that feature in the free version. |
|
| 8. |
How do I access a variable name programmatically? |
|
Answer» VARIABLE names can be built by adding strings together. For EXAMPLE, if we need to get ipv4 address of an arbitrary INTERFACE, where the interface to be used may be supplied via a ROLE parameter or other input, we can do it in this way. {{ hostvars[inventory_hostname]['ansible_' + which_interface]['ipv4']['address'] }} |
|
| 9. |
Install Nginx using Ansible playbook? |
|
Answer» The playbook file would be: - hosts: stagingwebservers gather_facts: False vars: - server_port: 8080 tasks: - NAME: install nginx APT: pkg=nginx state=INSTALLED update_cache=true - name: serve nginx config template: src=../files/flask.conf dest=/etc/nginx/conf.d/ notify: - restart nginx handlers: - name: restart nginx service: name=nginx state=restarted - name: restart flask app service: name=flask-demo state=restarted...In the above playbook, we are fetching all hosts of stagingwebservers group for executing these tasks. The first TASK is to install Nginx and then configure it. We are also taking a flask server for reference. In the end, we also defined handlers so that in case the state changes it will restart Nginx. After executing the above playbook we can verify whether Nginx is installed or not. ps waux | GREP nginx |
|
| 10. |
What is the ad-hoc command in Ansible? |
|
Answer» Ad-hoc commands are like one-line playbooks to perform a specific TASK only. The syntax for the ad-hoc COMMAND is ansible [PATTERN] -m [module] -a "[module options]"For EXAMPLE, we need to reboot all servers in the staging group ansible atlanta -a "/sbin/reboot" -u username --become [--ask-become-pass] |
|
| 11. |
How can looping be done over a list of hosts in a group, inside of a template? |
|
Answer» This can be done by accessing the “$groups” DICTIONARY in the template, like so: {% for host in groups['db_servers'] %}{{ host }}{% endfor %}If we need to ACCESS facts also we need to make sure that the facts have been populated. For INSTANCE, a play that talks to db_servers: - hosts: db_serverstasks:- DEBUG: MSG="Something to debug"Now, this can be used within a template, like so: {% for host in groups['db_servers'] %}{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}{% endfor %}. |
|
| 12. |
What is Ansible Vault? |
|
Answer» Ansible vault is used to KEEP SENSITIVE data such as passwords instead of placing it as PLAINTEXT in playbooks or roles. Any structured data file or any single value inside the YAML file can be encrypted by Ansible. To encrypt a file ansible-vault encrypt foo.yml bar.yml baz.ymlAnd SIMILARLY to decrypt ansible-vault decrypt foo.yml bar.yml baz.yml |
|
| 13. |
What is Ansible Inventory and its types? |
|
Answer» In Ansible, there are two types of INVENTORY files: Static and Dynamic.
Now we can fetch using this command ansible-inventory -i demo_aws_ec2.yaml -graph |
|
| 14. |
What are callback plugins in Ansible? |
|
Answer» Callback plugins basically control most of the output we see while running cmd programs. But it can also be used to add ADDITIONAL output. For example log_plays callback is used to record playbook events to a log file, and mail callback is used to send email on playbook failures. We can also add custom callback plugins by DROPPING them into a callback_plugins directory adjacent to play, inside a role, or by PUTTING it in one of the callback directory SOURCES configured in ansible.cfg. |
|
| 15. |
How to automate the password input in playbook using encrypted files? |
|
Answer» <P>To automate password input we can have a password file for all the passwords of encrypted files will be SAVED and ansible can make a call to fetch those when required. ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q user@gateway.example.com"'This can ALSO be achieved by having a SEPARATE script that specifies the passwords. But in this case, we need to print a password to stdout to work without annoying errors. ansible-playbook launch.yml --vault-password-file ~/ .vault_pass.py |
|
| 16. |
How to setup a jump host to access servers having no direct access? |
|
Answer» First, we NEED to SET a ProxyCommand in ansible_ssh_common_args INVENTORY variable, since any arguments specified in this variable are added to the sftp/scp/ssh command line when connecting to the relevant host(s). For example [gatewayed]staging1 ansible_host=10.0.2.1staging2 ansible_host=10.0.2.2To create a jump host for these we need to add a command in ansible_ssh_common_args ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q user@gateway.example.com"'In this way whenever we will try to CONNECT to any host in the gatewayed group ansible will APPEND these arguments to the command line. |
|