从 Ansible 中的字符串中删除最后 n 个字符
Remove last n charecters from a string in Ansible
下面是我构造字符串的剧本。
- 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 }}"
输出:
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"
}
}
循环完成后,我得到了所需的字符串,除了我在末尾留下了尾随 &&
,即 "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 个字符。
你能推荐一下吗?
- hosts: localhost
vars:
foo:
- {"name": "SCRIPT", "path": "/tmp/scripts"}
- {"name": "MON", "path": "/tmp/MON"}
tasks:
- debug:
var: foo
- set_fact:
cmds: "{{ [ 'ls -lrt ' + item.path + ' | tail -57' ] + cmds | default([]) }}"
loop: "{{ foo }}"
- set_fact:
allcmd: "{{ cmds | join(' && ')}}"
- debug:
var: allcmd
输出:
ok: [localhost] => {
"allcmd": "ls -lrt /tmp/MON | tail -57 && ls -lrt /tmp/scripts | tail -57"
}
link
中概述了一种更简单的方法
http://www.freekb.net/Article?id=2884
debug:
msg: "{{ 'Hello World'[:-3] }}"
下面是我构造字符串的剧本。
- 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 }}"
输出:
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"
}
}
循环完成后,我得到了所需的字符串,除了我在末尾留下了尾随 &&
,即 "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 个字符。
你能推荐一下吗?
- hosts: localhost
vars:
foo:
- {"name": "SCRIPT", "path": "/tmp/scripts"}
- {"name": "MON", "path": "/tmp/MON"}
tasks:
- debug:
var: foo
- set_fact:
cmds: "{{ [ 'ls -lrt ' + item.path + ' | tail -57' ] + cmds | default([]) }}"
loop: "{{ foo }}"
- set_fact:
allcmd: "{{ cmds | join(' && ')}}"
- debug:
var: allcmd
输出:
ok: [localhost] => {
"allcmd": "ls -lrt /tmp/MON | tail -57 && ls -lrt /tmp/scripts | tail -57"
}
link
中概述了一种更简单的方法http://www.freekb.net/Article?id=2884
debug:
msg: "{{ 'Hello World'[:-3] }}"