你可以用这个剧本来计算
wrapper.java.classpath
条目,然后在最后一行后追加您需要的另外两个条目。
剧本:
tasks:
- name: cat file
shell: cat testt.txt | grep '^wrapper.java.classpath'
register: file_grep
- name: find max number
set_fact:
max_number: "{{ max_number|default(0)|int + 1|int }}"
loop: "{{ file_grep.stdout_lines }}"
- name: Adding classpath entries for my jar
blockinfile:
dest: testt.txt
block: |
wrapper.java.classpath.{{ max_number|int + 1|int }}=%KARAF_HOME%/system/my*.jar
wrapper.java.classpath.{{ max_number|int + 2|int }}=%KARAF_HOME%/system/my2*.jar
insertafter: "^(wrapper.java.classpath)(.+)$"
backup: yes
示例配置文件(我的测试中的testt.txt):
wrapper.java.classpath.1=%KARAF_BASE%/lib/boot/*.jar
wrapper.java.classpath.2=%KARAF_BASE%/lib/wrapper/*.jar
wrapper.java.classpath.3=%KARAF_HOME%/system/org/ops4j/pax/logging/pax-logging-api/*/pax-logging-api-*.jar
wrapper.java.classpath.4=%KARAF_BASE%/lib/boot/*.jar
wrapper.java.classpath.5=%KARAF_BASE%/lib/wrapper/*.jar
random config line 1
more random config line 1
and one last random line
运行和结果:
[root@optima-ansible ILIAS]# ansible-playbook testt.yml
PLAY [localhost] ****************************************************************************************************************************************************************************************************
TASK [cat file] *****************************************************************************************************************************************************************************************************
changed: [localhost]
TASK [find max number] **********************************************************************************************************************************************************************************************
ok: [localhost] => (item=wrapper.java.classpath.1=%KARAF_BASE%/lib/boot/*.jar)
ok: [localhost] => (item=wrapper.java.classpath.2=%KARAF_BASE%/lib/wrapper/*.jar)
ok: [localhost] => (item=wrapper.java.classpath.3=%KARAF_HOME%/system/org/ops4j/pax/logging/pax-logging-api/*/pax-logging-api-*.jar)
ok: [localhost] => (item=wrapper.java.classpath.4=%KARAF_BASE%/lib/boot/*.jar)
ok: [localhost] => (item=wrapper.java.classpath.5=%KARAF_BASE%/lib/wrapper/*.jar)
TASK [Adding classpath entries for my jar] **************************************************************************************************************************************************************************
changed: [localhost]
PLAY RECAP **********************************************************************************************************************************************************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0
[root@optima-ansible ILIAS]# cat testt.txt
wrapper.java.classpath.1=%KARAF_BASE%/lib/boot/*.jar
wrapper.java.classpath.2=%KARAF_BASE%/lib/wrapper/*.jar
wrapper.java.classpath.3=%KARAF_HOME%/system/org/ops4j/pax/logging/pax-logging-api/*/pax-logging-api-*.jar
wrapper.java.classpath.4=%KARAF_BASE%/lib/boot/*.jar
wrapper.java.classpath.5=%KARAF_BASE%/lib/wrapper/*.jar
# BEGIN ANSIBLE MANAGED BLOCK
wrapper.java.classpath.6=%KARAF_HOME%/system/my*.jar
wrapper.java.classpath.7=%KARAF_HOME%/system/my2*.jar
# END ANSIBLE MANAGED BLOCK
random config line 1
more random config line 1
and one last random line[root@optima-ansible ILIAS]#
我们很高兴能找到以
包装器.java.classpath
,我们计算它们,然后附加(
insertafter
)将“index”数字动态递增到下2个值后的2行。
请检查一下
blockinfile
用于配置的模块文档
# BEGIN ANSIBLE MANAGED BLOCK
根据您的需要排列。
希望有帮助。