-1

this is the content of my playbook:

- name: Download checksum
  get_url:
    url: "{{ item.value.url }}.sha1"
    dest: "/VAR/{{ ansible_user }}/tmp/{{ item.value.dest }}.sha1"
    mode: 0640
    timeout: 300
    force: yes
  with_dict: "{{ packages.list }}"

- name: slurp checksum
  slurp:
    src: "/VAR/{{ ansible_user }}/tmp/{{  item.value.dest }}.sha1"
  register:  {{  item.value.dest }}
  with_dict: "{{ packages.list }}"

- name: debug
  debug:
    msg: "sha1:{{ ['item']['value']['dest']['content'] | b64decode }}"
  with_dict: "{{ packages.list }}"

in my variables i have :

packages:
  list:
    package_name1:
      url: http://domaine.com/package_name_src1
      dest: package_name_dest1
    package_name2:
      url: http://domaine.com/package_name_src2
      dest: package_name_dest2
      .....
      .....
      and so on

What i am trying to do is download the checksum which is located in http://package_url.sha1 and put it into a variable then print it with debug (msg). the final purpose is to use the checksome variable in get_url to check the checksum after the download. and the main problem if i want to simpliy things is i must acheive something like this

  debug:
   msg: {{ {{ item.value.dest}}.content }}

Which i know is wrong in ansible syntax maybe you can suggest to me a work around

Currently i get every time the following error:

    fatal: [server]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'value'\n\nThe error appears to have been in 'test.yml': line 54, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: debug\n      ^ here\n"
}
Midokate
  • 95
  • 3
  • 11

1 Answers1

0

i would also try to simplify things, and here are some changes i propose:

  1. in the slurp task, register one variable, say slurp_var. Ansible in each loop will not overwrite your variable each loop will add the results in the list array of slurp_var.results(example follows).
  2. to print - or process - your results, use a with_items loop.

here is a modified PB to demonstrate the result:

---
- hosts: localhost
  gather_facts: false
  vars:
    packages:
      list:
        package_name1:
          url: http://domaine.com/package_name_src1
          dest: file1
        package_name2:
          url: http://domaine.com/package_name_src2
          dest: file2


  tasks:
    - name: slurp checksum
      slurp:
        src: "/tmp/{{ item.value.dest }}.sha1"
      register:  slurp_var
      with_dict: "{{ packages.list }}"

    - name: debug
      debug:
        msg: "sha1: {{ item.content | b64decode }}"
      with_items: "{{ slurp_var.results }}"

demo run:

[http_offline@greenhat-29 tests]$ cat /tmp/file1.sha1        
c1b016a6506775492a1968a1fa703781c6d066c8
[http_offline@greenhat-29 tests]$ cat /tmp/file2.sha1        
6ab7a14cf0e04c09f1a2f0f572e7d67e35fa37bd
[http_offline@greenhat-29 tests]$ ansible-playbook test.yml  

PLAY [localhost] *******************************************************************************************************************************************************************************************************

TASK [slurp checksum] **************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'package_name1', 'value': {'url': 'http://domaine.com/package_name_src1', 'dest': 'file1'}})
ok: [localhost] => (item={'key': 'package_name2', 'value': {'url': 'http://domaine.com/package_name_src2', 'dest': 'file2'}})

TASK [debug] ***********************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'content': 'YzFiMDE2YTY1MDY3NzU0OTJhMTk2OGExZmE3MDM3ODFjNmQwNjZjOAo=', 'source': '/tmp/file1.sha1', 'encoding': 'base64', 'invocation': {'module_args': {'src': '/tmp/file1.sha1'}}, '_ansible_parsed': True, '_ansible_no_log': False, 'failed': False, 'changed': False, 'item': {'key': 'package_name1', 'value': {'url': 'http://domaine.com/package_name_src1', 'dest': 'file1'}}, '_ansible_item_result': True, '_ansible_ignore_errors': None, '_ansible_item_label': {'key': 'package_name1', 'value': {'url': 'http://domaine.com/package_name_src1', 'dest': 'file1'}}}) => {
    "msg": "sha1: c1b016a6506775492a1968a1fa703781c6d066c8\n"
}
ok: [localhost] => (item={'content': 'NmFiN2ExNGNmMGUwNGMwOWYxYTJmMGY1NzJlN2Q2N2UzNWZhMzdiZAo=', 'source': '/tmp/file2.sha1', 'encoding': 'base64', 'invocation': {'module_args': {'src': '/tmp/file2.sha1'}}, '_ansible_parsed': True, '_ansible_no_log': False, 'failed': False, 'changed': False, 'item': {'key': 'package_name2', 'value': {'url': 'http://domaine.com/package_name_src2', 'dest': 'file2'}}, '_ansible_item_result': True, '_ansible_ignore_errors': None, '_ansible_item_label': {'key': 'package_name2', 'value': {'url': 'http://domaine.com/package_name_src2', 'dest': 'file2'}}}) => {
    "msg": "sha1: 6ab7a14cf0e04c09f1a2f0f572e7d67e35fa37bd\n"
}

PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0   

[http_offline@greenhat-29 tests]$ 

One thing that needs your attention, is to inspect the slurp_var when a file you try to "slurp" is missing, then add some when clauses in your subsequent tasks..

hope it helps!

ilias-sp
  • 6,135
  • 4
  • 28
  • 41