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

答案2

您应该考虑使用事实而不是变量。

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

然后您可以使用以下方式访问事实

"{{ copy_destination }}"

有关更多信息,请参阅https://docs.ansible.com/ansible/devel/modules/set_fact_module.html

相关内容