How do I deploy Linodes using Ansible?
Is it possible to deploy Linode Instances with Ansible in addition to managing the configuration and software in the OS of those instances?
3 Replies
Linode support has been in Ansible for a few years. The linode
module offers many of the Instance management features of the Legacy Linode API (v3).
In the most recent release of Ansible 2.8, Linode support has been updated for use with the latest Linode API (v4). Most of this work was contributed by by Luke Murphy who started the process and saw it through to the Ansible release. He's also continuing this "Linode all the things" effort on the Molecule project.
To contribute more to this project, join the Ansible Linode Working Group.
See the docs for the Linode Inventory and linode_v4
module:
- https://docs.ansible.com/ansible/latest/plugins/inventory/linode.html
- https://docs.ansible.com/ansible/latest/modules/linode_v4_module.html
Here's an example yaml file and the instructions to execute it. This example will create a Linode Nanode and will install Minecraft server on it:
- Create a
mc.yml
file using the snippet below pip install --upgrade ansible
- Get a Linode Personal Access Token with permission to read and write Linodes.
export LINODE_TOKEN=... # Replace ... with the token
ansible-playbook mc.yml -e linode_token=$LINODE_TOKEN -e type=g6-nanode-1 -e region=us-east -e image=linode/debian9 -e ssh_key=~/.ssh/id_rsa.pub
---
- 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.instance.id }} IP is {{ my_linode.instance.ipv4 }}"
- name: Add new linode to host group
local_action:
module: add_host
hostname={{ my_linode.instance.ipv4[0] }}
group=launched
ansible_user=root
- name: Wait for SSH to come up
local_action:
module: wait_for
host={{ my_linode.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
apt:
update_cache: yes
autoclean: yes
autoremove: yes
upgrade: dist
- name: install Java and Screen
apt:
update_cache: yes
autoclean: yes
autoremove: yes
name: "{{ packages }}"
vars:
packages:
- default-jdk
- screen
- 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
It would be great to see an example where I can spin up two or three hosts and a node balancer as well.
I'm trying to do something like this:
Playbook for creating linodes and populating them.
name: Create group of linodes
hosts: localhost
vars_files:./vars/vars.yml
./vars/external.yml
tasks:
include_tasks: create.yml
with_items:
- web-p01
- web-p02
loop_control:
loop_var: linode
And my tasks look very much like your mc.yml above, which has been a big help. Now to get it working in a loop properly
But it creates the first linode and them bombs when trying to use the 'package' module to apply patches, since I'm using CentOS8, not Debian as my install image.
I like Ansible… but I also find it annoying. LOL.
Ok, so I think I've figured it out, and my mistakes all come down to indentation. The section where you have "configure linode for minecraft" needed to be all the way left in my main task file when I called it. It's now working great.
I'll post a simple exmaple at some point I hope.