大网数据广告活动
服务器运维问答

Managing multiple remote user accounts and pubkeys with Ansible

· 52CE 我爱测

I'm trying to work out a way to manage a single user's pubkey across multiple sites, as well as dropping that key into multiple $HOME directories. Reason being; our devs login as themselves to check on things, run sudo etc, howev…

问题原文

原帖链接 · Server Fault · CC BY-SA 4.0

I'm trying to work out a way to manage a single user's pubkey across multiple sites, as well as dropping that key into multiple $HOME directories. Reason being; our devs login as themselves to check on things, run sudo etc, however they deploy code as a passenger user over SSH so their pubkey needs to be in ~/home/passenger/.ssh/authorized_keys as well.

I'm not sure if changing AuthorizedKeysFile in /etc/ssh/sshd_config would make this less crazy but since I'm almost there with my current playbook, I thought I'd post the question as-is and see what comes of it. Perhaps this would indeed be better solved another way.

In my homedirs.yml file, I create user's home directories (they exist in LDAP) and then move their pubkey into place. This works fine for their own home directory, but the passenger user needs this key as well. Since I may use this playbook for other users and hosts, I have defined a to_user: list which defines which home directories the pubkey should be setup for.

How do I loop over the {{ item.to_user }} list in the authorized_key: play where {{ item.name }} is currently referenced?

playbook

---
- hosts: all
  vars_files:
    - ../group_vars/homedirs.yml
  gather_facts: false

  tasks:
    - name: Set up home directory
      when: inventory_hostname in item.allowed_hosts
      file:
        path: "/home/{{ item.name }}"
        state: directory
        owner: "{{ item.name }}"
        group: "{{ item.group }}"
        mode : 0750
      with_items: "{{ homedirs }}"


    - name: setup ssh pub key
      when: inventory_hostname in item.allowed_hosts
      authorized_key:
        user: '{{ item.name }}'
        state: present
        key: '{{ item.pubkey }}'
      with_items: "{{ homedirs }}"

homedirs.yml

# homedirs.yml
homedirs:
  - name   : usr123
    group  : developers
    pubkey :  "{{ lookup('file', '../group_vars/pubkeys/[email omitted]') }}"
    to_user:
      - user123
      - passenger
    allowed_hosts  :
      - web1.my.com
      - web1.test.my.com

  - name   : usr555
    group  : admin
    pubkey :  "{{ lookup('file', '../group_vars/pubkeys/[email omitted]') }}"
    to_user:
      - user555
      - sysadmin
    allowed_hosts  :
      - blah.blah.host

已采纳解答

原帖链接 · techraf · CC BY-SA 4.0

This is a classic requirement for a with_subelements loop in Ansible:

- name: Set up home directory
  when: inventory_hostname in item.0.allowed_hosts
  file:
    path: "/home/{{ item.1 }}"
    state: directory
    owner: "{{ item.1 }}"
    group: "{{ item.0.group }}"
    mode : 0750
  with_subelements:
    - "{{ homedirs }}"
    - to_user

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-07-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。