Ansible 使用拆分变量向文件添加行

Ansible add lines to file with split variable

我制作了一个 UI 用于配置我们的服务器。我将新的 IP 或服务器名称放入一个 txt 字段中,并根据服务器类型在其上设置 ansbible 剧本 运行,而不是使用清单文件。我试图让 ansible 为 {{ips}} 变量中的每个值添加行到文件中,而不是将它们全部放在同一行上。

我尝试了几种不同的方法,包括 lineinfile、blockinfile 和 replace,似乎比其他方法更接近我,但我仍然无法获得我想要的结果。

- name: Add new lines
    replace:
      path: /foo/bar
      regexp: '^# Test Line'
      replace: "# Test Line\n/foo/bar {{ ips }}"

这将在 ips 变量中添加一行所有 IP。

# Test Line
/foo/bar test1,test2

我想得到的是。

# Test Line
/foo/bar test1
/foo/bar test2

很难计算一次变量中有多少个 ip。有时是 1 有时是 10。

在以下解决方案中:

  • 我正在使用 python split 方法,您可以使用 insde jinja 表达式从逗号分隔的字符串 ips 字符串中创建一个列表。这只有在格式保持不变的情况下才有效。
  • 我使用 lineinfile 遍历该列表,使用 insertafter 选项

我在测试中确定该解决方案可以在需要时很好地处理欺骗。

这是演示手册

---
- name: test for So
  hosts: localhost

  vars:
    test_file: /tmp/test_file.txt
    first_ips: 127.0.0.1,127.0.0.2
    more_ips: 10.35.26.1,10.35.26.2
    dupe_ips: 127.0.0.2,10.35.26.1

  tasks:
    - name: Make sure we start from scratch
      shell: echo "I'm a line of text\n\n# Test Line\n\nThis is the end, my only friend" > {{ test_file }}

    - name: Show file at start
      debug:
        msg: "{{ lookup('file', test_file).split('\n') }}"

    - name: Add first ips
      lineinfile:
        path: "{{ test_file }}"
        insertafter: "# Test Line"
        line: "/foo/bar {{ item }}"
      loop: "{{ first_ips.split(',') }}"

    - name: Show file with first ips
      debug:
        msg: "{{ lookup('file', test_file).split('\n') }}"

    - name: Add second list of ips
      lineinfile:
        path: "{{ test_file }}"
        insertafter: "# Test Line"
        line: "/foo/bar {{ item }}"
      loop: "{{ more_ips.split(',') }}"

    - name: Show file with more ips
      debug:
        msg: "{{ lookup('file', test_file).split('\n') }}"

    - name: Test if dupes are handled correctly
      lineinfile:
        path: "{{ test_file }}"
        insertafter: "# Test Line"
        line: "/foo/bar {{ item }}"
      loop: "{{ dupe_ips.split(',') }}"

    - name: Show file that should not have changed
      debug:
        msg: "{{ lookup('file', test_file).split('\n') }}"

这是结果。我的调试任务将结果文件显示为行列表,以便在 运行 剧本时显示。 cat 如果您想查看没有多余引号和逗号的结果,请自己创建文件。

PLAY [test for So] *******************************************************************

TASK [Gathering Facts] ***************************************************************
ok: [localhost]

TASK [Make sure we start from scratch] ***********************************************
changed: [localhost]

TASK [Show file at start] ************************************************************
ok: [localhost] => {
    "msg": [
        "I'm a line of text",
        "",
        "# Test Line",
        "",
        "This is the end, my only friend"
    ]
}

TASK [Add first ips] *****************************************************************
changed: [localhost] => (item=/foo/bar 127.0.0.1)
changed: [localhost] => (item=/foo/bar 127.0.0.2)

TASK [Show file with first ips] ******************************************************
ok: [localhost] => {
    "msg": [
        "I'm a line of text",
        "",
        "# Test Line",
        "/foo/bar 127.0.0.2",
        "/foo/bar 127.0.0.1",
        "",
        "This is the end, my only friend"
    ]
}

TASK [Add second list of ips] ********************************************************
changed: [localhost] => (item=/foo/bar 10.35.26.1)
changed: [localhost] => (item=/foo/bar 10.35.26.2)

TASK [Show file with more ips] *******************************************************
ok: [localhost] => {
    "msg": [
        "I'm a line of text",
        "",
        "# Test Line",
        "/foo/bar 10.35.26.2",
        "/foo/bar 10.35.26.1",
        "/foo/bar 127.0.0.2",
        "/foo/bar 127.0.0.1",
        "",
        "This is the end, my only friend"
    ]
}

TASK [Test if dupes are handled correctly] *******************************************
ok: [localhost] => (item=/foo/bar 127.0.0.2)
ok: [localhost] => (item=/foo/bar 10.35.26.1)

TASK [Show file that should not have changed] ****************************************
ok: [localhost] => {
    "msg": [
        "I'm a line of text",
        "",
        "# Test Line",
        "/foo/bar 10.35.26.2",
        "/foo/bar 10.35.26.1",
        "/foo/bar 127.0.0.2",
        "/foo/bar 127.0.0.1",
        "",
        "This is the end, my only friend"
    ]
}

PLAY RECAP ***************************************************************************
localhost                  : ok=9    changed=3    unreachable=0    failed=0