简体   繁体   English

在 Ansible 剧本中将变量插入花括号的最易读方式

[英]Most readable way to insert variable into curly braces in Ansible playbook

I'm trying to use the loop feature to set the following lines in a config file:我正在尝试使用循环功能在配置文件中设置以下行:

$nrconf{kernelhints} = 0;
$nrconf{ucodehints} = 0;

The code below fails with "jinja2.exceptions.TemplateSyntaxError: unexpected '}'".下面的代码因“jinja2.exceptions.TemplateSyntaxError: unexpected '}'”而失败。

- name: disable kernelhints on apt install
  lineinfile:
    path: /etc/needrestart/needrestart.conf
    regexp: '^.*\$nrconf\{{{ item }}\}.*$'
    line: '$nrconf\{{{ item }}\} = 0;'
  loop:
    - 'kernelhints'
    - 'ucodehints'

I understand that I can use either {% raw %} or {{ '{' }}{{ item }} or {{ '{' + item }} approach, but the resulting code look makes me sad.我知道我可以使用{% raw %}{{ '{' }}{{ item }}{{ '{' + item }}方法,但生成的代码看起来让我很难过。 Is there any trick to make the code clean and clear?有什么技巧可以使代码干净清晰吗?

You could use string formatting:您可以使用字符串格式:

    - name: disable kernelhints on apt install
      lineinfile:
        path: needrestart.conf
        regexp: '{{ "^.*\$nrconf{%s}.*$" % item }}'
        line: '{{ "$nrconf{%s} = 0" % item }}'
      loop:
        - 'kernelhints'
        - 'ucodehints'

I think that's a little more clear.我认为这更清楚一点。

When writing this sort of thing for myself, I try to avoid nested quotes and instead use some of YAML's quote operators, like this:在为自己编写此类内容时,我尽量避免嵌套引号,而是使用一些 YAML 的引号运算符,如下所示:

    - name: disable kernelhints on apt install
      lineinfile:
        path: needrestart.conf
        regexp: >-
          {{ "^.*\$nrconf{%s}.*$" % item }}
        line: >-
          {{ "$nrconf{%s} = 0" % item }}
      loop:
        - 'kernelhints'
        - 'ucodehints'

Operationally it's identical, but it removes one level of quoting so that you don't go insane if you need nested quotes inside your expression.在操作上它是相同的,但它删除了一级引号,这样如果您需要在表达式中嵌套引号,您就不会 go 发疯。

In regexp , put the braces into brackets.regexp中,将大括号放在方括号中。 In line , concatenate the stringsline中,连接字符串

        regexp: '^.*\$nrconf[{]{{ item }}[}].*$'
        line: "{{ '$nrconf{' ~ item ~ '} = 0;' }}"

Example of a complete playbook for testing用于测试的完整剧本示例

shell> cat pb.yml - hosts: localhost tasks: - name: disable kernelhints on apt install lineinfile: path: /tmp/needrestart.conf regexp: '^.*\$nrconf[{]{{ item }}[}].*$' line: "{{ '$nrconf{' ~ item ~ '} = 0;' }}" loop: - kernelhints - ucodehints

For example, given the file例如,给定文件

shell> cat /tmp/needrestart.conf $nrconf{undef} = 0;

The playbook gives running with --check --diff options该剧本给出了运行--check --diff选项

shell> ansible-playbook pb.yml -CD PLAY [localhost] ***************************************************************************** TASK [disable kernelhints on apt install] **************************************************** --- before: /tmp/needrestart.conf (content) +++ after: /tmp/needrestart.conf (content) @@ -1 +1,2 @@ $nrconf{undef} = 0; +$nrconf{kernelhints} = 0; changed: [localhost] => (item=kernelhints) --- before: /tmp/needrestart.conf (content) +++ after: /tmp/needrestart.conf (content) @@ -1 +1,2 @@ $nrconf{undef} = 0; +$nrconf{ucodehints} = 0; changed: [localhost] => (item=ucodehints) PLAY RECAP *********************************************************************************** localhost: ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

The result结果

shell> cat /tmp/needrestart.conf $nrconf{undef} = 0; $nrconf{kernelhints} = 0; $nrconf{ucodehints} = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM