AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1722412
Accepted
alexanderadam
alexanderadam
Asked: 2022-05-23 12:30:24 +0800 CST2022-05-23 12:30:24 +0800 CST 2022-05-23 12:30:24 +0800 CST

(重新)在 Ansible 上使用用户输入启动 systemd 单元

  • 772

像gocryptfs这样的工具在与 systemd 结合使用时具有一些优势,因为可以轻松定义依赖关系、状态和权限。

由于它需要密码,它要么需要系统上的纯文本文件,要么需要使用systemd-ask-password. 通常首选标准输入变体,因为纯文本文件中的密码短语有一些缺点。

虽然Ansible 有一个 systemd 模块,但它似乎无法直接插入此类参数。

因此,我很好奇在使用 Ansible 时,使用密码输入处理服务(重新)重启的首选方式是什么。

会做类似这个伪代码的事情

- name: Start GoCryptFs
  systemd:
    name: gocryptfs
    state: started
    daemon_reload: true
    args:
      stdin: "{{ gocryptfs_password }}"
ansible systemd
  • 1 1 个回答
  • 186 Views

1 个回答

  • Voted
  1. Best Answer
    Vladimir Botka
    2022-05-23T14:26:31+08:002022-05-23T14:26:31+08:00

    配置gocryptfs以使用 option -extpass CMD。引用man gocryptfs

    使用外部程序(如 ssh-askpass)来提示密码。该程序应在标准输出上返回密码,尾随换行符被 gocryptfs 剥离。

    例如,在远程主机上创建一个脚本,该脚本返回变量gocryptfs_password的值

    shell> ssh admin@test_11 sudo cat /root/bin/gocryptfs-extpass.sh
    #!/bin/sh
    echo ${gocryptfs_password}
    

    也可以通过 Ansible 实现自动化

        - file:
            state: directory
            path: /root/bin
            owner: root
            group: wheel
            mode: '0755'
        - copy:
            dest: /root/bin/gocryptfs-extpass.sh
            owner: root
            group: wheel
            mode: '0700'
            content: |
              {{ '#' }}!/bin/sh
              echo ${gocryptfs_password}
    

    将密码放入控制器的文件中并加密

    shell> cat vault/test_11/gocryptfs_password.yml 
    gocryptfs password at test_11
    

    下面的剧本从文件中读取密码并为命令设置环境

    shell> cat pb.yml
    - hosts: test_11
      vars:
        gocryptfs_password_file: "vault/{{ inventory_hostname }}/gocryptfs_password.yml"
      tasks:
        - command: /root/bin/gocryptfs-extpass.sh
          register: result
          environment:
            gocryptfs_password: "{{ lookup('file', gocryptfs_password_file) }}"
          no_log: true
        - debug:
            var: result.stdout
    

    给

    ok: [test_11] => 
      result.stdout: gocryptfs password at test_11
    

    如果您设法将gocryptfs配置为使用-extpass /root/bin/gocryptfs-extpass.sh下面的任务应该可以完成这项工作

    - name: User systemctl import environment gocryptfs_password
      command: systemctl --user import-environment gocryptfs_password
    
    - name: Start GoCryptFs
      systemd:
        name: gocryptfs
        state: started
        daemon_reload: true
      environment:
        gocryptfs_password: "{{ lookup('file', gocryptfs_password_file) }}"
      no_log: true
    

    (未测试)


    笔记

    • 该方案应通过大部分安全要求:

      • 加密密码远程存储
      • 密码的传输是加密的
      • 密码的范围仅限于单个任务
    • 适当设置gocryptfs_password的值。有很多选择。例如,使用密码存储。然后,使用community.general.passwordstore查找插件而不是ansible.builtin.file

      environment:
        gocryptfs_password: "{{ lookup('community.general.passwordstore',
                                       'test_11/gocryptfs_password' }}"
    
    • 看
       shell> ansible-doc -t lookup community.general.passwordstore
    
    • 有关如何将环境变量gocryptfs_password传递给systemd的信息,请参阅将环境变量从当前 shell 传递到 systemd 单元。(信用@alexanderadam。请参阅下面的评论。)
       shell> systemctl --user import-environment gocryptfs_password
    
    • 请参阅正确的 Bash 和 shell 脚本变量大写。引用:“如果它是您的变量,则将其小写。如果您将其导出,则将其大写。......对范围为单个脚本或块的所有变量使用'蛇形大小写'(所有小写和下划线)。”
    • 2

相关问题

  • 系统路径检查网络驱动器

  • 防止 systemd / journald 记录套接字调用[关闭]

  • 如何在剧本中混合特权用户和非特权用户?

  • 如何在 group_vars 中使用模板变量

  • 第二次运行 playbook 时 Ansible 挂起

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve