Coder Social home page Coder Social logo

ssh-agent-ansible's Introduction

Pipeline for Testing ansible infrastructure with SSH-AGENT Automation Pipeline for Testing Action for ansible infrastructure with SSH-AGENT Automation

ssh-agent-ansible (Python Code)

This tool has been built for automating ssh-add for key with/without passphrase.

Runs ssh-agent, generates a file with passphrase for ssh_ask_pass and adds key and passphrase with ssh-add.

Why was this built?

When running a GitHub Action workflow for running ansible playbooks or roles for you Infrastructure as Code (IaC) projects, you will need to use SSH secrets (SSH Pvt Key, and passphrase (if applicable)) in a non-interactive way with the shell, this can become a bit cumbersome using toools like ssh-agent, ssh-add with direct shell commands.

Any Private Key with a passphrase requires the passphrase to be added interactively for every host specified in the ansible inventory So, in order to make this process non-interactive at the shell level and easy to use IaC CI pipelines through Github Actions, This tool/action runs ssh-agent, generates a file with specified passphrase for ssh_ask_pass and then adds the key and passphrase with ssh-add to the ssh-agent non-interactively.

Attributes (py):

- SSH_AUTH_SOCK (str): Path to the ssh AUTH socket file.
- ANSIBLE_SSH_KEY (str): Github Actions Secret saved SSH private Key (PEM).
- SSH_KEY_PASSPHRASE (str): Github Actions Secret saved SSH private key passphrase.

Functions:

- ssh_agent: Runs SSH agent and Adds Key and passphrase to SSH-AGENT.

Todo:

- Add functionality to differentiate between keys with and without passphrase.
- Upload and Publish as a Github Action on the Marketplace.     

Note:

- This is only works with private keys in the PEM format. If used with OpenSSH format keys it will error out.
- The action unsets SSH_AUTH_SOCK, kills the ssh-agent process and removes the SSH_AUTH_SOCK file before every run to remove any lingering or already running instances.
- Tested with python3.11, Ensure to use with atleast python3.6+ 

Action: Related Details

How to Use the Action in a Workflow

      - name: 'Remove old SSH_AUTH_SOCKET and SSH-AGENT Instance'
        run: |
            rm /tmp/ssh_agent.sock
            unset SSH_AUTH_SOCK
            pkill ssh-agent
        continue-on-error: true
        shell: bash


      - name: Execute Key & passphrase Storage with SSH-AGENT
        uses: marwaha-raghav/[email protected]
        with:
          SSH_AUTH_SOCK: "/tmp/ssh_agent.sock"
          SSH_KEY_PASSPHRASE: ${{secrets.SSH_KEY_PASSPHRASE}}
          ANSIBLE_SSH_KEY: ${{secrets.ANSIBLE_SSH_KEY}}


Inputs

  • SSH_AUTH_SOCK (str): Path to the ssh AUTH socket file.
  • ANSIBLE_SSH_KEY (str): Github Actions Secret saved SSH private Key (PEM).
  • SSH_KEY_PASSPHRASE (str): Github Actions Secret saved SSH private key passphrase.

Limitations

Works for the Current Job Only

Each job on Github Actions runs in a fresh instance of the VM, the SSH key will only be available in the job where this action has been referenced. THus this action will have to be called each time for a new Job.

SSH Private Key Format

If the private key is not in the PEM format, you will see an Error loading key "(stdin)": invalid format message.

Use ssh-keygen -p -f path/to/your/key -m pem to convert your key file to PEM, (Create a backup of your key, since this changes the original)

**Note: Examples for workflows are in the test dir.

ssh-agent-ansible's People

Contributors

marwaha-raghav avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

deekayen

ssh-agent-ansible's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.