Red Hat Enterprise Linux Diagnostics and Troubleshooting
Configure a system with Ansible as a configuration manager.
Outcomes
You should be able to install and configure the web service on the system by using Ansible.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
[student@workstation ~]$ lab start baseline-configmanagement
This command confirms that the required hosts for this exercise are accessible and gets the templates for the web service configuration.
Instructions
In this exercise, you configure workstation as an Ansible control node, and then install and configure the web service on the servera managed node.
On
workstationasstudentuser, create theworkdirdirectory. Change directory to it.[student@workstation ~]$
mkdir workdir[student@workstation ~]$cd workdirCreate an
ansible.cfgconfiguration file.[defaults] inventory = inventory remote_user = root host_key_checking = False deprecation_warnings = False
Create the
inventoryfile and setserveraas theweb_prodproduction web server.[webservers]
web_prod ansible_host=serveraVerify connectivity with the
web_prodmanaged node.[student@workstation workdir]$
ansible web_prod -m pingweb_prod | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "ping": "pong" }Move the downloaded templates to the
workdirdirectory and view the included substitution variables.[student@workstation workdir]$
mv ~/apache_* .[student@workstation workdir]$grep "{{" apache_httpdconf.j2# {{ ansible_managed }} [student@workstation workdir]$grep "{{" apache_indexhtml.j2<!-- {{ ansible_managed }} --> Hello from {{ inventory_hostname }}Create the
mywebserver.yamlAnsible Playbook for the web service configuration, to include these tasks:Install
httpdpackage if not installed.Use the templates as the web service configuration files.
Ensure that the firewall service is enabled for the web service.
Set the managed hosts to configure. It is not necessary to gather facts for these tasks.
- name: Install and configure a customized web server hosts: webservers gather_facts: False
Configure tasks to verify that the
httpdservice is the latest version, and verify the presence of thefirewalldservice.- name: Install httpd package ansible.builtin.yum: name: httpd state: latest - name: Validate firewall ansible.builtin.yum: name: firewalld state: presentConfigure tasks to inject templates as the
httpdconfiguration files.- name: Template out httpd configuration file template: src: apache_httpdconf.j2 dest: /etc/httpd/conf/httpd.conf owner: root group: root mode: '0444' - name: Template out httpd index file template: src: apache_indexhtml.j2 dest: /var/www/html/index.html owner: root group: root mode: '0444'Configure tasks to ensure that the
httpdandfirewalldservices are active and enabled at system startup.- name: Start and enable httpd daemon ansible.builtin.service: name: httpd state: started enabled: true - name: Start and enable firewalld daemon ansible.builtin.service: name: firewalld state: started enabled: trueConfigure a task to ensure that the
httpservice is allowed in the firewall.- name: Open http firewalld port firewalld: service: http immediate: yes permanent: yes state: enabledVerify that the final Ansible Playbook
mywebserver.yamlcontains this content:- name: Install and configure a customized web server hosts: webservers gather_facts: False tasks: - name: Install httpd package ansible.builtin.yum: name: httpd state: latest - name: Validate firewall ansible.builtin.yum: name: firewalld state: present - name: Template out httpd configuration file template: src: apache_httpdconf.j2 dest: /etc/httpd/conf/httpd.conf owner: root group: root mode: '0444' - name: Template out httpd index file template: src: apache_indexhtml.j2 dest: /var/www/html/index.html owner: root group: root mode: '0444' - name: Start and enable httpd daemon ansible.builtin.service: name: httpd state: started enabled: true - name: Start and enable firewalld daemon ansible.builtin.service: name: firewalld state: started enabled: true - name: Open http firewalld port firewalld: service: http immediate: yes permanent: yes state: enabledNote
You can verify the syntax of an Ansible Playbook without implementing it.
[student@workstation workdir]$
ansible-playbook --syntax-check mywebserver.yamlplaybook: mywebserver.yaml
Run the Ansible Playbook and verify its successful execution.
[student@workstation workdir]$
ansible-playbook mywebserver.yamlPLAY [Install and configure a customized web server] ********************* TASK [Install httpd package] ********************************************* changed: [web_prod] TASK [Validate firewall] ************************************************* ok: [web_prod] TASK [Template out httpd configuration file] ***************************** changed: [web_prod] TASK [Template out httpd index file] ************************************* ok: [web_prod] TASK [Start and enable httpd daemon] ************************************* changed: [web_prod] TASK [Start and enable firewalld daemon] ********************************* ok: [web_prod] TASK [Open http firewalld port] ****************************************** changed: [web_prod] PLAY RECAP *************************************************************** web_prod :ok=7changed=5 unreachable=0failed=0skipped=0 rescued=0 ignored=0Verify that the web server can be accessed.
[student@workstation workdir]$
curl servera<!-- ansible managed --> <html> <head><title>Apache is running!</title></head> <body> <h1> Hello from web_prod </h1> </body> </html> [student@workstation workdir]$cd ~