4

作为 Salam-o-Alikum,我编写了一个 Ansible 剧本,用于在 RedHat 和 Ubuntu 上设置 GRUB 引导加载程序密码,没有错误,我可以在两个位置看到Grub2.cfg的变化。奇怪的是,当我重新启动两台机器时,Ubuntu 机器会询问用户名和密码,但 Redhat 机器不会。我已经看到 50 多个教程过程是相同的,而且非常简单,但我不明白它为什么会这样。任何帮助将不胜感激。

这是我尝试过的。

硬化.yml

   ---
   - hosts: localhost
     become: yes
     gather_facts: true
     vars:
         grub_password_v1_passwd: puffersoft
         grub_password_v2_passwd: grub.pbkdf2.sha512.10000.A4DE89CBFB84A34253A71D5DD4939BED709AB2F24E909062A902D4751E91E3E82403D9D216BD506091CAA5E92AB958FBEF4B4B4B7CB0352F8191D47A9C93239F.0B07DD3D5AD46BF0F640136D448F2CFB84A6E05B76974C51B031C8B31D6F9B556802A28E95A5E65EC1F95983E24618EE2E9B21A0233AAA8D264781FE57DCE837
         grub_user: cloud_user
     tasks:
    - stat: path=/sys/firmware/efi/efivars/
      register: grub_efi
    - debug: vars=grub_efi
      when: ansible_distribution == 'Redhat'
      tags: grub-password

    - name: "Installing Template on Redhat Systems"
      template:
            src: grub-redhat.j2
            dest: /etc/grub.d/01_users
            owner: root
            group: root
            mode: '0700'
            notify:
                    - grub2-mkconfig EFI
                    - grub2-mkconfig MBR
      when: ansible_distribution == 'Redhat'
      tags: grub-password

    - name: "Installing Template on Ubuntu Systems"
      template:
            src: grub-ubuntu.j2
            dest: /etc/grub.d/40_custom
            owner: root
            group: root
            mode: '0700'
            notify:
                    - grub2-mkconfig EFI
                    - grub2-mkconfig MBR
      when: ansible_distribution == 'Ubuntu'
      tags: grub-password

    - name: "Grub EFI | Add Password"
      lineinfile: dest=/etc/grub2-efi.cfg regexp = "^password_pbkdf2 {{ grub_user }}" state=present insertafter = EOF line= "password_pbkdf2 {{ grub_user }} {{ grub_password_v2_passwd }}"
      when: grub_efi.stat.exists == True
      tags: grub-password

    - name: "Grub v2 MBR | Add Password"
      lineinfile: dest=/etc/grub2.cfg regexp = "^password_pbkdf2 {{ grub_user }}" state=present insertafter = EOF line= "password_pbkdf2 {{ grub_user }} {{ grub_password_v2_passwd }}"
      when: grub_efi.stat.exists == False

    - name: "grub2-mkconfig EFI"
      command: grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
      when: grub_efi.stat.exists == True

    - name: "grub2-mkconfig MBR"
      command: grub2-mkconfig -o /boot/grub2/grub.cfg
      when: grub_efi.stat.exists == False

grub-redhat.j2

#!/bin/sh -e
cat << EOF
if [ -f \${prefix}/user.cfg ]; then
  source \${prefix}/user.cfg
  if [ -n "\${GRUB2_PASSWORD}" ]; then
    set superusers="root"
    export superusers
    password_pbkdf2 root \${GRUB2_PASSWORD}
  fi
fi
set supperusers="{{ grub_user  }}"
password_pbkdf2 {{ grub_user }} {{ grub_password_v2_passwd  }}
EOF

grub-ubuntu.j2

#!/bin/sh
tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
set superusers="{{grub_user}}"
password_pbkdf2 {{grub_user}} {{grub_password_v2_passwd}}

Ansible 分发

Red Hat 7.7 Maipo 和 Ubuntu 18.04 仿生

4

1 回答 1

1

grub-redhat.j2 有一些拼写错误。

第 11 行:set supperusers="{{ grub_user }}"

改成:set superusers="{{ grub_user }}"

第 12 行:password_pbkdf2 {{ grub_user }} {{ grub_password_v2_passwd }}

改成:password_pbkdf2 {{ grub_user }} {{ grub_password_v2_passwd }}

如果您要在多台机器上使用相同的 grub 密码,您可能需要考虑使用ansible-vault encrypt_string来加密原始密码,然后创建一个运行 grub2-mkpasswd-pbkdf2 的附加任务(grub2-tools-的一部分)最小)与命令模块并将其传递给拱形变量。注册该任务的输出(例如 grub_password_v2_passwd)将导致您运行 playbook 的每个目标都接收到唯一的哈希,即使基础密码相同。expect 模块非常适合这个:

expect:
  command: grub2-mkpasswd-pbkdf2
  responses:
    Enter password: '{{ vaulted_raw_password }}'
    Reenter password: '{{ vaulted_raw_password }}'
no_log: true
于 2020-01-15T00:40:03.703 回答