This course is using an outdated version of the technology and is now considered to be Legacy content. It will be removed from our catalog on June 28, 2024. Please be sure to complete your course and finish any remaining labs before that date. We recommend moving to version 9.2, which is the latest version currently available.
Performance Checklist
In this lab, you will ensure your workstation is prepared to use Ansible and has been configured with an appropriate configuration file and inventory, and use a provided playbook in order to ensure that several servers are in the correct configuration.
Outcomes
You should be able to:
Install and configure Ansible.
Confirm that Ansible is working correctly and can connect to managed hosts, using ad hoc commands.
Run Ansible Playbooks to configure managed hosts.
Verify that workstation, servera, and serverb are started.
Log in to workstation as student using student as the password.
From workstation, run lab ansible-review setup to verify that the environment is ready.
[student@workstation ~]$lab ansible-review setup
On workstation, install the ansible package.
On workstation as the student user, install the ansible package administratively using the sudo command.
[student@workstation ~]$sudo yum install ansible[sudo] password for student:student...output omitted... ====================================================== Package Arch Version Repository Size ====================================================== Installing: ansible noarch 2.5.5-1.el7ae ansible 9.1M Transaction Summary ====================================================== Install 1 Package Total download size: 9.1 M Installed size: 46 MIs this ok [y/d/N]:y...output omitted... Installed: ansible.noarch 0:2.5.5-1.el7ae Complete!
On workstation as ansible-testuser, create the /home/ansible-testuser/lab directory.
In the lab directory, create an inventory file that defines the webservers host group.
This host group includes two managed hosts, servera.lab.example.com, and serverb.lab.example.com.
The password of ansible-testuser is redhat.
Switch to the ansible-testuser user.
Use redhat as the password.
[student@workstation ~]$su - ansible-testuserPassword:redhatLast login: Wed Aug 1 07:53:19 IST 2018 on pts/0[ansible-testuser@workstation ~]$
Create the /home/ansible-testuser/lab directory.
[ansible-testuser@workstation ~]$mkdir ~/lab
Create an inventory file in the lab directory.
This inventory file contains the webservers host group which includes two managed hosts, servera.lab.example.com, and serverb.lab.example.com.
[ansible-testuser@workstation ~]$cd ~/lab[ansible-testuser@workstation lab]$vi inventory[ansible-testuser@workstation lab]$cat inventory[webservers] servera.lab.example.com serverb.lab.example.com
In /home/ansible-testuser/lab, create an Ansible configuration file that uses the inventory file previously created.
It should use the ansible-testuser user account to log in to the remote managed hosts.
Configure the privilege escalation settings such that it uses sudo to perform tasks as root on the remote managed hosts.
Ansible should prompt the user for the sudo password.
In the lab directory, create an Ansible configuration file which uses the inventory file previously created.
You need to define the ansible-testuser user as the remote user, and enable Ansible to ask for a password to log in to the managed host.
Finally, you need to configure privilege escalation with sudo and root as the remote user, and enable Ansible to ask for a password to become root in the managed host.
[ansible-testuser@workstation lab]$vi ansible.cfg[ansible-testuser@workstation lab]$cat ansible.cfg[defaults] inventory = ./inventory remote_user = ansible-testuser ask_pass = True [privilege_escalation] become=True become_method=sudo become_user=root become_ask_pass=True
Use an Ansible ad hoc command to confirm that the two managed hosts in the webservers host group are available.
Verify that the httpd package is yet to be installed on either host.
Also, confirm that TCP port 80 is blocked by both hosts' firewall settings.
Confirm that the two managed hosts in the webservers host group are available.
Use the ping Ansible module.
[ansible-testuser@workstation lab]$ansible webservers -m pingSSH password:redhatSUDO password[defaults to SSH password]:redhatserverb.lab.example.com | SUCCESS => { "changed": false, "ping": "pong" } servera.lab.example.com | SUCCESS => { "changed": false, "ping": "pong" }
Use the command Ansible module to confirm that the httpd package is yet to be installed in the managed hosts of the webservers host group.
The following output shows that the httpd packaged is not installed on the managed hosts.
[ansible-testuser@workstation lab]$ansible webservers -m command \>-a "yum list installed httpd"SSH password:redhatSUDO password[defaults to SSH password]:redhat...output omitted... serverb.lab.example.com | FAILED | rc=1 >> Loaded plugins: enabled_repos_upload, langpacks, package_upload, product-id, : search-disabled-repos, subscription-manager Uploading Enabled Repositories Report Loaded plugins: langpacks, product-id, subscription-managerError: No matching Packages to listnon-zero return code servera.lab.example.com | FAILED | rc=1 >> Loaded plugins: enabled_repos_upload, langpacks, package_upload, product-id, : search-disabled-repos, subscription-manager Uploading Enabled Repositories Report Loaded plugins: langpacks, product-id, subscription-managerError: No matching Packages to listnon-zero return code
The ansible webservers -m yum -a "list=httpd" command is an alternative solution that in a number of ways is superior because it uses a purpose-built module instead of depending on command.
However, unless you looked in ansible-doc, you might not know about that module.
Ensure that TCP port 80 is blocked by firewalld on the managed hosts in the webservers host group.
Use the firewalld Ansible module to check the port and the http service definition, and disable access if it is open.
The output shows that neither the port nor the service definition was enabled in firewalld on the managed hosts, since no changes were made.
[ansible-testuser@workstation lab]$ansible webservers -m firewalld \>-a "port=80/tcp state=disabled immediate=true permanent=true"SSH password:redhatSUDO password[defaults to SSH password]:redhatservera.lab.example.com | SUCCESS => { "changed": false, "msg": "Permanent and Non-Permanent(immediate) operation" } serverb.lab.example.com | SUCCESS => { "changed": false, "msg": "Permanent and Non-Permanent(immediate) operation" }[ansible-testuser@workstation lab]$ansible webservers -m firewalld \>-a "service=http state=disabled immediate=true permanent=true"SSH password:redhatSUDO password[defaults to SSH password]:redhatservera.lab.example.com | SUCCESS => { "changed": false, "msg": "Permanent and Non-Permanent(immediate) operation" } serverb.lab.example.com | SUCCESS => { "changed": false, "msg": "Permanent and Non-Permanent(immediate) operation" }
Run the http://materials.example.com/labs/deploy-httpd.yml Ansible Playbook to install, and configure the httpd service on the managed hosts.
When done, verify that the httpd package is installed, and that TCP port 80 is open on those managed hosts.
Download the deploy-httpd.yml Ansible Playbook from http://materials.example.com/labs/deploy-httpd.yml.
[ansible-testuser@workstation lab]$wget \>http://materials.example.com/labs/deploy-httpd.yml...output omitted...
Run the deploy-httpd.yml playbook to deploy the httpd service, and open TCP port 80 on the managed hosts.
[ansible-testuser@workstation lab]$ansible-playbook deploy-httpd.ymlSSH password:redhatSUDO password[defaults to SSH password]:redhat...output omitted... servera.lab.example.com : ok=3 changed=1 unreachable=0 failed=0 serverb.lab.example.com : ok=3 changed=1 unreachable=0 failed=0[ansible-testuser@workstation lab]$
Use the command Ansible module to verify that the httpd package is installed on the managed hosts of the webservers host group.
The output shows that the httpd package is installed on the managed hosts.
[ansible-testuser@workstation lab]$ansible webservers -m command \>-a "yum list installed httpd"SSH password:redhatSUDO password[defaults to SSH password]:redhat[WARNING]: Consider using the yum module rather than running yum. If you need to use command because yum is insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message. servera.lab.example.com | SUCCESS | rc=0 >> Loaded plugins: langpacks, product-id, search-disabled-repos, subscription- : manager This system is not registered with an entitlement server. You can use subscription-manager to register. Installed Packages httpd.x86_64 2.4.6-80.el7 @rhel--server-dvd serverb.lab.example.com | SUCCESS | rc=0 >> Loaded plugins: langpacks, product-id, search-disabled-repos, subscription- : manager This system is not registered with an entitlement server. You can use subscription-manager to register. Installed Packages httpd.x86_64 2.4.6-80.el7 @rhel--server-dvd
Again, the ansible webservers -m yum -a "list=httpd" command is an alternative solution that you could also use to determine this.
Verify that TCP port 80 is open in the firewall settings of the managed hosts of the webservers host group with the command Ansible module.
The output shows that 80/TCP is open in the firewall settings of the managed hosts.
[ansible-testuser@workstation lab]$ansible webservers -m command \>-a "firewall-cmd --list-ports --zone=public"SSH password:redhatSUDO password[defaults to SSH password]:redhatservera.lab.example.com | SUCCESS | rc=0 >> 80/tcp serverb.lab.example.com | SUCCESS | rc=0 >> 80/tcp
You could simply run an ad hoc command with the firewalld module to check the configuration and correct it automatically if it was incorrect.
However, this approach might more clearly show you that the port is already configured on both hosts due to the playbook, without disturbing the systems' current configuration.
Log out as ansible-testuser from workstation.
[ansible-testuser@workstation lab]$logout[student@workstation ~]$
Navigate in a web browser to http://servera.lab.example.com and http://serverb.lab.example.com to verify the configuration.