# # This task is the thing that creates a digital ocean droplet # - name: Make sure ssh key exists digital_ocean: state: present command: ssh name: kevins-key ssh_pub_key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCZqnD4XzEEiEGju3x31RtvnAE3JGCAVvSLwsW8k2VfMhFBPemoK9nna0j+UK/J6ApMaqKT9RGFM+nQnyIdbxuRIygm3R56DHOiLyekWlWWe/mivefc6o7iSf6oFjSYO3aL4Z6/cN/wUusRxyJOlO2Iv90WF4wueoAUQEl1ZBPiAf8JD77zll+HJrU+98QxKI3ttbonsdWHK/OUlr1CXQjaQlrL5j5MwD9oF+0GCcYEdHd9KKTJoGfANf64P5JMm3RGqfTSEdNUNLi8b6RLgybomy8Hx1bnNf0y+WJwsSA1dMThHVWVuivloX6HvkySlzhhIGp6IxG6kMUPqSZYfZMytzOL4zy2RaKcTu54CbqqMRUJnMrTrRdqQs3+QlM2QAmhmmyw0n67CslY/4x01JIgVNvySPnYwOpLrsd9ATdr9vXeBrStZdmRlOGoHCj97h5P/OvwNx9Suhwf0GXLgJvWDPtuTzaTRewqm9SntcDaW5w9WuG90BoogLbfpKoHmhzVVugrHfbV096ysAAeDqdLv27kZgpgjjuQzTYQ0ukj8F3l3fjqMN+S5IXnHUddR/H6eYm3wf8bcG99kVby9NKQGbTjPly/C8aRGzdI3eBv8KE7/fdjhwzZy+4c7+w7CSyRGKDS2pJER4JJXHMko4y/5dbCBLQiRsRp+dFFRyZZdw== kevin" api_token: "{{ digitialocean_api_token }}" delegate_to: localhost register: droplet_ssh - debug: msg="SSH key ID is {{ droplet_ssh.ssh_key.id }}" - name: See if the instance is up already 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 the instance at digital ocean digital_ocean: state: present command: droplet name: "{{ inventory_hostname }}" api_token: "{{ digitialocean_api_token }}" id: "{{ digitialocean_id }}" size_id: 1gb region_id: sfo1 image_id: fedora-25-x64 wait_timeout: 500 ipv6: yes ssh_key_ids: "{{ droplet_ssh.ssh_key.id }}" delegate_to: localhost register: droplet_info when: host_is_up|failed - debug: msg="ID is {{ droplet_info.droplet.id }}" when: host_is_up|failed - debug: msg="IP is {{ droplet_info.droplet.ip_address }}" when: host_is_up|failed - name: make sure there is no old ssh host key for the host still around local_action: known_hosts path={{item}} host={{ inventory_hostname }} state=absent ignore_errors: True with_items: - /home/kevin/.ssh/known_hosts delegate_to: localhost when: host_is_up|failed - name: gather ssh host key from new instance local_action: command ssh-keyscan -t rsa {{ inventory_hostname }} ignore_errors: True register: hostkey delegate_to: localhost when: host_is_up|failed - name: add new ssh host key local_action: known_hosts path={{item}} key="{{ hostkey.stdout }}" host={{ inventory_hostname }} state=present ignore_errors: True with_items: - /home/kevin/.ssh/known_hosts when: host_is_up|failed