Commands & Configuration Flashcards
Which option for the ‘ansible’ command allows one to specify an inventory file to be used?
-i
With the ‘-i’ option, one can specify a path to an inventory file or one can pass a comma separated list of hosts.
Which ansible option allows one to specify which user to use when connecting/logging-in to the hosts?
-u
The ‘-u’ option allows you to specify the remote user for the SSH connection.
Which option forces ansible to use password-based authentication when connecting to remote hosts?
When might it be necessary?
-k
The ‘-k’ option informs ansible to prompt for a password when attempting to login via SSH.
By default, ansible assumes you want to connect with SSH keys rather than by password.
Which option is used to instruct ansible which user to switch to after logging in to the remote host?
‘-b’ or ‘–become’
The ‘-b’ option can instruct ansible to effectively ‘su’ to another user after logging in to the host. This is often used to elevate to root or some account with administrative privileges.
When used without an argument, the ‘-b’ option defaults to root.
Which option for the ‘ansible’ command instructs the command to prompt for a password to use to elevate to the account specified by the ‘-b’ option?
-K
This options instructs ansible to prompt for a privilege escalation password.
Suppose you want to use ansible to create the ‘student’ user account on all hosts in the inventory. You’re going to log-in to the remote hosts as the ‘admin’ user but then elevate to root on all of the hosts. How can this be done in a one-liner?
Assume that the ‘ansible.cfg’ file doesn’t specify an inventory file.
ansible -i inventory all -u admin -k -b -K -m user -a “name=student”
The built-in ‘user’ module allows you to manage user accounts via ansible.
Suppose you wanted to enable the user ‘student’ to have permission to run all commands as all users on all hosts without needing to input a password. How would you enable this?
Add the following to ‘/etc/sudoers’ or a drop-in file in ‘/etc/sudoers.d/’
student ALL=(ALL) NOPASSWD: ALL
The first ‘ALL’ corresponds to all hosts.
The second ‘ALL’ corresponds to being able to run the relevant command as all users/groups.
The third ‘ALL’ corresponds to being able to run all commands.
The ‘NOPASSWD’ tag allows student to run all commands without giving a password.
Suppose you wanted to enable the user ‘student’ to have permission to run ‘kill’ as all users on all hosts without needing to input a password and run ‘arp’ as all users on all hosts with needing a password. How would you enable this?
Add the following to ‘/etc/sudoers’ or a drop-in file in ‘/etc/sudoers.d/’
student ALL=(ALL) NOPASSWD: /bin/kill, PASSWD: /sbin/arp
Where is the default ansible inventory stored?
/etc/ansible/hosts
An alternative inventory location can be specified in ansible.cfg
Suppose in your ansible inventory you have a group titled ‘webservers’ and you want to see the hosts that belong to this group. Which command will do this?
ansible -i inventory webservers –list-hosts
‘-i inventory’ is used to defined an inventory file different than the default
Describe the following inventory file:
ansible1
ansible2
[webservers]
apache1
apache2
apache3
[databases]
pgsql1
pgsql2
[servers:children]
webservers
databases
There are 7 unique hosts being managed by the inventory.
There are 3 groups being managed. The ‘webservers’ and ‘databases’ groups are nested within the ‘servers’ group.
The hosts ‘ansible1’ and ‘ansible2’ are considered ‘ungrouped’ which means they belong to no group.
‘ungrouped’ is technically a group itself. This group refers to all hosts that belong to no group other than the built-in ‘all’ group.
Where is the ansible configuration file stored?
/etc/ansible/ansible.cfg
‘ansible –version’ will show the ‘ansible.cfg’ file being used.
Each project can have its own ‘ansible.cfg’ file. If a project-specific ‘ansible.cfg’ file is found, the main ‘/etc/ansible.ansible.cfg’ will be ignored.
Describe the following ansible.cfg file:
[defaults]
inventory = inventory
remote_user = ansible
host_key_checking = false
deprecation_warning = false
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
The [defaults] section sets the default settings while the [privilege_escalation] section sets how ansible runs commands on managed hosts.
‘inventory’ defines the path to the inventory file
‘remote_user’ is the name of the user that will log in on the remote host
‘ask_pass’ specifies whether or not to prompt for a password
‘become’ indicates whether you want to automatically switch to the ‘become_user’
‘become_user’ specifies the user that ansible will change to after connecting to the remote host
‘become_method’ sets how to become the other user after connecting
What is ansible-navigator?
ansible-navigator is a command-line tool and text-based interface for creating, reviewing, running and troubleshooting different types of Ansible content.
ansible-navigator is primarily used alongside execution environments.
An execution environment is just a container image serving as an Ansible control node.
Which command can be used to list all currently available Ansible modules on a machine?
ansible-doc -l
The ‘-l’ option lists all available modules.
The ‘ansible-doc’ command is used for viewing Ansible-specific documentation.
Suppose you want to find detailed documentation on how to use the ‘ansible.builtin.shell’ module. How could you do this?
ansible-doc -t module shell
If you don’t know the name of the module, you could first list all available modules with the ‘ansible-doc -l’ command.
If you’re interested in a different plugin type, you could run the command ‘ansible-doc -t [plugin-type] -l’ instead. Then after finding the plugin name, you would run ‘ansible-doc -t [plugin-type] [plugin-name]’ to get the detailed documenation page.
The ‘-t’ option allows you to filter the documentation for specific types of plugins (the default plugin type is ‘module’)
What is the ‘requirements.yml’ file?
The ‘requirements.yml’ file lists all required collections for a project.
This file is usually found in the current project directory.
The ‘requirements.yml’ file is usually used as an argument to the ‘ansible-galaxy’ command.
What is the ‘ansible-galaxy’ command?
‘ansible-galaxy’ is used to install collections from a Galaxy server.
The default Galaxy server is ‘galaxy.ansible.com’
How can one list all installed collections with the ‘ansible-galaxy’ command?
ansible-galaxy collection list
How can one install all collections specified by the ‘requirements.yml’ file?
ansible-galaxy collection install -r requirements.yml
The -r option allows one to specify a requirements file.
How can one install the Ansible ‘my.collection’ collection while making sure that it is accessible from the execution environment?
ansible-galaxy collection install my.collection -p collections
The ‘-p’ option allows one to specify the path where collections will be placed after being downloaded.
‘-p collections’ installs collections in ‘./collection/’
Without the ‘-p’ option, the collection is installed in the default collections path which is ‘~/.ansible/collections:/usr/share/ansible/collections’
The default path for collections is specified by the ‘collections_path’ variable in the ‘ansible.cfg’ file.
The default ‘collections_path’ is not available from within the ‘ansible-navigator’ execution environment.
How does one set up ‘ansible-navigator’ on a Linux machine?
- Install ‘ansible-navigator’: sudo dnf install ansible-navigator
- Login to the RedHat container registry: podman login registry.redhat.io
- Pull the RedHat execution environment image: podman pull registry.redhat.io/ansible-automation-platform-22/ee-supported-rhel8:latest
- All ‘ansible-navigator’ commands should now work!
Use RedHat developer account credentials for the registry login
’~/.ansible-navigator.yml’ can be defined to include generic settings for ‘ansible-navigator’
Like other Ansible commands, if an ‘ansible-navigator.yml’ file is found in the current project directory, this will have higher priority than the settings file found in the home directory.
How would you ping all hosts in the inventory to verify connectivity?
ansible all -m ping
What is the default module for running ad-hoc commands with the ‘ansible’ utility?
ansible.builtin.command
This means that ‘-m command’ isn’t necessary when using this module
How could you ensure that the ‘httpd’ service is running on ‘managed-node1’ ?
ansible managed-node1 -m service -a ‘name=httpd state=started’
How could you list the user account being used after connecting to each managed node? (without using the shell module)
ansible -a ‘whoami’
The command module is not idempotent.
‘-m command’ isn’t necessary since the command module is the default module.
The ‘ansible.builtin.command’ module doesn’t support the use of shell metacharacters (a metacharacter is a space, tab, newline, or one of the following characters: ‘|’, ‘&’, ‘;’, ‘(’, ‘)’, ‘<’, or ‘>’)
How could you verify that the ‘nginx’ package is installed on every managed host?
ansible all -m shell -a ‘rpm -qa | grep nginx’
Use of the pipe (‘|’) is supported in the ‘ansible.builtin.shell’ module
How could you copy the contents ‘Welcome!’ into the ‘message of the day’ file on each managed host?
ansible all -m copy -a ‘content=”Welcome!” dest’=/etc/motd’
Why is it better to use the ‘user’ module than to use the ‘command’ module for managing users with Ansible?
The ‘command’ module is not idemptotent. The ‘user’ module is idemptotent.
The ‘command’ module should only be used when no dedicated module exists or can be found for a task.
Describe the important pieces of the following playbook:
This playbook will install/enable the ‘vsftpd’ package on the ‘ansible2’ managed node. It will then use the ‘copy’ module to add content to the ‘/var/ftp/pub/README’ file.
Lines that begin with a dash (-) are part of a YAML list. The first line (-name: deploy vsftpd) is a ‘play’ within the playbook. Each playbook can contain multiple plays and within each play there can be multiple tasks.
The first two tasks use the old, deprecated Ansible playbook syntax for specifying arguments. The final task (copy) uses the modern syntax for specifying arguments. This modern syntax conforms better with YAML syntax.
How could you run the ‘vsftpd.yml’ file using ‘ansible-navigator’ with the output being displayed similarly to running playbooks normally?
ansible-navigator run -m stdout –pp never vsftpd.yml
‘-m stdout’ will write the command output to STDOUT instead of using interactive mode.
’–pp never’ will instruct ‘ansible-navigator’ to not check for a newer version of the specified container image.
Suppose you want to instruct ‘ansible-navigator’ to not check for a new container image every time you run a playbook without having to manually specify ‘–pp never’ on the command line every time. How can you do this?
Add the following to your ‘.ansible-navigator.yml’ file:
What matters here is the “policy: missing” for pulling container images. This will tell ‘ansible-navigator’ to only pull an image if the desired one is missing.
What is the purpose of running a playbook with interactive mode in ‘ansible-navigator’ ?
Interactive mode is good for debugging. It allows you to separately view each individual play/task in a playbook.
It can be used by simply running a playbook with ‘ansible-navigator’ and not specifying ‘-m stdouot’
What is a ‘play’ in an Ansible playbook?
A ‘play’ is a series of tasks executed against selected hosts from the inventory, using specific credentials.
Using multiple plays allows running tasks on different hosts, using different credentials from the same playbook.
Each play can have its own escalation parameters defined. Some of the common ones include the following:
remote_user
become
become_method
become_user
Where/How can variables be defined?
- Variables can be defined in playbooks
- The output of a command/task can be used as a variable via the ‘register’ keyword
- ‘vars_prompt’ can be used to ask for user input and then store that as a variable
- Variables can be specified on the command line
- Variables can be defined in include files
Include files make for the most portable playbooks as an include file can be creatd per environment, rather than hardcoding site-specific values in multiple different playbooks.
How could you define a ‘web_package’ variable with the value ‘httpd’ at the beginning of a play within a playbook?
Suppose you want to use an include file named ‘users.yml’ to define variables for a play. How could you do this?
It’s common practice to have a ‘vars’ subdirectory in your Ansible project directory for storing different include files.
How do you refer to a variable within a playbook after defining it? Use ‘web_package’ as an example.
Refer to a variable as: {{ web_package }}
If the variable is the first element, quotes must be used: “{{ web_package }}”
If the variable is used in a conditional, no curly braces are needed: web_package
What are host variables and how do they work?
Host variables are variables that are specific to a single host. They are defined in a YAML file that has the name of the inventory hostname and are stored in a ‘host_vars’ subdirectory within the project directory.
You can also define variables for host groups. This file should have the name of the host group and be located within the ‘group_vars’ directory in the project directory.