是的,你可以让它与 ssh 转发一起工作
只要你在 git clone 中成为的用户是 sudoers 的一部分,那么他就不需要使用 sudo 来执行 git
因此,除了密钥转发所需的所有配置外,还有一个在 Ansible 文档中甚至提到的技巧。
高层流程如下:
在控制机器中启用代理转发
在目标机器中启用接受代理密钥
创建一个用户并将他(或她:)添加到 sudoers 组
使用 ansible 的 git 模块克隆 repo,变成:your-sudoer-user
另外,为了避免主机上的任何权限被拒绝,只需将其克隆到 ~/something
你可以随时复制或链接到任何你想要的地方
这里是显示将用户添加到 sudoers 的剧本部分的链接,它基本上是复制粘贴:Ansible: create a user with sudo privileges
像魅力一样工作
另外,请确保在 BitBucket 的常规设置中添加 SSH 公钥,而不是在每个项目中。否则,您的 ssh 密钥将仅适用于一个特定的存储库。但是,如果您在 bitbucket 常规设置中添加 ssh 密钥,它将适用于您的所有存储库
下面是使它工作的代码,suduer用户是“部署者”
# the tasks to CREATE A SUDOER GROUP
- name: Make sure we have a 'wheel' group
group:
name: wheel
state: present
become: yes
- name: Allow 'wheel' group to have passwordless sudo
lineinfile:
dest: /etc/sudoers
state: present
regexp: '^%wheel'
line: '%wheel ALL=(ALL) NOPASSWD: ALL'
validate: 'visudo -cf %s'
become: yes
- name: Add sudoers users to wheel group
user: name=deployer groups=wheel append=yes state=present createhome=yes
become: yes
# tasks to ADD REPO with Ansible's GIT MODULE
- name: Add Git Repo - BitBucket
git:
repo: 'git@bitbucket.org:<your_username>/<your_repo>.git'
dest: ~/code # note this destination, you will avoid permissions issues
accept_hostkey: yes # btw, this is for the ssh key forwarding
recursive: no
become: deployer # this guy (or gal) is a sudoer by now
# 额外的“hack”可以一次性更改文件和文件夹的权限,它与大写 X 以及它适用于什么和不适用什么有关。也是从另一个stackoverflow中提取的
- name: Set perms on new Code repo to deployer:deployer dirs-0755 and files-0644
file:
path: ~/code
state: directory
owner: deployer
group: deployer
mode: u=rwX,g=rX,o=rX
recurse: yes
become: yes