Ansible Playbook: Obtenha valor da variável

1

Estou escrevendo meu primeiro manual ansioso agora. Isso é o que eu tenho até agora.

---

- 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

A saída da depuração é:

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

Como posso obter o valor

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

para continuar trabalhando com ele?

Por exemplo, assim:

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

ATUALIZAÇÃO Este é meu manual atual

---

- 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

A saída:

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
    
por Patrick 25.10.2018 / 07:18

2 respostas

1

Porque copied_file.results é uma matriz, então, para obter um arquivo único, você deve usar algo assim:

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

Ou você pode escrever um loop como este:

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

Você deve considerar o uso de um fato em vez de uma variável.

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

Você pode acessar o fato usando

"{{ copy_destination }}"

Para mais informações, consulte o link

    
por 25.10.2018 / 09:48