In the above play we defined 3 tasks and use the group ``cloud-vm`` as target to handle all VMs in the cloud but instead SSH to these VMs, we use ``connection=local`` to execute the API calls locally from our workstation.
In the above play we defined 3 tasks and use the group ``cloud-vm`` as target to handle all VMs in the cloud but instead SSH to these VMs, we use ``delegate_to: localhost`` to execute the API calls locally from our workstation.
In the first task, we ensure we have a running VM created with the Debian template. If the VM is already created but stopped, it would just start it. If you like to change the offering on an existing VM, you must add ``force: yes`` to the task, which would stop the VM, change the offering and start the VM again.
In the first task, we ensure we have a running VM created with the Debian template. If the VM is already created but stopped, it would just start it. If you like to change the offering on an existing VM, you must add ``force: yes`` to the task, which would stop the VM, change the offering and start the VM again.
@ -316,7 +318,6 @@ The playbook looks like the following:
---
---
- name: cloud base setup
- name: cloud base setup
hosts: localhost
hosts: localhost
connection: local
tasks:
tasks:
- name: upload ssh public key
- name: upload ssh public key
cs_sshkeypair:
cs_sshkeypair:
@ -349,26 +350,27 @@ The playbook looks like the following:
In the first play we setup the security groups, in the second play the VMs will created be assigned to these groups. Further you see, that we assign the public IP returned from the modules to the host inventory. This is needed as we do not know the IPs we will get in advance. In a next step you would configure the DNS servers with these IPs for accessing the VMs with their DNS name.
In the first play we setup the security groups, in the second play the VMs will created be assigned to these groups. Further you see, that we assign the public IP returned from the modules to the host inventory. This is needed as we do not know the IPs we will get in advance. In a next step you would configure the DNS servers with these IPs for accessing the VMs with their DNS name.
For more information see :ref:`Implicit Localhost <implicit_localhost>`
In playbook steps, we'll typically be using the following pattern:
In playbook steps, we'll typically be using the following pattern:
..code-block:: yaml
..code-block:: yaml
- hosts: localhost
- hosts: localhost
connection: local
gather_facts: False
gather_facts: False
tasks:
tasks:
@ -103,7 +104,7 @@ Here is a basic example of provisioning an instance in ad-hoc mode:
..code-block:: bash
..code-block:: bash
$ ansible localhost -m rax -a "name=awx flavor=4 image=ubuntu-1204-lts-precise-pangolin wait=yes" -c local
$ ansible localhost -m rax -a "name=awx flavor=4 image=ubuntu-1204-lts-precise-pangolin wait=yes"
Here's what it would look like in a playbook, assuming the parameters were defined in variables:
Here's what it would look like in a playbook, assuming the parameters were defined in variables:
@ -111,8 +112,7 @@ Here's what it would look like in a playbook, assuming the parameters were defin
tasks:
tasks:
- name: Provision a set of instances
- name: Provision a set of instances
local_action:
rax:
module: rax
name: "{{ rax_name }}"
name: "{{ rax_name }}"
flavor: "{{ rax_flavor }}"
flavor: "{{ rax_flavor }}"
image: "{{ rax_image }}"
image: "{{ rax_image }}"
@ -120,14 +120,14 @@ Here's what it would look like in a playbook, assuming the parameters were defin
group: "{{ group }}"
group: "{{ group }}"
wait: yes
wait: yes
register: rax
register: rax
delegate_to: localhost
The rax module returns data about the nodes it creates, like IP addresses, hostnames, and login passwords. By registering the return value of the step, it is possible used this data to dynamically add the resulting hosts to inventory (temporarily, in memory). This facilitates performing configuration actions on the hosts in a follow-on task. In the following example, the servers that were successfully created using the above task are dynamically added to a group called "raxhosts", with each nodes hostname, IP address, and root password being added to the inventory.
The rax module returns data about the nodes it creates, like IP addresses, hostnames, and login passwords. By registering the return value of the step, it is possible used this data to dynamically add the resulting hosts to inventory (temporarily, in memory). This facilitates performing configuration actions on the hosts in a follow-on task. In the following example, the servers that were successfully created using the above task are dynamically added to a group called "raxhosts", with each nodes hostname, IP address, and root password being added to the inventory.
..code-block:: yaml
..code-block:: yaml
- name: Add the instances we created (by public IP) to the group 'raxhosts'
- name: Add the instances we created (by public IP) to the group 'raxhosts'
local_action:
add_host:
module: add_host
hostname: "{{ item.name }}"
hostname: "{{ item.name }}"
ansible_host: "{{ item.rax_accessipv4 }}"
ansible_host: "{{ item.rax_accessipv4 }}"
ansible_ssh_pass: "{{ item.rax_adminpass }}"
ansible_ssh_pass: "{{ item.rax_adminpass }}"
@ -303,11 +303,11 @@ This can be achieved with the ``rax_facts`` module and an inventory file similar
gather_facts: False
gather_facts: False
tasks:
tasks:
- name: Get facts about servers
- name: Get facts about servers
local_action:
rax_facts:
module: rax_facts
credentials: ~/.raxpub
credentials: ~/.raxpub
name: "{{ inventory_hostname }}"
name: "{{ inventory_hostname }}"
region: "{{ rax_region }}"
region: "{{ rax_region }}"
delegate_to: localhost
- name: Map some facts
- name: Map some facts
set_fact:
set_fact:
ansible_host: "{{ rax_accessipv4 }}"
ansible_host: "{{ rax_accessipv4 }}"
@ -418,21 +418,19 @@ Create an isolated cloud network and build a server
- name: Build Servers on an Isolated Network
- name: Build Servers on an Isolated Network
hosts: localhost
hosts: localhost
connection: local
gather_facts: False
gather_facts: False
tasks:
tasks:
- name: Network create request
- name: Network create request
local_action:
rax_network:
module: rax_network
credentials: ~/.raxpub
credentials: ~/.raxpub
label: my-net
label: my-net
cidr: 192.168.3.0/24
cidr: 192.168.3.0/24
region: IAD
region: IAD
state: present
state: present
delegate_to: localhost
- name: Server create request
- name: Server create request
local_action:
rax:
module: rax
credentials: ~/.raxpub
credentials: ~/.raxpub
name: web%04d.example.org
name: web%04d.example.org
flavor: 2
flavor: 2
@ -449,6 +447,7 @@ Create an isolated cloud network and build a server
wait: yes
wait: yes
wait_timeout: 360
wait_timeout: 360
register: rax
register: rax
delegate_to: localhost
.._complete_environment:
.._complete_environment:
@ -462,12 +461,10 @@ Build a complete webserver environment with servers, custom networks and load ba
---
---
- name: Build environment
- name: Build environment
hosts: localhost
hosts: localhost
connection: local
gather_facts: False
gather_facts: False
tasks:
tasks:
- name: Load Balancer create request
- name: Load Balancer create request
local_action:
rax_clb:
module: rax_clb
credentials: ~/.raxpub
credentials: ~/.raxpub
name: my-lb
name: my-lb
port: 80
port: 80
@ -483,8 +480,7 @@ Build a complete webserver environment with servers, custom networks and load ba
register: clb
register: clb
- name: Network create request
- name: Network create request
local_action:
rax_network:
module: rax_network
credentials: ~/.raxpub
credentials: ~/.raxpub
label: my-net
label: my-net
cidr: 192.168.3.0/24
cidr: 192.168.3.0/24
@ -493,8 +489,7 @@ Build a complete webserver environment with servers, custom networks and load ba
register: network
register: network
- name: Server create request
- name: Server create request
local_action:
rax:
module: rax
credentials: ~/.raxpub
credentials: ~/.raxpub
name: web%04d.example.org
name: web%04d.example.org
flavor: performance1-1
flavor: performance1-1
@ -513,8 +508,7 @@ Build a complete webserver environment with servers, custom networks and load ba
register: rax
register: rax
- name: Add servers to web host group
- name: Add servers to web host group
local_action:
add_host:
module: add_host
hostname: "{{ item.name }}"
hostname: "{{ item.name }}"
ansible_host: "{{ item.rax_accessipv4 }}"
ansible_host: "{{ item.rax_accessipv4 }}"
ansible_ssh_pass: "{{ item.rax_adminpass }}"
ansible_ssh_pass: "{{ item.rax_adminpass }}"
@ -524,8 +518,7 @@ Build a complete webserver environment with servers, custom networks and load ba
when: rax.action == 'create'
when: rax.action == 'create'
- name: Add servers to Load balancer
- name: Add servers to Load balancer
local_action:
rax_clb_nodes:
module: rax_clb_nodes
credentials: ~/.raxpub
credentials: ~/.raxpub
load_balancer_id: "{{ clb.balancer.id }}"
load_balancer_id: "{{ clb.balancer.id }}"
address: "{{ item.rax_networks.private|first }}"
address: "{{ item.rax_networks.private|first }}"
@ -578,12 +571,10 @@ Using a Control Machine
- name: Create an exact count of servers
- name: Create an exact count of servers
hosts: localhost
hosts: localhost
connection: local
gather_facts: False
gather_facts: False
tasks:
tasks:
- name: Server build requests
- name: Server build requests
local_action:
rax:
module: rax
credentials: ~/.raxpub
credentials: ~/.raxpub
name: web%03d.example.org
name: web%03d.example.org
flavor: performance1-1
flavor: performance1-1
@ -598,8 +589,7 @@ Using a Control Machine
register: rax
register: rax
- name: Add servers to in memory groups
- name: Add servers to in memory groups
local_action:
add_host:
module: add_host
hostname: "{{ item.name }}"
hostname: "{{ item.name }}"
ansible_host: "{{ item.rax_accessipv4 }}"
ansible_host: "{{ item.rax_accessipv4 }}"
ansible_ssh_pass: "{{ item.rax_adminpass }}"
ansible_ssh_pass: "{{ item.rax_adminpass }}"
@ -613,37 +603,38 @@ Using a Control Machine
hosts: new_web
hosts: new_web
gather_facts: false
gather_facts: false
tasks:
tasks:
- name: Wait for rackconnnect automation to complete
- name: ensure we run all tasks from localhost
local_action:
delegate_to: localhost
module: rax_facts
block:
credentials: ~/.raxpub
- name: Wait for rackconnnect automation to complete