5.0 KiB
Here's the refactored version of your technical guide based on the suggestions provided:
Step-by-Step Guide to Setting Up GitHub SSH Keys on Ansible Control Node
Step 1: Generate SSH Keys for GitHub Access
-
Generate SSH key pair for GitHub access:
ssh-keygen -t rsa -b 4096- When prompted, save the key in a specific file (e.g.,
/home/prometheus/.ssh/github_rsa). - Enter a passphrase for added security.
- When prompted, save the key in a specific file (e.g.,
-
Add the SSH key to the SSH agent:
eval "$(ssh-agent -s)" ssh-add /home/prometheus/.ssh/github_rsa -
Copy the public key:
cat /home/prometheus/.ssh/github_rsa.pub
Step 2: Add SSH Key to GitHub
-
Go to GitHub:
- Navigate to your GitHub account settings.
- Go to "SSH and GPG keys" > "New SSH key".
-
Add your SSH public key:
- Title:
ansible-control-node - Key: Paste the contents of
/home/prometheus/.ssh/github_rsa.pub
- Title:
Step 3: Configure SSH to Use the Key for GitHub
-
Edit SSH configuration:
nano /home/prometheus/.ssh/config -
Add the following configuration:
Host github.com HostName github.com IdentityFile /home/prometheus/.ssh/github_rsa IdentitiesOnly yes -
Set the correct permissions:
chmod 600 /home/prometheus/.ssh/config
Step 4: Clone Your GitHub Repository
-
Navigate to the home directory:
cd ~ -
Clone the repository using the SSH URL:
git clone git@github.com:crazystorm720/ansible.git ~/ansible
Step 5: Configure Ansible Inventory and Configuration
-
Navigate to the repository directory:
cd ~/ansible -
Create the necessary directories and files:
mkdir -p group_vars host_vars roles touch hosts.ini ansible.cfg setup_playbook.yml -
Configure the inventory file:
# hosts.ini [managed_hosts] target_host1 ansible_host=192.168.1.1 ansible_user=prometheus target_host2 ansible_host=192.168.1.2 ansible_user=prometheus -
Create Ansible configuration file:
# ansible.cfg [defaults] inventory = hosts.ini remote_user = prometheus host_key_checking = False private_key_file = /home/prometheus/.ssh/id_rsa [privilege_escalation] become = True become_method = sudo become_user = root
Step 6: Create a Basic Playbook
- Create the playbook file:
# setup_playbook.yml --- - name: Setup Ansible environment hosts: managed_hosts vars: ssh_public_key_path: "/home/prometheus/.ssh/id_rsa.pub" ansible_user: prometheus tasks: - name: Ensure SSH directory exists for the user file: path: "/home/{{ ansible_user }}/.ssh" state: directory owner: "{{ ansible_user }}" group: "{{ ansible_user }}" mode: "0700" - name: Copy SSH public key to authorized_keys file authorized_key: user: "{{ ansible_user }}" key: "{{ lookup('file', ssh_public_key_path) }}" state: present - name: Ping the target hosts ping:
Step 7: Commit and Push Your Changes to GitHub
-
Add all files to the staging area:
git add . -
Commit your changes:
git commit -m "Initial commit: Set up Ansible environment and SSH key management for prometheus user" -
Push your changes to GitHub:
git push origin main
Step 8: Test the Setup
- Test the connection to the managed hosts:
Verify that you can connect to the managed hosts and that the SSH key authentication works as expected.
ansible all -m ping
Step 9: Run the Playbook
-
Navigate to your Ansible directory (if not already there):
cd ~/ansible -
Run the playbook:
ansible-playbook setup_playbook.yml
Summary
- Generate SSH Keys: Create an SSH key pair specifically for GitHub access.
- Add SSH Key to GitHub: Add the public key to your GitHub account.
- Configure SSH: Set up the SSH configuration to use the new key for GitHub.
- Clone Repository: Clone your GitHub repository to your control node.
- Configure Ansible: Set up inventory, configuration files, and playbook.
- Commit and Push to GitHub: Add, commit, and push your changes to the repository.
- Test the Setup: Verify that you can connect to the managed hosts using Ansible and that the SSH key authentication works as expected.
- Run the Playbook: Execute the playbook to configure the managed hosts.
By following these streamlined steps, you'll have a properly set up Ansible control node with secure SSH key management for GitHub, and all required components for SSH key management, playbooks, and version control via GitHub. If you have any further questions or need additional assistance, feel free to ask!