我正在尝试使用 Ansible 在虚拟机上安装 docker 凭证存储
这是 playbook.yaml 中凭证存储代码的一部分
但是,问题是我尝试将 gpg 密钥 id 导出为 play.4 上的变量,但无法在 play.6 上回显该变量
我尝试使用寄存器,但寄存器变量似乎无法在其他游戏的 shell 命令中使用
- name: 1. Install gpg and pass
apt:
update_cache: yes
name:
- gpg
- pass
- name: 2. Create GPG key
shell: |
cat > /root/gpgKey <<EOF
%echo Generating a default key
Key-Type: default
Subkey-Type: default
Name-Real: abc999
Name-Comment: abc999
Name-Email: [email protected]
Expire-Date: 0
Passphrase: abc999
%commit
%echo done
EOF
- name: 3. Generate keys with `/root/gpgKey` file
shell: |
sudo gpg --batch --generate-key /root/gpgKey
- name: 4. Verify key generation
shell: |
var=$(sudo gpg --list-secret-keys --keyid-format=long | sed '4!d' | tr -d " ")
- name: 5. Download docker-credential-pass
shell: |
export PASS_VERSION="v0.6.0"
wget -q "https://github.com/docker/docker-credential-helpers/releases/download/${PASS_VERSION}/docker-credential-pass-${PASS_VERSION}-amd64.tar.gz" -O - | sudo tar -x -C /usr/bin
sudo chmod 710 "/usr/bin/docker-credential-pass"
- name: 6. Echo GPG id
shell: |
echo $var
ansible-playbook playbook.yaml -vvv
这是play.6命令的输出echo $var
:标准输出什么也没有。
changed: [localhost] => {
"changed": true,
"cmd": "echo $var\n",
"delta": "0:00:00.002488",
"end": "2023-04-27 09:36:46.483048",
"invocation": {
"module_args": {
"_raw_params": "echo $var\n",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"rc": 0,
"start": "2023-04-27 09:36:46.480560",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
答案1
关于评论“您需要register
命令的输出以便 Ansible 存储它。“ 和 ”我register
之前尝试过,但找不到register
在其他play的shell中使用该变量的方法。“您可能已经查看了以下最小示例剧本
---
- hosts: localhost
become: true
gather_facts: false
tasks:
- name: 4. Verify key generation
shell:
cmd: "gpg --list-secret-keys --keyid-format=long | sed '4!d' | tr -d ' '"
register: VAR
- name: 6. Echo GPG ID
shell:
cmd: "echo {{ VAR.stdout_lines }}"
register: result
- name: Show result
debug:
var: result
或者更通用的
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Echo example 1
shell:
cmd: "echo 12:34:56:78:90:AB:CD:EF"
register: VAR
- name: Echo example 2
shell:
cmd: "echo {{ VAR.stdout_lines }}"
register: result
- name: Show registered variable
debug:
var: result
- name: Show result content only
debug:
msg: "{{ result }}"
因为它向您展示了如何熟悉注册、返回值和数据结构。
进一步的文档