Course Development Director in Content
Ansible is a powerful automation tool that can be used for managing configuration state or even performing coordinated multi-system deployments. This activity is intended to provide the steps to get started with a simple Ansible configuration.
We have been tasked with putting together a presentation to demonstrate how Ansible may be used to install software on remote hosts automatically.
Install Ansible on the Control Host
Log in to the Control Host server using
cloud_user, and the provided Public IP address and password:
ssh cloud_user@<PUBLIC IP>
epel-release and enter
y when prompted:
sudo yum install epel-release
Install Ansible and enter
y when prompted:
sudo yum install ansible
Now, we need to create the
ansible user on both the control host and workstation host.
On the control node:
sudo useradd ansible
Connect to the workstation node using the provided password:
ansible user to the workstation and set a password for the
ansible user. We need to make sure it is something we will remember:
sudo useradd ansible sudo passwd ansible logout
Configure a Pre-Shared Key for Ansible
With our user created, we need to create a pre-shared key that allows the user to log in from
workstation without a password.
Change to the
sudo su - ansible
Generate a new SSH key, accepting the default settings when prompted:
Copy the SSH key to
workstation, providing the password we created earlier:
Test that we no longer need a password to log in to the
Once we succeed at logging in, log out of
Configure the Ansible User on the Workstation Host
Our next job is to configure the
ansible user on the workstation host so that that they may
sudo without a password.
Log in to the workstation host as
cloud_user using the password provided by the lab:
Add this line at the end of the file:
ansible ALL=(ALL) NOPASSWD: ALL
Save the file:
Log out of
Create a Simple Inventory
Next, we need to create a simple inventory,
/home/ansible/inventory, consisting of only the
control host, as the
ansible user, run the following commands:
Add the text "workstation" to the file and save using
Write an Ansible Playbook
We need to write an Ansible playbook into
/home/ansible/git-setup.yml on the control node that installs
workstation, then executes the playbook.
On the control host, as the
ansible user, create an Ansible playbook:
Add the following text to the file:
--- # install git on target host - hosts: workstation become: yes tasks: - name: install git yum: name: git state: latest
Save and exit the file (
:wq in vim).
Run the playbook:
ansible-playbook -i /home/ansible/inventory /home/ansible/git-setup.yml
Verify that the playbook ran successfully:
ssh workstation which git
By completing this lab, we now have a better understanding of how to deploy Ansible, create a connection between two nodes, create a simple inventory, and how to write an Ansible playbook. Congratulations on completing this lab!