ec2-ssh-key-authorized

Description

Add ssh key for use with AWS EC2.

Variables

Name Type Default Description

aws_access_key

string --

The AWS access key. Required

aws_secret_key

string --

The AWS secret key. Required

key_content

string --

The content of the public ssh key. Required

name

string --

The alias of the ssh key on AWS. Required

password

string --

The password to unlock the key (only used if key doesn't exist already).

path

string ~/.ssh/id_ed25519

The path to the private ssh key.

The path to the public key will be infered (added '.pub').

user

string --

The name of the ssh key owner.

Code

doc:
  short_help: Add ssh key for use with AWS EC2.
args:
  _import: ssh-key-exists
  name:
    doc:
      short_help: The alias of the ssh key on AWS.
    type: string
    required: true
  key_content:
    doc:
      short_help: The content of the public ssh key.
    type: string
    required: true
    excludes: path
  path:
    doc:
      short_help: The path to the private ssh key.
      help: |
        The path to the private ssh key.

        The path to the public key will be infered (added '.pub').
    type: string
    required: true
    excludes: key_content
  aws_access_key:
    doc:
      short_help: The AWS access key.
    type: string
    required: true
  aws_secret_key:
    doc:
      short_help: The AWS secret key.
    type: string
    secret: true
    required: true

frecklets:
- ssh-key-exists:
    frecklet::skip: '{{:: path | true_if_empty ::}}'
    path: '{{:: path ::}}'
    password: '{{:: password ::}}'
    user: '{{:: user ::}}'
- frecklet:
    name: ec2-ssh-key-authorized.at.yml
    type: ansible-tasklist
    properties:
      elevated: '{{:: user | false_if_empty ::}}'
      internet: true
      idempotent: true
    resources:
      ansible-tasklist:
      - internally-register-public-ssh-key.at.yml
      python-package:
      - boto
      - boto3
      - botocore
    desc:
      short: "add ssh key pair (name: '{{:: name ::}}') to aws account"
      long: |
        {%:: if path -::%}
        Read the public key file '{{:: path ::}}.pub' and use the the content to create or overwrite a key pair for EC2 on AWS, using the following details:{%:: else -::%}
        Use the 'key_content' string ( {{:: key_content ::}} ) to create or overwrite a key pair for EC2 on AWS, using the following details:{%:: endif ::%}

            aws access key: {{:: aws_access_key ::}}
            aws_secret_key: <your secret key>
            key pair name: {{:: name ::}}

      references:
        AWS ec2 key pair documentation: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html?icmpid=docs_ec2_console
        Ansible 'ec2_key' module documentation: https://docs.ansible.com/ansible/latest/modules/ec2_key_module.html
  vars:
    __aws_access_key__: '{{:: aws_access_key ::}}'
    __aws_secret_key__: '{{:: aws_secret_key ::}}'
    __name__: '{{:: name ::}}'
    __key_content__: '{{:: key_content ::}}'
    __path_to_key__: '{{:: path ::}}'
    __use_become__: '{{:: user | false_if_empty ::}}'
frecklecute --community ec2-ssh-key-authorized --help

Usage: frecklecute ec2-ssh-key-authorized [OPTIONS]

  Add ssh key for use with AWS EC2.

Options:
  --aws-access-key AWS_ACCESS_KEY
                                  The AWS access key.  [required]
  --aws-secret-key AWS_SECRET_KEY
                                  The AWS secret key.  [required]
  --name NAME                     The alias of the ssh key on AWS.  [required]
  --key-content KEY_CONTENT       The content of the public ssh key.
  --password PASSWORD             The password to unlock the key (only used if
                                  key doesn't exist already).
  --path PATH                     The path to the private ssh key.
  --user USER                     The name of the ssh key owner.
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.ec2_ssh_key_authorized.Ec2SshKeyAuthorized
#


from dataclasses import dataclass
from pyckles import AutoPycklet
from typing import *    # noqa

@dataclass
class Ec2SshKeyAuthorized(AutoPycklet):
    """Add ssh key for use with AWS EC2.

       Args:
         aws_access_key: The AWS access key.
         aws_secret_key: The AWS secret key.
         key_content: The content of the public ssh key.
         name: The alias of the ssh key on AWS.
         password: The password to unlock the key (only used if key doesn't exist already).
         path: The path to the private ssh key.
         user: The name of the ssh key owner.

    """

    FRECKLET_ID = "ec2-ssh-key-authorized"

    aws_access_key: str = None
    aws_secret_key: str = None
    key_content: str = None
    name: str = None
    password: str = None
    path: str = None
    user: str = None


    def __post_init__(self):
        super(Ec2SshKeyAuthorized, self).__init__(var_names=["aws_access_key", "aws_secret_key", "key_content", "name", "password", "path", "user"])


frecklet_class = Ec2SshKeyAuthorized
# -*- coding: utf-8 -*-


#
# module path: pycklets.ec2_ssh_key_authorized.Ec2SshKeyAuthorized
#


from pyckles import AutoPycklet

class Ec2SshKeyAuthorized(AutoPycklet):
    """Add ssh key for use with AWS EC2.

       Args:
         aws_access_key: The AWS access key.
         aws_secret_key: The AWS secret key.
         key_content: The content of the public ssh key.
         name: The alias of the ssh key on AWS.
         password: The password to unlock the key (only used if key doesn't exist already).
         path: The path to the private ssh key.
         user: The name of the ssh key owner.

    """

    FRECKLET_ID = "ec2-ssh-key-authorized"

    def __init__(self, aws_access_key=None, aws_secret_key=None, key_content=None, name=None, password=None, path="~/.ssh/id_ed25519", user=None):

        super(Ec2SshKeyAuthorized, self).__init__(var_names=["aws_access_key", "aws_secret_key", "key_content", "name", "password", "path", "user"])
        self._aws_access_key = aws_access_key
        self._aws_secret_key = aws_secret_key
        self._key_content = key_content
        self._name = name
        self._password = password
        self._path = path
        self._user = user

    @property
    def aws_access_key(self):
        return self._aws_access_key

    @aws_access_key.setter
    def aws_access_key(self, aws_access_key):
        self._aws_access_key = aws_access_key

    @property
    def aws_secret_key(self):
        return self._aws_secret_key

    @aws_secret_key.setter
    def aws_secret_key(self, aws_secret_key):
        self._aws_secret_key = aws_secret_key

    @property
    def key_content(self):
        return self._key_content

    @key_content.setter
    def key_content(self, key_content):
        self._key_content = key_content

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, name):
        self._name = name

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, password):
        self._password = password

    @property
    def path(self):
        return self._path

    @path.setter
    def path(self, path):
        self._path = path

    @property
    def user(self):
        return self._user

    @user.setter
    def user(self, user):
        self._user = user



frecklet_class = Ec2SshKeyAuthorized