Ansible 更新authorized_keys 文件

标签 ansible

我正在尝试编写一个通用操作,以使我们所有服务器上的 authorized_keys 文件保持最新。基本上,我想保留开发人员列表并指定允许他们连接到哪些服务器。

这就是我所拥有的...

ssh.yaml

  tasks:
    - name: 'provision dev-app servers with correct keys'
      authorized_key:
        user: 'deployment'
        key: '{{ item.key }}'
        comment: '{{ item.email }}'
        state: '{{ item.state }}'
      when: "('dev-app' in group_names) and ('dev-app' in item.servers or 'all' in item.servers)"
      with_items:
        - '{{ users }}'

vars.yaml

  - name: 'Jacob Haug'
    username: 'jacob'
    email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="066c67656964466c676569646e6773612865696b" rel="noreferrer noopener nofollow">[email protected]</a>'
    key: "{{ lookup('file', 'permissions/keys/jacob.pub') }}"
    servers:
      - 'all'
    state: 'present'

  - name: 'Some Developer'
    username: 'developer'
    email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e98d8c9f8c8586998c9ba98c91888499858cc78a8684" rel="noreferrer noopener nofollow">[email protected]</a>'
    key: "{{ lookup('file', 'permissions/keys/developer.pub') }}"
    servers:
      - 'dev-app'
      - 'dev-admin'
      - 'prd-app'
      - 'prd-admin'
      - 'prd-scraper'
    state: 'present'

这非常有效。但是,我想删除以其他方式添加到 authorized_keys 中的任何项目。根据authorized_keys模块的文档,我可以使用exclusive参数,但是,该选项不支持循环,并且需要我在一个批处理操作中传递所有 key 。

https://docs.ansible.com/ansible/latest/modules/authorized_key_module.html

什么是更好的方法来做到这一点?任何建议将不胜感激。

最佳答案

来自您在有关独占选项的问题中所指向的文档

Whether to remove all other non-specified keys from the authorized_keys file. Multiple keys can be specified in a single key string value by separating them by newlines.

我会尝试什么:使用 set_fact 和循环来创建一个具有所需内容的 var,并在下一个任务中使用 authorized_keys 模块中的该 var 和 专有选项。类似的东西(待全面测试和调整):

tasks:
    - name: 'get keys to declare'
      set_fact:
        declare_keys: >-
          {{
            declare_keys | default([])
            +
            [item.key + ' ' + item.email])
          }}
      when:
        - "item.state == 'present'"
        - "'dev-app' in group_names"
        - "'dev-app' in item.servers or 'all' in item.servers"
      with_items:
        - '{{ users }}'

    - name: 'provision dev-app servers with correct keys'
      authorized_key:
        user: 'deployment'
        key: '{{ declare_keys.join("\n") }}'
        exclusive: yes

关于Ansible 更新authorized_keys 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55699883/

相关文章:

azure - 使用 cloud-init 作为与 ansible 一起使用的 custom_data

python - 在使用 python ansible 模块为新主机执行 ansible playbook 时,我们可以忽略在 .ssh 目录中添加主机的要求吗?

ansible - ansible 中的冲突操作声明

xpath - 运行 Ansible 任务会引发共享连接关闭错误

git - Ansible git 模块不 checkout 分支

yaml - ansible - "when"条件不适用于逻辑 "AND"运算符

ansible - 使用 ansible-galaxy 直接从 git URI 安装 Ansible 集合

json - 按属性过滤对象并使用 jmespath 中的键进行选择

linux - 使用 Ansible 比较两个远程主机上的文件

amazon-web-services - 如何在 docker 容器上运行 ansible-playbook 以在 AWS EC2 Ubuntu 实例上执行命令?