<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>ngetchell.com - Posts tagged with &quot;GitHub&quot;</title><description>All blog posts tagged with &quot;GitHub&quot;</description><link>https://ngetchell.com/</link><item><title>Using Ansible via GitHub Actions and Tailscale</title><link>https://ngetchell.com/blog/using-ansible-via-github-actions-and-tailscale/</link><guid isPermaLink="true">https://ngetchell.com/blog/using-ansible-via-github-actions-and-tailscale/</guid><description>Use Tailscale and GitHub Actions run your ansible playbooks against nodes on your tailnet.</description><pubDate>Mon, 13 Nov 2023 16:10:28 GMT</pubDate><content:encoded>
&lt;p&gt;I build this blog using Hugo and updated it nightly (or on push to main) via GitHub Actions.
I run an nginx web server, sitting on a DigitalOcean VPS to serve the traffic.
It is a very simple setup for someone who is used to running a CMS (have been doing it since 2005).
Almost too simple.&lt;/p&gt;



&lt;p&gt;With this in mind, I set up to automate the creation and deletion of my VPS using Ansible.
The playbook is specific to my situation.
I set up nginx, tailscale, redirections, a staging env only accessable from the tailscale network, certbot, etc.
Every blog would be different so I won&apos;t be sharing my playbook but I will go over how I set up Ansible to connect to my VPS over the Tailscale network.&lt;/p&gt;



&lt;p&gt;For those that don&apos;t know, Tailscale is a mesh VPN that allows direct wireguard connections between peers.
Tailscale runs a coordination server for you to establish the connection, then it is peer-to-peer.
At work, we put it through it&apos;s paces and found it easy to deploy, the user deactivation process was near instantanious, and the price was right.&lt;/p&gt;



&lt;p&gt;On my own, I use it to access my home resources while on the road.
My mobile devices and laptops automatically connect so I am able to access my home media, rss reader, and more.&lt;/p&gt;



&lt;h2&gt;Why?&lt;/h2&gt;



&lt;p&gt;You may say to yourself, why make it so complicated?
Security and fun is the answer.
Being able to put the VPS behind a firewall that only allows port 80 and 443 incoming is nice.
Since tailscale initiates the connection from the client, it isn&apos;t impeeded by the firewall.
It is fun because I love building rube goldberg machines so lay off!&lt;/p&gt;



&lt;h2&gt;Setting up Ansible&lt;/h2&gt;



&lt;p&gt;First order of business is to make sure you have an inventory file that contains your assets and their corresponding tailnet addresses.
You&apos;ll need it inside of the GitHub action.&lt;/p&gt;



&lt;p&gt;Use the ansible_host inventory variable to handle it if you aren&apos;t using host_vars.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;hugo ansible_host&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt;102.1&lt;/span&gt;&lt;span&gt;.264.22&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Run your playbook with the new inventory file to make sure you are ready.
Any troubleshooting that can happen &lt;strong&gt;before&lt;/strong&gt; you roll out CI/CD will save you time later.
The iteration times are slow, no matter the CI/CD environment.
Plus, to iterate your CI, you need to commit code to generate the jobs so there is proof of your incompetence!
We must keep our incompetence to ourselves.
I love hiding my sins by squashing my PRs.&lt;/p&gt;



&lt;h2&gt;Configuring the Action&lt;/h2&gt;



&lt;p&gt;I love the layout of a GitHub Action yml file.
Simple, well-documented, and comes with batteries included.
The latest Ubuntu image works best for me and &lt;a href=&quot;https://github.com/actions/runner-images/blob/main/images/linux/Ubuntu2204-Readme.md&quot;&gt;look at everything it comes with&lt;/a&gt;.
That, coupled with a great VS Code extension and I was off to the races.
Looks at this tool tip.
It is doing the cron time math for me (in UTC).&lt;/p&gt;



&lt;figure&gt;&lt;img src=&quot;https://ngetchell.com/wp-media/2026/07/ansible-tooltip.png&quot; alt=&quot;GitHub Actions vs code extension displaying a helpful tooltip&quot; /&gt;&lt;/figure&gt;



&lt;p&gt;Make sure to configure your secrets in the repository so you can reference them in the action.&lt;/p&gt;



&lt;figure&gt;&lt;img src=&quot;https://ngetchell.com/wp-media/2026/07/ansible-secrets.png&quot; alt=&quot;GitHub action secrets configuration page&quot; /&gt;&lt;/figure&gt;



&lt;p&gt;GitHub has a great action dispatch system where you can generate actions for just about any event logged to your repository.
Here, set up the action to run when there is a commit to the main branch (when I merge a PR), when I kick it off manually via the workflow dispatch feature, and every day at 13:01 UTC.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;on&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  push&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    branches&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;      - &lt;/span&gt;&lt;span&gt;main&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  workflow_dispatch&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  schedule&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    - &lt;/span&gt;&lt;span&gt;cron&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &apos;01 13 * * *&apos;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;We have secrets to reference, so we set them as env vars near the top of the yml file.
To generate your ssh key, run &lt;code&gt;ssh-keygen -t ed25519&lt;/code&gt; to get a private key to add to GitHub.
You can add the public key to the knownhost file on the server using &lt;code&gt;ssh-copy-id&lt;/code&gt;.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;env&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  SSH_KEY_GITHUB_ACTIONS&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ${{ secrets.SSH_KEY_GITHUB_ACTIONS }}&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  VPS_DEPLOY_USER&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ${{ secrets.VPS_DEPLOY_USER }}&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  VPS_DEPLOY_HOST&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ${{ secrets.VPS_DEPLOY_HOST }}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Define your job next.
You give it a name, tell it what to run on, and give it a bunch of steps to complete.
First, we checkout the code.
Since I&apos;m using a git submodule, I need to pass that config as parameter.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;jobs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  playbook&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    runs-on&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ubuntu-latest&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    steps&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;      - &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; Checkout&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        uses&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; actions/checkout@v3&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        with&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          submodules&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; recursive&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          fetch-depth&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; 0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Then use the Tailscale provided reusable action to authenticate to your tailnet.
I tag my job so when I can apply ACLs on the ephemeral instance.
This instance will disappear shortly after the runner dies.&lt;/p&gt;



&lt;figure&gt;&lt;img src=&quot;https://ngetchell.com/wp-media/2026/07/ansible-tailscale-ephemeral-device.png&quot; alt=&quot;A screencap of the tailscale admin device screen showing the ephemeral device.&quot; /&gt;&lt;/figure&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;      - &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; Connect to Tailnet&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        uses&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; tailscale/github-action@v2&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        with&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          oauth-client-id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ${{ secrets.TS_OAUTH_CLIENT_ID }}&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          oauth-secret&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ${{ secrets.TS_OAUTH_SECRET }}&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          tags&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; tag:github&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Now comes the fun part.
We drop into a bash terminal and set a few configs, then invoke ansible-playbook.
The first few lines are to set up your ssh private key since I disable password based authentication.
I&apos;m then able to call ansible-playbook with the env vars I set earlier in the file.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;      - &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; Invoke Ansible&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        run&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          mkdir -p ~/.ssh/&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          chmod 0700 ~/.ssh&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          eval $(ssh-agent -s)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          ssh-add &amp;lt;(echo &quot;$SSH_KEY_GITHUB_ACTIONS&quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          pip install -r requirements.txt&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          ansible-playbook ansible/playbook.yml \&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            -i ansible/tailscale_inventory \&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            --ssh-common-args=&apos;-o StrictHostKeyChecking=no&apos; \&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            --extra-vars ansible_user=$VPS_DEPLOY_USER \&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            --extra-vars ansible_host=$VPS_DEPLOY_HOST&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;If I was truly paranoid, I&apos;d add the ssh host key as an environment variable so I wouldn&apos;t need to disable strict host checking.&lt;/p&gt;



&lt;h2&gt;Entire Action&lt;/h2&gt;



&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; Configure Server&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;on&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  push&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    branches&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;      - &lt;/span&gt;&lt;span&gt;main&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  workflow_dispatch&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  schedule&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    - &lt;/span&gt;&lt;span&gt;cron&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; &apos;01 13 * * *&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;env&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  SSH_KEY_GITHUB_ACTIONS&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ${{ secrets.SSH_KEY_GITHUB_ACTIONS }}&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  VPS_DEPLOY_USER&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ${{ secrets.VPS_DEPLOY_USER }}&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  VPS_DEPLOY_HOST&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ${{ secrets.VPS_DEPLOY_HOST }}&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;jobs&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;  playbook&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    runs-on&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ubuntu-latest&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    steps&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;      - &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; Checkout&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        uses&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; actions/checkout@v3&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        with&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          submodules&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; recursive&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          fetch-depth&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;      - &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; Connect to Tailnet&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        uses&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; tailscale/github-action@v2&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        with&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          oauth-client-id&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ${{ secrets.TS_OAUTH_CLIENT_ID }}&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          oauth-secret&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; ${{ secrets.TS_OAUTH_SECRET }}&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          tags&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; tag:github&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;      &lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;      - &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; Invoke Ansible&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        run&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          mkdir -p ~/.ssh/&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          chmod 0700 ~/.ssh&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          eval $(ssh-agent -s)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          ssh-add &amp;lt;(echo &quot;$SSH_KEY_GITHUB_ACTIONS&quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          pip install -r requirements.txt&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          ANSIBLE_HOST_KEY_CHECKING=False&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;          ansible-playbook ansible/playbook.yml \&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            -i ansible/tailscale_inventory \&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            --ssh-common-args=&apos;-o StrictHostKeyChecking=no&apos; \&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            --extra-vars ansible_user=$VPS_DEPLOY_USER \&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            --extra-vars ansible_host=$VPS_DEPLOY_HOST&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
</content:encoded><category>Ansible</category><category>GitHub</category><category>GitHub Actions</category></item></channel></rss>