下面是我的数据的一个人为示例。当列表中的任何字典mylst.env具有带有名称的键时,我想提取所有客户的姓名n1
我想这可以通过嵌套循环来完成,但无法弄清楚如何实现。
- hosts: localhost
gather_facts: no
vars:
- mylst:
- apiVersion: v1
name: customer1
metaData:
cycles: 10
ships: 12
env:
- name: n1
value: v1
- name: n2
value: v2
- apiVersion: v1
name: customer2
metaData:
cycles: 10
ships: 12
env:
- name: n1
value: v1
- name: n3
value: v3
- apiVersion: v1
name: customer3
metaData:
cycles: 10
ships: 12
env:
- name: n3
value: v1
- name: n4
value: v4
tasks:
- set_fact:
cust_lst: …Run Code Online (Sandbox Code Playgroud) 也许我遗漏了一些关于 go 的非常基本的东西regexp.FindStringSubmatch()。我想捕获字符串后面所有数字的组"Serial Number: ",但得到意外的输出。我的代码如下:
package main
import "fmt"
import "regexp"
func main() {
x := "Serial Number: 12334"
r := regexp.MustCompile(`(\d+)`)
res := r.FindStringSubmatch(x)
for i,val := range res {
fmt.Printf("entry %d:%s\n", i,val)
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
entry 0:12334
entry 1:12334
Run Code Online (Sandbox Code Playgroud)
我更熟悉 python 的分组,它看起来非常简单:
>>> re.search('(\d+)', "Serial Number: 12344").groups()[0]
'12344'
Run Code Online (Sandbox Code Playgroud)
我怎样才能让分组在 go 中工作?谢谢
在python中,split()没有分隔符的函数会拆分空白/制表符等,并返回一个删除了所有空格的列表
>>> "string with multiple spaces".split()
['string', 'with', 'multiple', 'spaces']
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现类似的东西go呢?
package main
import "fmt"
import "strings"
func main() {
s := "string with multiple spaces"
lst := strings.Split(s, " ")
for k, v := range lst {
fmt.Println(k,v)
}
}
Run Code Online (Sandbox Code Playgroud)
以上给出
0 string
1
2
3
4 with
5 multiple
6
7
8
9
10
11 spaces
Run Code Online (Sandbox Code Playgroud)
我想每个字符串保存在lst[0],lst[1],lst[2]和lst[3].可以这样做吗?谢谢
有人可以解释为什么sys.stdout.write()附加11到我的字符串?
$ python3
Python 3.4.3+ (default, Jul 28 2015, 13:17:50)
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 'hello'
>>> y = 'world'
>>> msg = ''
>>> import sys
>>> msg += x
>>> msg += '\n' + y
>>> msg
'hello\nworld'
>>> sys.stdout.write(msg)
hello
world11
>>> print(msg)
hello
world
Run Code Online (Sandbox Code Playgroud) 我编写了一个简单的ansible脚本来创建一个临时目录,并希望将该目录的名称保存到一个变量中。我的.yml文件是:
- hosts: " {{ lookup('env', 'HOSTNAME') }} "
tasks:
- name : Create staging directory
tempfile:
state: directory
suffix: staging
path: "{{ lookup('env', 'HOME') }}"
become: true
register: output
- name: print stdout
debug: msg="{{ output }}"
Run Code Online (Sandbox Code Playgroud)
运行上面的输出会打印一个dict
$ ansible-playbook -i hosts tempfile.yml
PLAY [localhost] ******************************************************************************************
TASK [Gathering Facts] ************************************************************************************
ok: [localhost]
TASK [Create staging directory] ***************************************************************************
changed: [localhost]
TASK [print stdout] ***************************************************************************************
ok: [localhost] => {
"msg": {
"changed": true,
"gid": 0,
"group": "root",
"mode": …Run Code Online (Sandbox Code Playgroud) 我想files通过预先添加/auto和附加_sw_table到每个元素来生成一个新列表。虽然我可以通过明确地将新字符串作为 args 提到regex_replacein来做到这一点map,但我想使用变量来实现相同的目标。以下工作:
- hosts: localhost
gather_facts: no
tasks:
- set_fact:
my_lst: []
top_dir: /auto
extra: sw_table
files: ['/etc/passwd', '/etc/group']
- set_fact:
my_lst: "{{ files | map('regex_replace', '(.*)', '/auto\\1_sw_table') | list }}"
- debug: var=my_lst
Run Code Online (Sandbox Code Playgroud)
输出:
$ ansible-playbook -i localhost, ./test.yaml
PLAY [localhost] **************************************************************************************************
TASK [set_fact] ***************************************************************************************************
ok: [localhost]
TASK [set_fact] ***************************************************************************************************
ok: [localhost]
TASK [debug] ******************************************************************************************************
ok: [localhost] => {
"my_lst": [
"/auto/etc/passwd_sw_table",
"/auto/etc/group_sw_table"
]
}
Run Code Online (Sandbox Code Playgroud)
如何使用变量top_dir并 …