Ansible用拆分变量在文件中添加行

时间:2019-05-13 14:12:38

标签: ansible ansible-inventory

我制作了一个用于配置服务器的UI。我将新IP或服务器名放入txt字段中,并根据其所使用的服务器类型(而不是使用清单文件)运行了可移植的剧本。我正在尝试为{{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。有时是一个,有时是10。

1 个答案:

答案 0 :(得分:0)

在以下解决方案中:

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

我在测试中确保了该解决方案在需要时能够很好地处理重复项。

这是演示手册

---
- 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