1. Configuration management with Ansible in Foreman
By using Ansible as a configuration management tool in Foreman, you can define the desired state of your hosts and maintain it over time. Foreman combines Ansible features with recurring job runs and centralized orchestration, which helps you manage host configurations consistently.
1.1. High-level steps for configuration management with Ansible
You use Ansible roles to define the desired state of the hosts registered to Foreman. Running the roles enforces the configuration.
Configuration management with Ansible involves the following high-level steps:
-
Import Ansible roles into Foreman.
-
Assign specific roles to a host or host group.
-
Optional: Override Ansible variables per host or host group in Foreman.
-
Run the roles on the host or host group.
As a result, your hosts maintain a consistent configuration over time. The Ansible roles become a part of their definition.
|
Note
|
Running an Ansible role on a host triggers a remote execution job in the background but the use case is different. The typical use case for a remote execution job is to run an ad-hoc task or orchestration. The typical use case for configuration management by using Ansible and Ansible roles is long-term state management. |
1.2. Ansible versions supported for integration with Foreman
Supported Ansible versions follow the Ansible versions provided by the base operating system of Foreman server or any Smart Proxies for remote execution.
Foreman uses Ansible as provided by the base operating system of Foreman server or any Smart Proxies for remote execution. Therefore, the supported version of Ansible depends on your base operating system configuration.
2. Enabling the Ansible plugin
Before you can use Ansible for configuration management in Foreman, enable Ansible integration in Foreman and on all Smart Proxy servers that you want to use to run Ansible roles on hosts.
-
Enable the Ansible plugin on your Foreman server:
# foreman-installer \ --enable-foreman-plugin-ansible \ --enable-foreman-proxy-plugin-ansible
-
On all Smart Proxy servers that you want to use to run Ansible roles on hosts, enable the Ansible plugin:
# foreman-installer --enable-foreman-proxy-plugin-ansible
3. Configuring Ansible definitions to apply to hosts
Configure and organize Ansible definitions in Foreman to control which configurations are available for your hosts. Keeping definitions organized helps you apply consistent changes across your hosts.
3.1. Importing Ansible roles and variables
You can import Ansible roles and variables from the Ansible paths on Foreman server or Smart Proxy servers that have Ansible enabled.
-
Place the roles and variables in the appropriate Ansible paths on all Smart Proxies from where you want to use the roles. For more information, see Ansible role and collection paths in Foreman.
If you want to use custom or third party Ansible roles, ensure to configure an external version control system to synchronize roles between Foreman server and Smart Proxy servers.
-
In the Foreman web UI, navigate to Configure > Ansible > Roles.
-
Click Import to select the Smart Proxy from which you want to import.
-
Select the roles that you want to import.
-
Click Submit.
3.2. Overriding Ansible variables in Foreman
You can use Foreman to override Ansible variables for roles imported into Foreman. This helps you customize the behavior of imported roles to fit your specific needs.
|
Note
|
If you use an Ansible role to run a task as a user that is not the |
-
Ansible variables are available in Foreman. For more information, see Importing Ansible roles and variables
-
To use overridden Ansible variables, your user account has a role that allows viewing the attributes that are matched against hosts.
-
In the Foreman web UI, navigate to Configure > Ansible > Variables.
-
Select the Ansible variable that you want to override and manage with Foreman.
-
In the Default Behavior area, select the Override checkbox.
-
In the Parameter Type field, select the value type for validation such as string or boolean. The types array and hash have further options for handling upon a variable match. For more information, see the Prioritize Attribute Order area below.
-
In the Default Value field, enter the default value that you want to use if there is no match for the variable.
-
Optional: If you do not want to display the value of the variable as plain text in the Foreman web UI, select the Hidden Value checkbox to display the content of the variable as asterisks. This is useful for sensitive values such as passwords or secret tokens.
-
Optional: Expand the Optional Input Validator area and specify conditions that will be used to validate concrete values of the variable:
-
Select Required if you want to enforce users to fill in this variable.
-
In the Validator Type field, select how the value will be validated:
-
list– The value will be validated against an enumeration of allowed values. -
regex– The value will be validated against a regular expression pattern.
-
-
-
Optional: In the Prioritize Attribute Order area, specify the order of priority to match an override with a host by host attributes. Order at the top takes higher precedence. The first match wins.
You can combine multiple attributes into a single matcher key using a comma as the AND operation. For example, the matcher key of
hostgroup, environmentwould expect matchers such ashostgroup = "web servers"ANDenvironment = production.If you use the parameter type array or hash, you can further set:
-
Merge Overrides – Merges members of the arrays/hashes instead of replacing the whole array or hash. If the hashes contain the same key, the value is overwritten by the value of the host.
-
Merge Default – Adds the default value to the array or hash.
-
Avoid Duplicates – Ensures that the values in the array or hash are unique.
-
-
Optional: Expand the Specify Matchers area and specify criteria for selecting hosts on which the variable overrides.
-
To save the override settings, click Submit.
-
To use the Ansible variable, add the variable as a parameter to your host or host group, or add the variable as a global parameter:
-
To add the variable to a host:
-
In the Foreman web UI, navigate to Hosts > All Hosts and select the host that you want to use.
-
Click the Ansible tab, and in the Variables area, click the pencil icon to edit the value of the variable.
-
Click the tick icon to accept the value of the changed variable or the cross icon to cancel the change.
-
-
To add the variable to a host group:
-
In the Foreman web UI, navigate to Configure > Host Groups, and select the host group that you want to use.
-
Click the Parameters tab, and in the Host Group Parameters area, click Add Parameter.
-
In the Name field, add the Ansible variable name.
-
From the Type list, select the type of the variable for validation.
-
In the Value field, enter the value for the variable.
-
-
To add the variable as a global parameter:
-
In the Foreman web UI, navigate to Configure > Global Parameters, and click Create Parameter.
-
In the Name field, add the Ansible variable name.
-
From the Type list, select the type of the variable for validation.
-
In the Value field, enter the value for the variable.
-
Optional: If you do not want to display the Ansible variable in plain text, select the Hidden Values checkbox to display the content of the variable as asterisks in the Foreman web UI.
-
-
3.3. Assigning Ansible roles to a host group
You can assign a previously imported Ansible role to a host group. This enables Foreman to apply the configuration defined by the role when you provision a new host by using the host group.
-
In the Foreman web UI, navigate to Configure > Host Groups.
-
Click the host group name to which you want to assign an Ansible role.
-
On the Ansible Roles tab, select the role that you want to add from the Available Ansible Roles list.
-
Click the + icon to add the role to the host group. You can add more than one role.
-
Click Submit.
3.4. Assigning Ansible roles to an existing host
You can assign a previously imported Ansible role to an existing host. This enables Foreman to apply the configuration defined by the role when you run the Ansible roles on the host.
-
In the Foreman web UI, navigate to Hosts > All Hosts.
-
Select the host and click Edit.
-
On the Ansible Roles tab, select the role that you want to add from the Available Ansible Roles list.
-
Click the + icon to add the role to the host. You can add more than one role.
-
Click Submit.
-
Optional: On the Parameters tab, click Add Parameter to add any parameter variables that you want to pass to job templates at run time. This includes all Ansible Playbook parameters and host parameters that you want to associate with the host. To use a parameter variable with an Ansible job template, you must add a Host Parameter.
3.5. Changing the order of Ansible roles
Change the order of Ansible roles on a host to control how role tasks are applied. This helps prevent dependency and precedence issues during configuration runs.
-
In the Foreman web UI, navigate to Hosts > All Hosts.
-
Select a host.
-
Select the Ansible Roles tab.
-
In the Assigned Ansible Roles area, you can change the order of the roles by dragging and dropping the roles into the preferred position.
-
Click Submit to save the order of the Ansible roles.
3.6. Removing Ansible roles from a host
Remove Ansible roles from a host to stop applying configurations to the host.
-
In the Foreman web UI, navigate to Hosts > All Hosts.
-
Select the host and click Edit.
-
Select the Ansible Roles tab.
-
In the Assigned Ansible Roles area, click the - icon to remove the role from the host. Repeat to remove more roles.
-
Click Submit.
4. Applying Ansible definitions to hosts
When a host has one or more Ansible roles assigned to it, Foreman can run the roles on the host to apply and enforce the configuration state that the roles define.
4.1. Running Ansible roles on a host
Run Ansible roles on a host through the Foreman web UI to apply the assigned configuration immediately. This helps you verify and enforce the intended state of the host.
-
The public SSH key from Smart Proxy is available on the host. For more information, see Distributing SSH keys for remote execution in Managing hosts.
-
In the Foreman web UI, navigate to Hosts > All Hosts.
-
Select the host on which you want to run the assigned Ansible roles.
-
From the Schedule a job menu, select Run Ansible roles.
-
You can view the status of your Ansible job by navigating to Monitor > Jobs.
4.2. Running Ansible roles on a host group
When a host group has one or more Ansible roles assigned to it, Foreman can run the roles on all hosts in the host group at once to apply and enforce the configuration state that the roles define.
-
The public SSH key from Smart Proxy is available on the hosts in the host group. For more information, see Distributing SSH keys for remote execution in Managing hosts.
-
In the Foreman web UI, navigate to Configure > Host Groups.
-
From the list in the Actions column for the host group, select Run all Ansible roles.
-
You can view the status of your Ansible job on the Run Ansible roles page. Click Rerun to rerun a job.
4.3. Configuring a host to run Ansible roles in check mode
Run Ansible roles in check mode through the Foreman web UI to preview changes before applying them. This helps you reduce risk when validating role behavior on a host.
-
In the Foreman web UI, navigate to Hosts > All Hosts.
-
Click Edit for the host you want to enable check mode for.
-
In the Parameters tab, ensure that the host has a parameter named
ansible_roles_check_modewith typebooleanset totrue. -
Click Submit.
5. Using Ansible Vault with Foreman
You can encrypt sensitive Ansible data files using the Ansible Vault tool and configure Ansible to access the encrypted files using a password stored in a file.
-
If you customized
/etc/ansible/ansible.cfg, copy your configuration from/etc/ansible/ansible.cfgto/usr/share/foreman-proxy/.ansible.cfg. -
Encrypt the sensitive file using the
ansible-vaultcommand:# ansible-vault encrypt /etc/ansible/roles/Role_Name/vars/main.yml
Note that
ansible-vaultchanges the file permissions to600. -
Change the group and permissions of the encrypted file to ensure that the
foreman-proxyuser can read it:# chgrp foreman-proxy /etc/ansible/roles/Role_Name/vars/main.yml # chmod 0640 /etc/ansible/roles/Role_Name/vars/main.yml
-
Create the
/usr/share/foreman-proxy/.ansible_vault_passwordfile and enter the Vault password into it. -
Change the user and permissions of the
.ansible_vault_passwordfile to ensure that only theforeman-proxyuser can read it:# chown foreman-proxy:foreman-proxy /usr/share/foreman-proxy/.ansible_vault_password # chmod 0400 /usr/share/foreman-proxy/.ansible_vault_password
-
Add the path of the Vault password file to the
[defaults]section in/usr/share/foreman-proxy/.ansible.cfg:[defaults] vault_password_file = /usr/share/foreman-proxy/.ansible_vault_password
The path to the Vault password file must be absolute.
6. Integrating Foreman and AWX
You can integrate Foreman and AWX to use Foreman server as a dynamic inventory source for AWX. When provisioning new hosts from Foreman server, you can use the provisioning callback function to trigger playbook runs from AWX.
6.1. Adding Foreman server to AWX as a dynamic inventory item
To add Foreman server to AWX as a dynamic inventory item, you must create a credential for a Foreman server user on AWX, add an AWX user to the credential, and then configure an inventory source.
-
If your Foreman deployment is large, for example, managing tens of thousands of hosts, using a non-admin user can negatively impact performance because of time penalties that accrue during authorization checks. For large deployments, consider using an admin user.
-
For non-admin users, the
Ansible Tower Inventory Readerrole is assigned to your Foreman server user. For more information about managing users, roles, and permission filters, see Creating and managing roles in Administering Foreman. -
Your Foreman server and AWX are hosted on the same network or subnet.
-
In the AWX web UI, create a credential for your Foreman. For more information about creating credentials, see Add a New Credential and Red Hat Satellite 6 credentials in AWX community documentation. Note that this part of the AWX documentation also applies to Foreman.
Table 1. Foreman credentials Credential Type: Foreman Foreman URL:
https://foreman.example.com
Username:
The username of the Foreman user with the integration role.
Password:
The password of the Foreman user.
-
Add an AWX user to the new credential. For more information about adding a user to a credential, see Getting Started with Credentials in AWX community documentation.
-
Add a new inventory. For more information, see Add a new inventory in AWX community documentation.
In the new inventory, add Foreman server as the inventory source, specifying the following inventory source options.
Table 2. Inventory source options Source Foreman Credential
The credential you create for Foreman server.
Overwrite
Select
Overwrite Variables
Select
Update on Launch
Select
Cache Timeout
90
-
Ensure that you synchronize the source that you add.
6.2. Configuring provisioning callback for a host
When you create hosts in Foreman, you can use AWX to run playbooks to configure your newly created hosts. This is called provisioning callback in AWX.
The provisioning callback function triggers a playbook run from AWX as part of the provisioning process. The playbook configures the host after the provisioning process.
In Foreman server, the Kickstart Default and Kickstart Default Finish templates include three snippets:
-
ansible_provisioning_callback -
ansible_tower_callback_script -
ansible_tower_callback_service
You can add parameters to hosts or host groups to provide the credentials that these snippets can use to run Ansible Playbooks on your newly created hosts.
-
Foreman is added as a dynamic inventory in AWX. For more information, see Integrating Foreman and AWX.
-
In the AWX web UI, complete the following tasks:
-
Create a machine credential for your new host. Ensure that you enter the same password in the credential that you plan to assign to the host that you create in Foreman. For more information, see Credentials in AWX community documentation.
-
Create a project. For more information, see Projects in AWX community documentation.
-
Add a job template to your project. In your job template, you must enable provisioning callbacks, generate the host configuration key, and note the template_ID of your job template. For more information, see Job Templates in AWX community documentation.
-
-
In the Foreman web UI, navigate to Configure > Host Group.
-
Create a host group or edit an existing host group.
-
In the Host Group window, click the Parameters tab.
-
Click Add Parameter.
-
Enter the following information for each new parameter:
Table 3. Host parameters Name Value Description ansible_tower_provisioningtrue
Enables Provisioning Callback.
ansible_tower_api_urlDefines the URL of your AWX, including the required API path. For older versions of AWX, use
/api/v2instead of/api/controller/v2. If unsure, check the API endpoints of your instance to verify the correct path.ansible_job_template_idtemplate_ID
The ID of your provisioning template that you can find in the URL of the template:
/templates/job_template/5.ansible_host_config_keyconfig_KEY
The host configuration key that your job template generates in AWX.
-
Click Submit.
-
Create a host using the host group.
-
On your new host, start the
ansible-callbackservice:# systemctl start ansible-callback
-
On your new host, check the the status of the
ansible-callbackservice:# systemctl status ansible-callback
Provisioning callback is configured correctly if the command returns the following output:
foreman.example.com systemd[1]: Started Provisioning callback to AWX...NoteFor manual provisioning callback, you can use the provisioning callback URL and the host configuration key from a host to call AWX:
$ curl \ --data curl \ --data host_config_key=My_Host_Config_Key \ --show-error \ --silent \ https://awx.example.com/api/v2/job_templates/8/callback/
Ensure that you use
httpswhen you enter the provisioning callback URL.This triggers the playbook run specified in the template against the host.
Appendix A: Ansible role and collection paths in Foreman
Foreman imports and runs Ansible roles and collections from specific system paths.
System-installed roles and collections are located in /usr/share/ansible, while custom roles and collections should be placed in /etc/ansible.
Foreman imports and runs Ansible roles from the following paths:
-
/etc/ansible/roles -
/usr/share/ansible/roles -
/etc/ansible/collections -
/usr/share/ansible/collections
Roles and collections from installed packages are placed under /usr/share/ansible.
If you want to add custom roles or collections, place them under /etc/ansible.
Appendix B: Customizing Ansible configuration
Foreman manages essential Ansible configuration that is required for Ansible integration with Foreman. However, you can customize other Ansible configuration options as usual.
Foreman stores the essential Ansible configuration as environment variables in /etc/foreman-proxy/ansible.env.
This file is managed by foreman-installer.
Ansible reads configuration from a configuration file and the environment provided by Smart Proxy.
If you need to customize Ansible configuration, you can do so in the system-wide /etc/ansible/ansible.cfg file or in the /usr/share/foreman-proxy/.ansible.cfg file in the home directory of the foreman-proxy user.
Note that if you use /usr/share/foreman-proxy/.ansible.cfg, Ansible spawned by Foreman ignores configuration in /etc/ansible/ansible.cfg.
Note that environment variables take precedence over values in ansible.cfg, which ensures that the essential configuration required by Foreman is retained.
The following table lists the essential Ansible configuration options managed by Foreman.
| Environment Variable | Config Key | Description |
|---|---|---|
ANSIBLE_CALLBACKS_ENABLED |
callbacks_enabled |
Enables callback to Foreman; equivalent to |
ANSIBLE_CALLBACK_WHITELIST |
callback_whitelist |
Enables callback to Foreman; equivalent to |
ANSIBLE_COLLECTIONS_PATHS |
collections_paths |
List of paths to Ansible collections |
ANSIBLE_HOST_KEY_CHECKING |
host_key_checking |
Disables checking of host keys during SSH connection |
ANSIBLE_LOCAL_TEMP |
local_tmp |
Temporary directory on Smart Proxy |
ANSIBLE_ROLES_PATH |
roles_path |
List of paths to Ansible roles |
ANSIBLE_SSH_ARGS |
ssh_args |
Arguments passed to SSH connection |