Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated guides to avoid connection: local #44227

Merged
merged 1 commit into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/docsite/rst/dev_guide/developing_modules_general.rst
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ Ansible playbook.
- Add the following to the new playbook file::

- name: test my new module
connection: local
hosts: localhost
tasks:
- name: run the new module
Expand Down
96 changes: 49 additions & 47 deletions docs/docsite/rst/scenario_guides/guide_cloudstack.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ and fulfill the missing data by either setting ENV variables or tasks params:
---
- name: provision our VMs
hosts: cloud-vm
connection: local
tasks:
- name: ensure VMs are created and running
delegate_to: localhost
cs_instance:
api_key: your api key
api_secret: your api secret
Expand Down Expand Up @@ -111,10 +111,11 @@ By passing the argument ``api_region`` with the CloudStack modules, the region w
.. code-block:: yaml

- name: ensure my ssh public key exists on Exoscale
local_action: cs_sshkeypair
cs_sshkeypair:
name: my-ssh-key
public_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
api_region: exoscale
delegate_to: localhost

Or by looping over a regions list if you want to do the task in every region:

Expand Down Expand Up @@ -144,20 +145,19 @@ Below you see an example how it can be used in combination with Ansible's block
tasks:
- block:
- name: ensure my ssh public key
local_action:
module: cs_sshkeypair
cs_sshkeypair:
name: my-ssh-key
public_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

- name: ensure my ssh public key
local_action:
module: cs_instance:
cs_instance:
display_name: "{{ inventory_hostname_short }}"
template: Linux Debian 7 64-bit 20GB Disk
service_offering: "{{ cs_offering }}"
ssh_key: my-ssh-key
state: running

delegate_to: localhost
environment:
CLOUDSTACK_DOMAIN: root/customers
CLOUDSTACK_PROJECT: web-app
Expand Down Expand Up @@ -241,28 +241,30 @@ Now to the fun part. We create a playbook to create our infrastructure we call i
---
- name: provision our VMs
hosts: cloud-vm
connection: local
tasks:
- name: ensure VMs are created and running
cs_instance:
name: "{{ inventory_hostname_short }}"
template: Linux Debian 7 64-bit 20GB Disk
service_offering: "{{ cs_offering }}"
state: running

- name: ensure firewall ports opened
cs_firewall:
ip_address: "{{ public_ip }}"
port: "{{ item.port }}"
cidr: "{{ item.cidr | default('0.0.0.0/0') }}"
loop: "{{ cs_firewall }}"
when: public_ip is defined

- name: ensure static NATs
cs_staticnat: vm="{{ inventory_hostname_short }}" ip_address="{{ public_ip }}"
when: public_ip is defined

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.
- name: run all enclosed tasks from localhost
delegate_to: localhost
block:
- name: ensure VMs are created and running
cs_instance:
name: "{{ inventory_hostname_short }}"
template: Linux Debian 7 64-bit 20GB Disk
service_offering: "{{ cs_offering }}"
state: running

- name: ensure firewall ports opened
cs_firewall:
ip_address: "{{ public_ip }}"
port: "{{ item.port }}"
cidr: "{{ item.cidr | default('0.0.0.0/0') }}"
loop: "{{ cs_firewall }}"
when: public_ip is defined

- name: ensure static NATs
cs_staticnat: vm="{{ inventory_hostname_short }}" ip_address="{{ public_ip }}"
when: public_ip is defined

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.

Expand Down Expand Up @@ -316,7 +318,6 @@ The playbook looks like the following:
---
- name: cloud base setup
hosts: localhost
connection: local
tasks:
- name: upload ssh public key
cs_sshkeypair:
Expand Down Expand Up @@ -349,26 +350,27 @@ The playbook looks like the following:

- name: install VMs in the cloud
hosts: cloud-vm
connection: local
tasks:
- name: create and run VMs on CloudStack
cs_instance:
name: "{{ inventory_hostname_short }}"
template: Linux Debian 7 64-bit 20GB Disk
service_offering: "{{ cs_offering }}"
security_groups: "{{ cs_securitygroups }}"
ssh_key: defaultkey
state: Running
register: vm

- name: show VM IP
debug: msg="VM {{ inventory_hostname }} {{ vm.default_ip }}"

- name: assign IP to the inventory
set_fact: ansible_ssh_host={{ vm.default_ip }}

- name: waiting for SSH to come up
wait_for: port=22 host={{ vm.default_ip }} delay=5
- delegate_to: localhost
block:
- name: create and run VMs on CloudStack
cs_instance:
name: "{{ inventory_hostname_short }}"
template: Linux Debian 7 64-bit 20GB Disk
service_offering: "{{ cs_offering }}"
security_groups: "{{ cs_securitygroups }}"
ssh_key: defaultkey
state: Running
register: vm

- name: show VM IP
debug: msg="VM {{ inventory_hostname }} {{ vm.default_ip }}"

- name: assign IP to the inventory
set_fact: ansible_ssh_host={{ vm.default_ip }}

- name: waiting for SSH to come up
wait_for: port=22 host={{ vm.default_ip }} delay=5

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.

Expand Down
3 changes: 1 addition & 2 deletions docs/docsite/rst/scenario_guides/guide_docker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ examples to get you started:

# Simple playbook to invoke with the above example:

- name: Test docker_inventory
- name: Test docker_inventory, this will not connect to any hosts
hosts: all
connection: local
gather_facts: no
tasks:
- debug: msg="Container - {{ inventory_hostname }}"
Expand Down