About this lab
The purpose of this lab is to show the use of an inventory for Ansible to build a multi-host playbook.
vTeam Specialization Program
Pure Storage nominated me recently to join the Pure Storage vTeam Specialization program for New Stack. The idea behind the program is, to create an active community within Pure Storage. Allowing Puritans to learn and develop their skills and grow into a subject matter experts.
The program consists of training and lab exercises that are focussed on developing experience in the New Stack space (Kubernetes, Ansible, Open Stack and more).
Since I think there are more people out there how want to learn more about New Stack, I will blog my progress in a series of lab exercises.
|Description:||Create a playbook to provision across multiple hosts|
|Objective:||Be able to provision storage for multiple hosts using a single playbook|
|Task #1:||Ensure Ansible remote communication to all hosts is configured|
|Task #2:||Create an inventory file|
|Task #3:||Create a playbook that uses the inventory|
|Task #4:||Install the purestorage python SDK on all hosts|
|Success Criteria:||The python SDK is installed on all targeted hosts|
For this lab I’ll be using a control host and some Ubuntu Linux host I’ve created in an earlier lab. Read about that lab here.
Create our Ansible inventory
To be able to run an Ansible playbook against multiple target hosts, we need to start by creating an inventory file. The inventory will allow us to target the hosts in the inventory and execute Ansible modules against them. Here you can read more on how to create an inventory. For now I’ll just keep it sweet and short, showing what I’ve created below:
all: hosts: node1: ansible_host: 192.168.10.121 ansible_user: dnix node2: ansible_host: 192.168.10.122 ansible_user: dnix node3: ansible_host: 192.168.10.123 ansible_user: dnix children: flasharray: hosts: node1: node2: flashblade: hosts: node2: node3:
Lines 2 – 11 we specify a list of all the hosts we use. For each host we specify the IP address and the user to connect with. This way I do not depend on DNS for the resolution of the hostname.
I’ve also specified the ansible_user. This is only necessary if the user that runs the playbook is different from the user that need to be used for the target hosts.
Next, I specified two groups on lines 13 – 20, in the
children section called
flashblade. Each group only contains two out of the three hosts. As a result, we can now use these groups in our Playbooks to target only a subset of our hosts for specific tasks.
Ansible playbook using inventory
To use our Inventory in Ansible, we will use the following, quite simple, playbook. It contains to plays, as shown below, the first of which installed the purestorage SDK on hosts that are in the flasharray group and the second installs the purity-fb SDK on hosts in the flashblade group.
- name: Install FlashArray SDK hosts: flasharray gather_facts: true tasks: - name: Install purestorage SDK pip: name: purestorage - name: Install FlashBlade SDK hosts: flashblade gather_facts: true tasks: - name: Install purity-fb SDK pip: name: purity-fb
hosts: parameter we specify which hosts to target. In our earlier playbooks we used
localhost, since the Pure Storage module are (generally) ran from the same host that the playbook is executed on. Now that we are using an inventory, we can also specify
all to target all hosts in the inventory or
flashblade to target only the hosts in those groups.
The task that we execute is using the module
pip to install the package
purity-fb. That is it, not much to it.
We can kick the playbook of using:
ansible-playbook -i inventory.yaml install_sdk.yaml
-i parameter points to out inventory file and the
install_sdk.yaml is the name I gave my playbook.
And that is it, the first play (lines 1-7) will only target hosts
node2, the second play (lines 8-14) will taget hosts
As you have seen, this lab was a quick one. We used Ansible to target multiple hosts to install some software. By using an inventory file we can quickly target a large group of hosts to execute tasks on all of these hosts. As a result we were able to target smaller set of hosts for specific plays, by using groups. This provides the flexibility to target the correct set of hosts for the correct plays.