Ansible 플레이북: 변수에서 값 가져오기

Ansible 플레이북: 변수에서 값 가져오기

저는 현재 첫 번째 Ansible 플레이북을 작성 중입니다. 이것이 내가 지금까지 가지고 있는 것입니다.

---

- hosts: kolumbus   become: yes

  tasks:
  - name: Copy RPM
    copy:
      src: "{{ item }}"
      dest: /tmp
    with_fileglob:
      - /opt/omd/versions/default/share/check_mk/agents/check-mk-agent-*.noarch.rpm
    register: copied_file

  - debug: var=copied_file

디버그 출력은 다음과 같습니다.

TASK [debug] **********************************************************************************************************************************************************************************
ok: [kolumbus] => {
    "copied_file": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": false,
                "checksum": "55ae455adc639f9cfca738683b5955f32e78d2db",
                "dest": "/tmp/check-mk-agent-1.5.0p5-1.noarch.rpm",
                "diff": {
                    "after": {
                        "path": "/tmp/check-mk-agent-1.5.0p5-1.noarch.rpm"
                    },
                    "before": {
                        "path": "/tmp/check-mk-agent-1.5.0p5-1.noarch.rpm"
                    }
                },
                "failed": false,
                "gid": 0,
                "group": "root",
                "invocation": {
                    "module_args": {
                        "attributes": null,
                        "backup": null,
                        "content": null,
                        "delimiter": null,
                        "dest": "/tmp",
                        "diff_peek": null,
                        "directory_mode": null,
                        "follow": false,
                        "force": false,
                        "group": null,
                        "mode": null,
                        "original_basename": "check-mk-agent-1.5.0p5-1.noarch.rpm",
                        "owner": null,
                        "path": "/tmp/check-mk-agent-1.5.0p5-1.noarch.rpm",
                        "recurse": false,
                        "regexp": null,
                        "remote_src": null,
                        "selevel": null,
                        "serole": null,
                        "setype": null,
                        "seuser": null,
                        "src": "check-mk-agent-1.5.0p5-1.noarch.rpm",
                        "state": "file",
                        "unsafe_writes": null,
                        "validate": null
                    }
                },
                "item": "/opt/omd/versions/default/share/check_mk/agents/check-mk-agent-1.5.0p5-1.noarch.rpm",
                "mode": "0644",
                "owner": "root",
                "path": "/tmp/check-mk-agent-1.5.0p5-1.noarch.rpm",
                "secontext": "unconfined_u:object_r:user_home_t:s0",
                "size": 32768,
                "state": "file",
                "uid": 0
            }
        ]
    }
}

값을 어떻게 얻을 수 있나요?

"dest": "/tmp/check-mk-agent-1.5.0p5-1.noarch.rpm",

계속 사용하시겠습니까?

예를 들어:

  - name: Install check_mk
    yum:
      name: "{{ copied_file.results.dest }}"
      state: present

업데이트 이것이 내 현재 스크립트입니다.

---

- hosts: kolumbus
  become: yes

  tasks:
  - name: Copy RPM
    copy:
      src: "{{ item }}"
      dest: /tmp
    with_fileglob:
      - /opt/omd/versions/default/share/check_mk/agents/check-mk-agent-*.noarch.rpm
    register: copied_file
  - set_fact: copy_destination={{ copied_file.results.dest }}

  - name: Install check_mk
    yum:
      name: "{{ copy_destination }}"
      state: present

산출:

PLAY [kolumbus] *******************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [kolumbus]

TASK [Copy RPM] *******************************************************************************************************************************************************************************
ok: [kolumbus] => (item=/opt/omd/versions/default/share/check_mk/agents/check-mk-agent-1.5.0p5-1.noarch.rpm)

TASK [set_fact] *******************************************************************************************************************************************************************************
fatal: [kolumbus]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'dest'\n\nThe error appears to have been in '/home/support/ansible/playbooks/update-cmk-linux.yml': line 14, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    register: copied_file\n  - set_fact: copy_destination={{ copied_file.results.dest }}\n    ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes.  Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}
    to retry, use: --limit @/home/support/ansible/playbooks/update-cmk-linux.retry

PLAY RECAP ************************************************************************************************************************************************************************************
kolumbus                   : ok=2    changed=0    unreachable=0    failed=1

답변1

copied_file.results배열이기 때문에 단일 파일의 대상을 얻으려면 다음과 같이 사용해야 합니다.

- set_fact: copy_destination={{ copied_file.results[0].dest }}

아니면 다음과 같이 루프를 작성할 수도 있습니다.

 - set_fact:
    copy_dest = "{{ copy_dest | default([]) | union([item.dest]) }}"
  with_items:
    - "{{ copied_file.results }}"

답변2

변수 대신 사실을 사용하는 것을 고려해야 합니다.

- set fact:
  copy_destination: "{{ copied_file.results.dest }}"

그런 다음 다음을 사용하여 사실에 액세스할 수 있습니다.

"{{ copy_destination }}"

자세한 내용은 다음을 참조하세요.https://docs.ansible.com/ansible/devel/modules/set_fact_module.html

관련 정보