2

I'm writing my first ansible playbook right now. That's what I have so far.

---

- 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

The output of debug is:

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

How can I get the value

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

to keep working with it?

For example like this:

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

UPDATE This is my current playbook

---

- 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

The Output:

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

2 Answers 2

1

Because copied_file.results is an array, so to get dest of single file you should use something like this:

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

Or you can write a loop like this:

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

You should consider using a fact instead of a variable.

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

You can then access the fact using

"{{ copy_destination }}"

For more information, see https://docs.ansible.com/ansible/devel/modules/set_fact_module.html

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.