diff --git a/cloud.yml b/cloud.yml new file mode 100644 index 0000000000000000000000000000000000000000..7294e5e884aa8c3fad9204af3fa6c28340687261 --- /dev/null +++ b/cloud.yml @@ -0,0 +1,12 @@ +## +# Dummy Ansible playbook + +--- +# file: cloud.yml + +- name: "No cloud action required" + hosts: "localhost" + connection: local + gather_facts: false + sudo: no + tasks: [] diff --git a/ec2.yml b/ec2.yml new file mode 100644 index 0000000000000000000000000000000000000000..8d74c1c32675fa7ffbff23776070a5d19372e660 --- /dev/null +++ b/ec2.yml @@ -0,0 +1,46 @@ +## +# Ansible playbook for managing an ec2 inventory + +--- +# file: ec2.yml + +- name: "EC2: Launch a new host" + hosts: "localhost" + connection: local + gather_facts: false + sudo: no + tasks: + - name: "Create the new instance" + ec2: + instance_tags: '{"Name":"Ansible-Host-{{ host }}","AnsibleHost":"{{ host }}","AnsibleGroups":"{{ initgroups }}"}' + assign_public_ip: yes + group_id: "{{ ec2_group_id }}" + key_name: "{{ ec2_key_name }}" + image: "{{ ec2_ami_id }}" + instance_type: "{{ ec2_instance_type }}" + vpc_subnet_id: "{{ ec2_subnet_id }}" + region: "{{ ec2_region }}" + state: present + wait: yes + register: ec2 + - name: "Waiting for the new instance(s) to get up and running" + ec2: + instance_ids: "{{ ec2.instance_ids }}" + instance_type: "{{ ec2_instance_type }}" + region: "{{ ec2_region }}" + state: running + wait: yes + - name: "Add new instance(s) to the inventory" + add_host: + hostname="{{ host }}" + static_ipv4="{{ item.public_ip }}" + groups="{{ initgroups }}" + with_items: ec2.instances + - name: "Waiting for SSH service becoming available" + wait_for: + host="{{ item.public_ip }}" + port=22 + delay=10 + timeout=120 + state=present + with_items: ec2.instances diff --git a/inithost.sh b/inithost.sh index b6104e74673ab39c8e72947953df69687ea76d51..1675c2bd3038c0d5d2bd7a723003bc066ea7e6dd 100755 --- a/inithost.sh +++ b/inithost.sh @@ -5,14 +5,16 @@ cd $( cd $(dirname $(realpath $0)) ; pwd ) if [ "$2" == "" ] then echo "Usage" - echo "inithost.sh HOST IP [USER [KEYFILE]]" + echo "inithost.sh HOST IP [USER [KEYFILE [GROUPS]]]" exit 101; fi +CLOUD=cloud HOST=$1 -IP=$2 +IP=inithostip=$2 ROOT=root KEYFILE=--ask-pass +INITGROUPS=inventory shift shift if [ "$1" != "" ] @@ -25,6 +27,16 @@ if [ "$1" != "" ] KEYFILE=--private-key=$1 shift fi +if [ "$1" != "" ] + then + INITGROUPS=$1 + shift +fi + +if [ "$IP" == "inithostip=ec2" ] + then + CLOUD=ec2 + IP= +fi -./ansible-playbook.sh inithost --extra-vars="inituser=$ROOT host=$HOST inithostip=$IP firstuser=$USER" --tags="prepare" $KEYFILE "$@" -./ansible-playbook.sh inithost --extra-vars="host=$HOST inithostip=$IP distribute_keys=true" --tags="config" "$@" +./ansible-playbook.sh inithost --extra-vars="cloud=$CLOUD host=$HOST inituser=$ROOT firstuser=$USER initgroups=$INITGROUPS $IP distribute_keys=true" $KEYFILE "$@" diff --git a/inithost.yml b/inithost.yml index db133ac4e55dda8160c42b4f57cdb8d595ec8ace..e8e865a7e9f67689055e6f437788e5deb88d671e 100644 --- a/inithost.yml +++ b/inithost.yml @@ -4,21 +4,22 @@ --- # file: inithost.yml +# Check if we need to launch an instance there first +- include: "{{ cloud }}.yml" + - name: "Prepare 1" hosts: "{{ host }}" connection: local gather_facts: false sudo: no - tags: - - prepare tasks: - name: "InitHost | Remove previous entries from known_hosts" shell: "ssh-keygen -R {{ item }}" with_items: - - '{{ inithostip }}' + - '{{ inithostip|default(static_ipv4) }}' - '{{ host }}' - name: "InitHost | Copy root id to new host" - shell: "ssh-copy-id -i root@{{ inithostip }} -o PasswordAuthentication=yes -o PubkeyAuthentication=no -o IdentitiesOnly=yes" + shell: "ssh-copy-id -i root@{{ inithostip|default(static_ipv4) }} -o PasswordAuthentication=yes -o PubkeyAuthentication=no -o IdentitiesOnly=yes" register: inithost_ssh_copy_id changed_when: "inithost_ssh_copy_id.rc != 0" failed_when: false @@ -28,22 +29,18 @@ connection: local gather_facts: false sudo: yes - tags: - - prepare tasks: - name: "InitHost | Include new host into /etc/hosts" lineinfile: dest=/etc/hosts - regexp="^{{ inithostip }} " - line="{{ inithostip }} {{ host }}" + regexp="^{{ inithostip|default(static_ipv4) }} " + line="{{ inithostip|default(static_ipv4) }} {{ host }}" - name: "Setup first user" hosts: "{{ host }}" gather_facts: false user: "{{ inituser }}" sudo: yes - tags: - - prepare vars: first_user: - name: "{{ firstuser }}" @@ -65,25 +62,17 @@ user={{ item.name }} key="{{ lookup('file', inventory_dir + '/files/keys/' + item.name + '.d2s.pub') }}" with_items: first_user - # TODO: Move the following to common role and make it conditional - #- name: "InitHost | Remove console-kit-daemon" - # shell: mv console-kit-daemon console-kit-daemon.off chdir=/usr/sbin - # ignore_errors: true - name: "Security setup" hosts: "{{ host }}" gather_facts: true sudo: yes - tags: - - config roles: - common - name: "Upload keys" hosts: "all" sudo: yes - tags: - - config roles: - { role: common, when: ignore_these_tasks is defined } - { role: commonauth }