gpt4 book ai didi

regex - Ansible:在 GRUB 命令行中插入单词

转载 作者:行者123 更新时间:2023-12-04 01:09:48 24 4
gpt4 key购买 nike

我想使用 Ansible 的 lineinfile 或替换模块来添加单词 splash到 GRUB 中的命令行。

它应该适用于以下所有示例:

示例 1:

  • 之前:GRUB_CMDLINE_DEFAULT=""
  • 之后:GRUB_CMDLINE_DEFAULT="splash"

  • 示例 2:
  • 之前:GRUB_CMDLINE_DEFAULT="quiet"
  • 之后:GRUB_CMDLINE_DEFAULT="quiet splash"

  • 示例 3:
  • 之前:GRUB_CMDLINE_DEFAULT="quiet nomodeset"
  • 之后:GRUB_CMDLINE_DEFAULT="quiet nomodeset splash"

  • 帖子 Ansible: insert a single word on an existing line in a file很好地解释了如何在没有引号的情况下完成此操作。但是,我无法让它在引号内插入单词。

    为了添加单词 splash 在 Ansible 角色或剧本中需要输入什么?到命令行如图所示?

    最佳答案

    您可以在没有 shell 的情况下执行此操作输出,带 2 lineinfiles模块。
    在您的示例中,您正在搜索 splash :

    - name: check if splash is configured in the boot command
    lineinfile:
    backup: true
    path: /etc/default/grub
    regexp: '^GRUB_CMDLINE_LINUX=".*splash'
    state: absent
    check_mode: true
    register: grub_cmdline_check
    changed_when: false

    - name: insert splash if missing
    lineinfile:
    backrefs: true
    path: /etc/default/grub
    regexp: "^(GRUB_CMDLINE_LINUX=\".*)\"$"
    line: '\1 splash"'
    when: grub_cmdline_check.found == 0
    notify: update grub
    诀窍是如果我们能找到 splash 就尝试删除该行。某处,但只做检查 check_mode: true .如果找到该术语 ( found > 0),那么我们不需要更新该行。如果没有找到,则意味着我们需要插入它。我们将它附加在后面的引用中。

    关于regex - Ansible:在 GRUB 命令行中插入单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55844981/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com