我想在我的karaf配置文件中添加一行,以下是配置代码段
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
现在我希望ansible脚本统计所有" wrapper.java.classpath。*"并在上面插入新的两行,如
wrapper.java.classpath.4=%KARAF_HOME%/system/my*.jar
wrapper.java.classpath.5=%KARAF_HOME%/system/my2*.jar
保持这一点,下一个条目将是wrapper.java.classpath.4计数可能会因安装而改变。
我正在调查lineinfile,但它不能正常工作,无法用regexp
收集计数 - name: Adding classpath entries for my jar
lineinfile:
dest: $KARAF_HOME/etc/wrapper.conf
regexp: 'wrapper\.java\.classpath\.*'
line: 'wrapper.java.classpath.4=%KARAF_HOME%/system/my*.jar'
backrefs: yes
目前在shell脚本中,我正如下面所做的那样
NUM_CLASSPATH_ENTRIES=$(less ${WRAPPER_CONF_FILE} | grep wrapper\.java\.classpath | wc -l)
KARAF_MY_ENTRY="wrapper.java.classpath.$((NUM_CLASSPATH_ENTRIES+1))=%KARAF_BASE%/lib/my*.jar"
sed -i "/wrapper\.java\.classpath\.$NUM_CLASSPATH_ENTRIES.*/a $KARAF_MY_ENTRY" ${WRAPPER_CONF_FILE}
答案 0 :(得分:1)
您可以使用此剧本来计算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]#
我们正在寻找以wrapper.java.classpath
开头的行,我们计算它们,然后在动态递增" index"之后追加(insertafter
)2行。编号为接下来的2个值。
请查看blockinfile
模块文档,了解如何根据您的需要配置# BEGIN ANSIBLE MANAGED BLOCK
行。