Ansible 플레이북에서 명령의 stdout을 변수로 추출하는 방법

Ansible 플레이북에서 명령의 stdout을 변수로 추출하는 방법

Ansible을 사용하여 가상 머신에 Docker 자격 증명 저장소를 설치하려고 합니다.

이것은 playbook.yaml의 자격 증명 저장 코드의 일부입니다.

그러나 문제는 gpg 키 ID를 play.4에서 변수로 내보내려고 하는데 play.6에서 해당 변수를 에코할 수 없다는 것입니다.

레지스터를 사용해 보았지만 레지스터 변수가 다른 게임의 셸 명령에서 작동하지 않는 것 같습니다.

 - 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

댓글에 대해"registerAnsible이 이를 저장할 수 있도록 명령의 출력이 필요합니다 ." 그리고"나는 register이전에 이것을 시도했지만 register다른 플레이의 쉘에서 변수를 사용하는 방법을 찾을 수 없었습니다."다음과 같은 최소 예제 스크립트를 본 적이 있을 것입니다.

---
- 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 }}"

등록, 반환 값, 데이터 구조에 익숙해지는 방법을 보여주기 때문입니다.

추가 문서

관련 정보