Ansible Playbook:変数から値を取得する

Ansible Playbook:変数から値を取得する

私は現在最初の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 }}"

おすすめ記事