Ash*_*har 1 formatting trim filter ansible ansible-facts
下面是我构建字符串的剧本。
- name: Construct command for all paths on a particular IP
set_fact:
allcmd: "{{ allcmd | default('') + '\"ls -lrt ' + item.path + ' | tail -57 &&' }}"
loop: "{{ user1[inventory_hostname] }}"
- debug:
msg: "allcmd is:{{ allcmd }}"
Run Code Online (Sandbox Code Playgroud)
输出:
ok: [10.9.9.11] => (item={u'path': u'/tmp/scripts', u'name': u'SCRIPT'}) => {
"ansible_facts": {
"allcmd": "ls -lrt /tmp/scripts | tail -57 &&"
},
"ansible_loop_var": "item",
"changed": false,
"item": {
"name": "SCRIPT",
"path": "/tmp/scripts"
}
}
ok: [10.9.9.11] => (item={u'path': u'/tmp/MON', u'name': u'MON'}) => {
"ansible_facts": {
"allcmd": " ls -lrt /tmp/scripts | tail -57 && ls -lrt /tmp/MON | tail -57 &&"
},
"ansible_loop_var": "item",
"changed": false,
"item": {
"name": "MON",
"path": "/tmp/MON"
}
}
Run Code Online (Sandbox Code Playgroud)
&&循环完成后,我得到了所需的字符串,除了最后留下了尾随的事实ie "allcmd": " ls -lrt /tmp/scripts | tail -57 && ls -lrt /tmp/MON | tail -57 &&"。
我希望&&从 allcmd 变量中删除最后 3 个字符。期望的输出:
"allcmd": " ls -lrt /tmp/scripts | tail -57 && ls -lrt /tmp/MON | tail -57"
找不到任何过滤器或功能来删除 ansible 中的最后 n 个字符。
你能建议一下吗?
小智 6
there is a much easier way outlined in this link
http://www.freekb.net/Article?id=2884
debug:
msg: "{{ 'Hello World'[:-3] }}"
Run Code Online (Sandbox Code Playgroud)