我正在使用模块openssl_pkcs12,可以从* .pem文件中提取* .crt(证书),但是我不知道该如何提取* .key(KEY)。
代码示例(从文件创建和提取证书):
library("RODBC")
# Stores data types of columns for table resp
ColDataTypes <- list(RESP_CODE = "VARCHAR(8)", koef = "DOUBLE", outl = "VARCHAR(1)")
# Opens connection to file
con <- odbcConnectAccess2007(paste0(PathReport,"1apgr_", NYear, "m", CMonth,"_svari.accdb"))
# Saves resp table
sqlSave(con, DataDelivery,
tablename = "resp",
safer = FALSE, varTypes = ColDataTypes, rownames = FALSE)
然后,如果仅对这两个文件进行比较,我可以看到CERTIFICATE完全匹配。 key.pem文件:
- name: Generate PKCS#12 file
local_action:
module: openssl_pkcs12
action: export
path: /tmp/pkcs/ansible.p12
friendly_name: raclette
privatekey_path: /tmp/pkcs/key.pem
certificate_path: /tmp/pkcs/cert.pem
state: present
- name: Dump/Parse PKCS#12 file
local_action:
module: openssl_pkcs12
action: parse
src: /tmp/pkcs/ansible.p12
path: /tmp/pkcs/ansible.pem
state: present
cert.pem文件:
$ cat key.pem
-----BEGIN PRIVATE KEY-----
MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQCu8twQ9txatJWx
k1cJzQCz38JEZ0Rp/Bt3tOcw+OeSAwL8Ge2vqM6eHNAqCNzCfHI6QxrbYkxd1n+d
pfYQHHdDVAbqeFRdEd7vsyGbAwzmwiYiIZxwRfmpMLltGIOScB5CiJazMIETJ4F5
/8m2UoPXfb2jVxdg/wbZxotIbeUmaaCCpwa/4VF8oeR+Z7KRZzaCFWzIWKZOjLew
j285NSVe+pSUZbbs/YrgoEojG+I/DNBvdIp05Ik/qFnN3ddpgCsds5gWz/vt6OOx
o4luD0maMPJImGTecanaW0qHqE/FzzAPBGQDTVxQeCEYEGQEH0vXywTBo1Mm5SFG
cu4gbQF4XbZzv2qgZy5UO/rdR0zW+Sa6vcrj6YbOPZsN4TtssbUuxkpPQLnoq1pa
KfWDPKUAMmd89X7a6xYpp0Ia383WZ1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mImqRV/s+/y68B+a3ZfW3PjOa
SxflBSgq79kBWYDn+35XvlQwq+Nm0M6Qa21SzOKnxyKTepfyTpbJDpsKO6hdETCn
pHE84cTvoBPspfyRX1VZFKTfTOkNEhd0WSx+E1HEHTefnts6z3vodXNXZa5VRUHf
C74p7fdeRplA9jBTcMUFVubiAmqPR2o+g4M+wmWosUekUjWDxtD4RiSHsGVLY9he
6ltWpJin/bBUKTuy2NyBzsg96liB/8pQ6U5XDA+KmneAX/umd6JtUqNY9zBE/lWS
20nkAGghP+wqKrT0BCXBygoOgYm8NQ3TapqxeFcyRGSa3TQFIERAHqHvJVpoPl9P
L8JiXjoPThU+gpCEVswCiWX+dlh7yrec0MRUSHP/upZqG4s0OjrTbmuBOhccYQw=
-----END CERTIFICATE-----
可以在此处找到更多信息(How to get .pem file from .key and .crt files?)。
目前,我正在使用以下shell模块示例:
cat cert.pem key.pem > complete.pem
我在线搜索了其他模块,例如(openssl_certificate,openssl_csr和openssl_privatekey)。还有其他模块可以提取用Ansible编写的key.pem吗?
答案 0 :(得分:1)
非常基本的解决方案,但是我们开始...
从上一个任务中,您将获得一个包含私钥和证书的文件/tmp/ansible.pem
。基本上:
-----BEGIN PRIVATE KEY-----
[Key content here]
[...]
-----END PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
[Cert content here]
[...]
-----END CERTIFICATE-----
以下任务将读取文件并提取每个元素。
注释:
- name: Get the key part
debug:
msg: >-
{{
lookup('file', '/tmp/ansible.pem') |
regex_replace("[\s\S.]*(-----BEGIN PRIVATE KEY-----[\s\S.]*-----END PRIVATE KEY-----)[\s\S.]*", "\1")
}}
- name: Get the cert part
debug:
msg: >-
{{
lookup('file', '/tmp/ansible.pem') |
regex_replace("[\s\S.]*(-----BEGIN CERTIFICATE-----[\s\S.]*-----END CERTIFICATE-----)[\s\S.]*", "\1")
}}