summaryrefslogtreecommitdiffstats
path: root/tasks/persistent_cloud.yml
blob: 6658ada12174726ad46d777c3d22594c382167ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# New tasks to spin up instance in https://fedorainfracloud.org

- name: check it out
  local_action: shell nc -w 5 {{ inventory_hostname }} 22 < /dev/null
  register: host_is_up
  ignore_errors: true
  changed_when: false
  check_mode: no

- name: spin UP VM using nova_compute
  become: False
  local_action:
      module: nova_compute
      auth_url: "{{os_auth_url}}"
      login_username: "admin"
      login_password: "{{ADMIN_PASS}}"
      login_tenant_name: "{{inventory_tenant}}"
      name: "{{inventory_instance_name}}"
      image_id: "{{ image|image_name_to_id('admin', ADMIN_PASS, inventory_tenant, os_auth_url) }}"
      wait_for: 300
      flavor_id: "{{ instance_type|flavor_name_to_id('admin', ADMIN_PASS, inventory_tenant, os_auth_url) }}"
      security_groups: "{{security_group}}"
      key_name: "{{ keypair }}"
      nics: "{{ cloud_networks }}"
      user_data: "#cloud-config\ndisable_root: 0"
      floating_ips:
        - "{{public_ip}}"
  register: nova_result
  when: host_is_up|failed

# instance can be both id and name, volume must be id
# volume must be id
#
# Check that the volume is available
#
- local_action: shell nova --os-auth-url="{{os_auth_url}}" --os-username="admin" --os-password="{{ADMIN_PASS}}" --os-tenant-name={{inventory_tenant}} volume-list | grep ' {{item.volume_id}} ' | grep 'available'
  with_items: "{{ volumes|default([]) }}"
  register: volume_available
  failed_when: volume_available.rc == 2
  changed_when: volume_available.rc == 0
  ignore_errors: True
  when: volumes is defined
  check_mode: no

#
# If it is attach it.
#
- local_action: shell nova --os-auth-url="{{os_auth_url}}" --os-username="admin" --os-password="{{ADMIN_PASS}}" --os-tenant-name={{inventory_tenant}} volume-attach "{{inventory_instance_name}}" "{{item.volume_id}}" "{{item.device}}"
  with_items: "{{ volumes|default([]) }}"
  ignore_errors: True
  failed_when: False
  when: volumes is defined and volume_available is defined and volume_available

- name: wait for he host to be hot
  local_action: wait_for host={{ public_ip }} port=22 delay=1 timeout=600
  when: host_is_up|failed

# SSH is up and running, however cloud-init still did not deployed ssh keypair
# we have to wait some time. 10 sec is usually enough, but not always.

- name: waiting for cloud-init
  pause: seconds=30
  when: host_is_up|failed

#
# Next we try and gather facts. If the host doesn't have python2 this will fail.
#

- name: gather facts
  setup:
  check_mode: no
  ignore_errors: True
  register: facts

#
# If that failed, then we use the raw module to install things
#

- name: install python2 and dnf stuff
  raw: dnf -y install python-dnf libselinux-python
  when: facts|failed

# TODO - somehow guess when keypair is finally deployed and return little bit earlier
## We need to specify user, here we trying with fedora or root
#- name: wait until ssh is available
#  # local_action:  shell false; until [ "$?" -eq "0" ]; do sleep 2; ssh -o PasswordAuthentication=no fedora@{{ public_ip }} 'echo foobar'  || ssh -o PasswordAuthentication=no root@{{ public_ip }} 'echo foobar'; done
#  # local_action: shell false; until [ "$?" -eq "0" ]; do sleep 2; ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no fedora@{{ public_ip }} 'echo foobar'; done
#  local_action: shell whoami && ssh -vvvv -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no fedora@{{ public_ip }} 'echo foobar'
#  # async: 20
#  # poll: 5
#  ignore_errors: True
#
- name: Include SSH config
  include_role: name=basessh