action-ansible-playbook icon indicating copy to clipboard operation
action-ansible-playbook copied to clipboard

SSH Issues – a bit more documentation would be nice

Open vardumper opened this issue 3 years ago • 9 comments

In order to make sure ansible has access to the boxes, I needed to remove the following line form my inventory file. Also I had to comment out the known_hosts setting.

I had the line in all:vars section

[all:vars]
ansible_ssh_private_key_file = ~/.ssh/id_rsa

After removing it, the playbook action could connect.

vardumper avatar Oct 31 '21 07:10 vardumper

@vardumper agree it would be nice to have some documentation advising against setting the ansible_ssh_private_key_file value in the README. I'm not the best at node.js but it looks like the key is copied to a file called ".ansible_key" so you might be able to set ansible_ssh_private_key_file to that but easier to just omit it.

if (key) {
    const keyFile = ".ansible_key"
    fs.writeFileSync(keyFile, key + os.EOL, { mode: 0600 })
    core.saveState("keyFile", keyFile)
    cmd.push("--key-file")
    cmd.push(keyFile)
}

clincha avatar Oct 14 '22 22:10 clincha

EDIT: I solved it

I can't connect :/ I tried both suggestions and I got a permission denied error. I stored the private key string in an action secret and added it to the "key" variable. Is there anything I'm missing?

name: Deploy
on:
  push:
    branches: [ main ]
  workflow_dispatch:
 
jobs:
  deploy_code:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        
      - name: Run Ansible playbook
        uses: dawidd6/[email protected]
        with:
          playbook: ansible/deploy.yml
          key: ${{secrets.ANSIBLE_PRIVATE_KEY}} 
          inventory: |
            [exampleserver]
            example ansible_host=example.com ip=55.555.55.55 ansible_port=5555 ansible_ssh_user=example

MasteringBOX avatar Feb 25 '23 00:02 MasteringBOX

EDIT: I solved it

@MasteringBOX could you please elaborate more how you have fixed this? I'm facing the similar issue, looks like action is ignoring the value of key parameter.

jozef-valko avatar Mar 14 '23 11:03 jozef-valko

Hi @jozef-valko,

I don't fully remember, it was something silly. Try adding your key to a secret called exactly SECRET_SSH_KEY. I was using key: ${{secrets.ANSIBLE_PRIVATE_KEY}} and while I was desperately trying things I added the key to a new secret called SECRET_SSH_KEY and it worked. So try creating that action secret with your secret key contents and changing the key value to: key: ${{secrets.SECRET_SSH_KEY}} But it may have been something else. Let me know if that works!

By the way, after configuring this I ended up using Github actions directly, they are pretty straightforward and they will deploy faster.

MasteringBOX avatar Mar 14 '23 16:03 MasteringBOX

@MasteringBOX I made it work, it was issue on my side with SSH keys and config. After generating new SSH keypair and setting parameters correctly:

- name: deploy stuff
   uses: dawidd6/action-ansible-playbook@v2
   with:
   playbook: deploy.yaml
   key: ${{ secrets.SSH_KEY }}
   options: |
     --user ssh_user
     --ssh-extra-args "-o StrictHostKeyChecking=no"

and setting --ssh-extra-args "-o StrictHostKeyChecking=no" GH action was able to connect to machines successfully.

jozef-valko avatar Mar 14 '23 16:03 jozef-valko

Don't forget to set the ssh user

port19x avatar Jul 31 '23 14:07 port19x

I will gladly review PRs improving the documentation.

dawidd6 avatar Jan 05 '24 17:01 dawidd6

Screenshot from 2024-01-21 11-46-00 Always getting this error This is my workflow Screenshot from 2024-01-21 12-48-40

arnabnath180 avatar Jan 21 '24 07:01 arnabnath180

Solved this issue by creating a self hosted runner

arnabnath180 avatar Jan 22 '24 18:01 arnabnath180