首页 文章

Ansible授权密钥模块无法读取公钥

提问于
浏览
0

我正在尝试使用ansible(版本2.1.2.0)在我们的服务器网络上创建命名的ssh访问 . 从跳转框运行ansible我正在创建一组用户并使用users模块创建私钥/公钥对

- user:
      name: "{{ item }}"
      shell: /bin/bash
      group: usergroup
      generate_ssh_key: yes
      ssh_key_comment: "ansible-generated for {{ item }}"
    with_items: "{{ list_of_usernames }}"

从那里我想将公钥复制到每个远程服务器上的authorized_users文件 . 我正在使用authorized_key_module来获取并复制用户生成的公钥作为远程服务器上的authorized_key:

- authorized_key:
      user: "{{ item }}"
      state: present
      key: "{{ lookup('file', '/home/{{ item }}/.ssh/id_rsa.pub') }}"
    with_items:
      - "{{ list_of_usernames }}"

我发现查找将失败,因为它无法访问用户的.ssh文件夹中的pub文件 . 然而,如果我以root用户身份运行ansible(这对我来说不是我愿意接受的选项),它会愉快地运行 .

fatal: [<ipAddress>]: FAILED! => {"failed": true, "msg": "{{ lookup('file', '/home/testuser/.ssh/id_rsa.pub') }}: the file_name '/home/testuser/.ssh/id_rsa.pub' does not exist, or is not readable"}

除了以root身份运行ansible之外,还有更好的方法吗?

编辑:对不起我忘记提到我正在运行成为:是的

编辑#2:下面是我正在尝试运行的浓缩手册,当然在这个实例中它会将authorized_key添加到localhost但显示我正面临的错误:

---
- hosts: all
  become: yes
  vars:
    active_users: ['user1','user2']
  tasks:

  - group:
      name: "users"

  - user:
      name: "{{ item }}"
      shell: /bin/bash
      group: users
      generate_ssh_key: yes
    with_items: "{{ active_users }}"

  - authorized_key:
      user: "{{ item }}"
      state: present
      key: "{{ lookup('file', '/home/{{ item }}/.ssh/id_rsa.pub') }}"
    become: yes
    become_user: "{{ item }}"
    with_items:
      - "{{ active_users }}"

2 回答

  • 2

    好的,问题在于查找插件 .
    它在ansible控制主机上执行,具有运行 ansible-playbookbecome: yes don 't elevate plugins'权限的用户的权限 .

    要解决此问题,请捕获 user 任务的结果并在其他任务中使用其输出:

    - user:
        name: "{{ item }}"
        shell: /bin/bash
        group: docker
        generate_ssh_key: yes
        ssh_key_comment: "ansible-generated for {{ item }}"
      with_items:
        - ansible5
        - ansible6
      register: new_users
      become: yes
    
    - debug: msg="user {{ item.item }} pubkey {{ item.ssh_public_key }}"
      with_items: "{{ new_users.results }}"
    

    虽然您需要委派其中一些任务,但这个想法是一样的 .

  • 2

    在大多数linux / unix机器上,只有两个帐户可以访问 /home/testuser/.ssh/id_rsa.pubroottestuser ,因此如果要修改这些文件,则需要 roottestuser .

    您可以使用 become 使用 become . 你说你没有提到你运行ansible的用户是否有sudo访问权限 . 如果是这样,并且使用sudo对你来说很好,那么你可以简单地做:

    - authorized_key:
          user: "{{ item }}"
          state: present
          key: "{{ lookup('file', '/home/{{ item }}/.ssh/id_rsa.pub') }}"
        with_items:
          - "{{ list_of_usernames }}"
        become: yes
    

    默认情况下,这将使用sudo以root身份运行这些命令,同时保持其他任务由非root用户运行 . 这也是使用ansible运行的首选方法 .

    如果您不希望这样,并且希望尽可能保持用户无root,则需要将命令作为 testuser (以及您要修改的任何其他用户)运行 . 这意味着您仍然需要修补 sudoers 文件,以允许您的ansible用户转换为任何这些用户(这也可以打开一些安全问题 - 尽管没有能够以root身份运行任何东西),之后你可以做的:

    - authorized_key:
          user: "{{ item }}"
          state: present
          key: "{{ lookup('file', '/home/{{ item }}/.ssh/id_rsa.pub') }}"
        with_items:
          - "{{ list_of_usernames }}"
        become: yes
        become_user: "{{ item }}"
    

    使用这种方法有一些注意事项,因此您可能需要阅读有关ansible文档的完整Privilege Escalation页面,尤其是在您尝试此操作之前的Unpriviliged Users部分 .

相关问题