zuul-jobs/roles/upload-git-mirror/tasks/main.yaml
David Moreau Simard ed9f1d14a0
Add parameter to upload-git-mirror to specify ssh host key
If we attempt to push a mirror to a host we've never connected to
before, it won't be in the SSH known hosts and the mirror will fail.

This makes a new host_key parameter required and the role takes care
of setting it up in the known_hosts for the duration of the job.

Change-Id: I4bd40a289003fe3e88c9af4029576236c064fc3d
Depends-On: https://review.openstack.org/#/c/649183/
2019-04-01 16:34:59 -04:00

61 lines
1.9 KiB
YAML

- block:
- name: Create SSH private key tempfile
tempfile:
state: file
register: ssh_private_key_tmp
- name: Set up private key
copy:
content: "{{ git_mirror_credentials.ssh_key }}"
dest: "{{ ssh_private_key_tmp.path }}"
mode: 0600
- name: Generate SSH configuration
set_fact:
ssh_config: |
host {{ git_mirror_credentials.host }}
HostName {{ git_mirror_credentials.host }}
IdentityFile {{ ssh_private_key_tmp.path }}
User {{ git_mirror_credentials.user }}
- name: Write SSH configuration to ~/.ssh/config
blockinfile:
state: present
path: "{{ ansible_user_dir }}/.ssh/config"
create: yes
mode: 0600
block: "{{ ssh_config }}"
- name: Add host key to known hosts
known_hosts:
state: present
name: "{{ git_mirror_credentials.host }}"
key: "{{ git_mirror_credentials.host_key }}"
- name: Mirror the git repository
command: git push --mirror {{ git_mirror_credentials.user }}@{{ git_mirror_credentials.host }}:{{ git_mirror_repository }}
args:
chdir: "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}"
tags:
- skip_ansible_lint
always:
# Registered variables below are only used for integration testing
- name: Remove SSH private key from disk
command: "shred --remove {{ ssh_private_key_tmp.path }}"
register: git_mirror_key_removed
- name: Remove SSH configuration in ~/.ssh/config
blockinfile:
state: absent
path: "{{ ansible_user_dir }}/.ssh/config"
mode: 0600
block: "{{ ssh_config }}"
register: git_mirror_ssh_config_removed
- name: Remove host key from known hosts
known_hosts:
state: absent
name: "{{ git_mirror_credentials.host }}"
key: "{{ git_mirror_credentials.host_key }}"
register: git_mirror_host_key_removed