Ansible 任务 - clone private git without SSH forwarding
Ansible task - clone private git without SSH forwarding
我正在尝试创建一个 Ansible 剧本,它将 运行 来自我们开发团队的计算机和 CI/CD 服务器。
剧本中的任务之一是从私有 git 存储库中获取我们项目的源代码。因为 playbook 必须从 CI/CD 服务器 运行 我们不能使用 SSH 转发。
我想到的是将必要的 SSH 私钥复制到远程主机,然后使用密钥从私有 git 存储库中克隆代码。
然而,尝试此操作时,克隆任务挂起。当尝试手动启动命令时,它会要求输入 SSH 私钥的密码。 SSH 密钥不使用密码(空白)。
谁能分享他们对这个(可能很常见)问题的解决方案?
如果有人需要,这是我目前的剧本:
- name: Create SSH directory
file: path=/root/.ssh state=directory
- name: Copy SHH key for Git access
copy:
content: "{{ git_ssh_key }}"
dest: /root/.ssh/id_rsa
owner: root
group: root
mode: 0600
# Also tried this, but it also hangs
#- name: Start SSH agent and add SSH key
# shell: eval `ssh-agent -s` && ssh-add
- name: Get new source from GIT
git:
key_file: /root/.ssh/id_rsa
repo: "git@gitlab.com:user/repo.git"
dest: "{{ staging_dir }}"
depth: 1
accept_hostkey: yes
clone: yes
我正在使用 ansible 2.3.1.0, python version = 2.7.12
我想问题是对于自动化任务,git 没有使用 ssh 密钥。我读过(通过浏览几分钟)git 2.10,你可以在调用 git 时提供 ssh 东西来传递......所以我想你可以尝试这样的事情:
git -c core.sshCommand='ssh -i /path/to/private/key' fetch origin
或者您想要使用的任何其他命令。您也可以在配置中修复它:
git config core.sshCommand 'ssh -i /path/to/private/key'
总之,希望对你有所帮助
Specify private SSH-key to use when executing shell command with or without Ruby?
以下是使其工作的步骤(在 MacOS Ubuntu LTS 上使用 Ansible 2.3.1 和 Python 2.7.10 进行测试):
生成没有密码的新 SSH 密钥对 ssh-keygen -f my_ssh_key -N ''
。
将 my_ssh_key.pub
添加到您的存储库服务器用户配置文件
- GitLab - https://gitlab.com/profile/keys
- Github - https://github.com/settings/keys
使用以下剧本进行测试:
_
---
- hosts: localhost
gather_facts: no
vars:
git_ssh_public_key: "Your public ssh key"
git_ssh_key: |
-----BEGIN RSA PRIVATE KEY-----
.... actual key here ....
-----END RSA PRIVATE KEY-----
tasks:
- name: Copy SSH public key file
copy:
content: "{{ git_ssh_public_key }}"
dest: /root/.ssh/id_rsa.pub
mode: 0644
- name: Copy SSH private key file
copy:
content: "{{ git_ssh_key }}"
#src: id_rsa
dest: /root/.ssh/id_rsa
mode: 0600
- name: Get new source from GIT
git:
repo: "git@gitlab.com:user/repo.git"
dest: "/var/www/"
depth: 1
accept_hostkey: yes
clone: yes
重要安全通知
如果您想在现实世界中使用此示例,请不要以明文形式保存您的私钥 - 使用 Ansible Vault.
您还应该不使用 root 作为您的 ansible 用户。在没有sudo权限的情况下创建新用户会更安全。
我正在尝试创建一个 Ansible 剧本,它将 运行 来自我们开发团队的计算机和 CI/CD 服务器。
剧本中的任务之一是从私有 git 存储库中获取我们项目的源代码。因为 playbook 必须从 CI/CD 服务器 运行 我们不能使用 SSH 转发。
我想到的是将必要的 SSH 私钥复制到远程主机,然后使用密钥从私有 git 存储库中克隆代码。
然而,尝试此操作时,克隆任务挂起。当尝试手动启动命令时,它会要求输入 SSH 私钥的密码。 SSH 密钥不使用密码(空白)。
谁能分享他们对这个(可能很常见)问题的解决方案?
如果有人需要,这是我目前的剧本:
- name: Create SSH directory
file: path=/root/.ssh state=directory
- name: Copy SHH key for Git access
copy:
content: "{{ git_ssh_key }}"
dest: /root/.ssh/id_rsa
owner: root
group: root
mode: 0600
# Also tried this, but it also hangs
#- name: Start SSH agent and add SSH key
# shell: eval `ssh-agent -s` && ssh-add
- name: Get new source from GIT
git:
key_file: /root/.ssh/id_rsa
repo: "git@gitlab.com:user/repo.git"
dest: "{{ staging_dir }}"
depth: 1
accept_hostkey: yes
clone: yes
我正在使用 ansible 2.3.1.0, python version = 2.7.12
我想问题是对于自动化任务,git 没有使用 ssh 密钥。我读过(通过浏览几分钟)git 2.10,你可以在调用 git 时提供 ssh 东西来传递......所以我想你可以尝试这样的事情:
git -c core.sshCommand='ssh -i /path/to/private/key' fetch origin
或者您想要使用的任何其他命令。您也可以在配置中修复它:
git config core.sshCommand 'ssh -i /path/to/private/key'
总之,希望对你有所帮助
Specify private SSH-key to use when executing shell command with or without Ruby?
以下是使其工作的步骤(在 MacOS Ubuntu LTS 上使用 Ansible 2.3.1 和 Python 2.7.10 进行测试):
生成没有密码的新 SSH 密钥对
ssh-keygen -f my_ssh_key -N ''
。将
my_ssh_key.pub
添加到您的存储库服务器用户配置文件- GitLab - https://gitlab.com/profile/keys
- Github - https://github.com/settings/keys
使用以下剧本进行测试:
_
---
- hosts: localhost
gather_facts: no
vars:
git_ssh_public_key: "Your public ssh key"
git_ssh_key: |
-----BEGIN RSA PRIVATE KEY-----
.... actual key here ....
-----END RSA PRIVATE KEY-----
tasks:
- name: Copy SSH public key file
copy:
content: "{{ git_ssh_public_key }}"
dest: /root/.ssh/id_rsa.pub
mode: 0644
- name: Copy SSH private key file
copy:
content: "{{ git_ssh_key }}"
#src: id_rsa
dest: /root/.ssh/id_rsa
mode: 0600
- name: Get new source from GIT
git:
repo: "git@gitlab.com:user/repo.git"
dest: "/var/www/"
depth: 1
accept_hostkey: yes
clone: yes
重要安全通知
如果您想在现实世界中使用此示例,请不要以明文形式保存您的私钥 - 使用 Ansible Vault.
您还应该不使用 root 作为您的 ansible 用户。在没有sudo权限的情况下创建新用户会更安全。