Commands & Configuration Flashcards
(156 cards)
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 option 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 define 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?
- First, ensure that the proper RedHat repository is enabled:
sudo subscription-manager repos --enable ansible-automation-platform-2.5-for-rhel-9-x86_64-rpms
- 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
The repository used in the example is just one of the many RedHat repositories containing the ‘ansible-navigator’ RPM. Additionally, the execution environment image is one of many.
’~/.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