So far, we created playbooks that first launched an instance, and then we extended this in the previous recipe to subsequently install Apache onto the running instance. This recipe describes a playbook that can launch any number of instances, and install Apache onto each of those instances.
Ensure that you are logged on to a correctly configured OpenStack client and can access the OpenStack environment that has Ansible installed.
We will extend the previous recipe's playbook to add flexibility to include a variable number of instances.
Launch instances on OpenStack
. This complete play is shown as follows, where we introduce a variable called count
, which we have set to 2
, and we also introduce the with_sequence
section, which forms our loop that will execute that task the specified number of count times. Note that we also include the count value as part of the instance name:- name: Launch instances on OpenStack hosts: localhost gather_facts: false vars: count: 2 tasks: - name: Create ansible security group os_security_group: state: present name: ansible verify: false - name: Create a rule to allow SSH connections os_security_group_rule: security_group: ansible protocol: tcp port_range_min: 22 port_range_max: 22 remote_ip_prefix: 0.0.0.0/0 verify: false - name: Create webserver security group os_security_group: state: present name: webserver verify: false - name: Create a rule to allow http connections os_security_group_rule: security_group: webserver protocol: tcp port_range_min: 80 port_range_max: 80 remote_ip_prefix: 0.0.0.0/0 verify: false - name: Deploy an instance os_server: state: present name: cookbook{{ item }} image: xenial-image key_name: demokey timeout: 200 flavor: m1.tiny network: private-net verify: false register: nova_cookbook with_sequence: count={{ count }} - name: Add instance to Inventory add_host: name="{{ item.server.name }}" groups=webservers ansible_ssh_host="{{ item.server.accessIPv4 }}" with_items: "{{ nova_cookbook.results }}"
- name: Wait for port 22 to be ready hosts: webservers gather_facts: False tasks: - local_action: wait_for port=22 host="{{ ansible_ssh_host }}" search_regex=OpenSSH delay=10 - hosts: webservers remote_user: ubuntu become: yes gather_facts: no
The pre_tasks:
section is optional. Your use will vary on any implicit restrictions imposed on the image or environment you are using. This was described in the previous recipe on its use here:
pre_tasks: - name: Set APT proxy raw: echo "Acquire::http::Proxy "http://192.168.1.20:3128";" > /etc/apt/apt.conf - name: 'install python2' raw: sudo apt-get -y install python-simplejson tasks: - name: Ensure Apache is installed apt: name=apache2 state=latest - name: Ensure that Apache is started service: name=apache2 state=started
multi-orchestrate-instances.yml
, you execute this with the following:source openrc ansible-playbook multi-orchestrate-instances.yml
This will bring back an output like the following. This produces more output than the other plays so far, so only the last part is shown:
We have included a few extra items in this playbook that extends the previous playbook that installs Apache to a newly launched instance. These are described here:
vars: count: 2
We introduce a variable called count
, to which we assign the value 2
. This variable is limited to the scope of this particular play (named Launch instances on OpenStack
). This variable is used to form a loop, as indicated by this attribute assigned to the os_server
call:
with_sequence: count={{ count }}
This basically states: run the os_server
module task when count = 1
, and when count = 2
. As we are in a sequence, we have access to the value of count
, in a variable called item
. We use this to append to the name
variable of the instance allowing us to end up with cookbook1
and cookbook2
with the following syntax:
name: cookbook{{ item }}
3.135.247.181