Lau*_*kas 12 git ansible ansible-2.x
我正在尝试创建一个Ansible playbook,它将从我们的开发团队计算机和CI/CD服务器运行.
该手册中的任务之一是从私有git存储库获取我们项目的源代码.由于剧本必须从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
Run Code Online (Sandbox Code Playgroud)
我在用 ansible 2.3.1.0, python version = 2.7.12
以下是使其工作的步骤(在MacOS,Ubuntu LTS上使用Ansible 2.3.1和Python 2.7.10测试):
生成没有密码的新SSH密钥对ssh-keygen -f my_ssh_key -N ''。
添加my_ssh_key.pub到您的存储库服务器用户配置文件
使用以下剧本进行测试:
_
---
- 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
Run Code Online (Sandbox Code Playgroud)
重要安全通知
如果您想在现实世界中使用此示例,请不要将您的私钥保存为纯文本-使用Ansible Vault。
你也应该不要使用root作为您ansible用户。创建没有sudo权限的新用户会更安全。
| 归档时间: |
|
| 查看次数: |
3433 次 |
| 最近记录: |