Skip to content

Instantly share code, notes, and snippets.

@displague
Forked from rdhyee/minecraft_do.yml
Last active November 5, 2018 14:07
Show Gist options
  • Save displague/05cb6444589ead1b18bcda588f73d807 to your computer and use it in GitHub Desktop.
Save displague/05cb6444589ead1b18bcda588f73d807 to your computer and use it in GitHub Desktop.
Ansible playbook to launch a Linode instance and then configure it to run Minecraft. Note that some things are hardwired: the label of the instance, the version of minecraft

Requires Ansible in this branch ansible/ansible#46151 (until that is merged .. then devel or master)

$ ansible-playbook  /tmp/minecraft_linode.yml  -e linode_token=$LINODE_TOKEN -e ssh_key="$(cat ~/.ssh/id_rsa.pub)" -e type=g6-nanode-1 -e region=us-east -e image=linode/debian9 -e ansible_ssh_private_key_file=~/.ssh/id_rsa --private-key=~/.ssh/id_rsa
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [launch Linode instance] ************************************************************************************************************************************************************************************************************************************************************************************************

TASK [pwd] *******************************************************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost -> localhost]

TASK [spin up Linode instance] ***********************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost -> localhost]

TASK [print info about my_linode] ********************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost -> localhost] => {
    "msg": "ID is 11273318 IP is ['45.79.149.139']"
}

TASK [Add new linode to host group] ******************************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost -> localhost]

TASK [Wait for SSH to come up] ***********************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost -> localhost]

PLAY [Configure linode for minecraft] ****************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************************************************************
fatal: [45.79.149.139]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey,password).\r\n", "unreachable": true}
	to retry, use: --limit @/tmp/minecraft_linode.retry

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************************************************************
45.79.149.139              : ok=0    changed=0    unreachable=1    failed=0    skipped=0
localhost                  : ok=5    changed=2    unreachable=0    failed=0    skipped=0
# http://www.ansibleworks.com/docs/modules.html#linode_v4
# Create a new linode
# Will return the linode details including the linode label (used for idempotence)
- name: launch Linode instance
hosts: localhost
gather_facts: False
tasks:
- name: pwd
local_action: command pwd
- name: spin up Linode instance
local_action:
module: linode_v4
state=present
label=minecraft1
access_token={{linode_token}}
authorized_keys={{ssh_key}}
type={{type}}
region={{region}}
image={{image}}
register: my_linode
- name: print info about my_linode
local_action:
module: debug
msg="ID is {{ my_linode.data.instance.id }} IP is {{ my_linode.data.instance.ipv4 }}"
- name: Add new linode to host group
local_action:
module: add_host
hostname={{ my_linode.data.instance.ipv4[0] }}
group=launched
ansible_user=root
- name: Wait for SSH to come up
local_action:
module: wait_for
host={{ my_linode.data.instance.ipv4[0] }}
port=22
search_regex=OpenSSH
timeout=320
- name: Configure linode for minecraft
hosts: launched
gather_facts: True
tasks:
- name: apply apt-get update --fix-missing
command: apt-get update --fix-missing
become_user: root
become_method: sudo
- name: install java
apt: pkg=default-jdk
become_user: root
become_method: sudo
- name: install screen
apt: pkg=screen
become_user: root
become_method: sudo
- name: make the minecraft directory
file: state=directory path=/root/minecraft
- name: download minecraft (1.6.4)
command: wget https://s3.amazonaws.com/Minecraft.Download/versions/1.6.4/minecraft_server.1.6.4.jar chdir=/root/minecraft creates=/root/minecraft/minecraft_server.1.6.4.jar
become_user: root
become_method: sudo
- name: run minecraft
command: screen -S minecraft -d -m java -Xmx512M -Xms512M -jar /root/minecraft/minecraft_server.1.6.4.jar
become_user: root
become_method: sudo
@decentral1se
Copy link

Here is how I've built my host group before to connect to the new Linode:

- name: Dynamically create the Linode connection details.
  add_host:
    hostname: freshly-created-linode
    ansible_user: root
    ansible_host: "{{ linode_creation_details.data.instance.ipv4[0] }}"
    ansible_ssh_pass: "{{ ssh_root_pass }}"
    ansible_ssh_private_key_file: "{{ generated_ssh_key.ssh_key_file }}"
    ansible_ssh_extra_args: '-o StrictHostKeyChecking=no'

Or so, you might try to make sure you pass all of these (if not already) to make the connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment