1

In my playbook, I am trying to get list of sub-directory names using the find module and then extracting the basename from path. I have been able to get the list but the elements are prepended with u'. How can I remove those from the output?

Ansible version 2.9

I tried to look at these SO posts here and here, but couldn't get it to work.
I may not have fully understood how they should be applied

This is part of my playbook:

- name: set item.path | basename
  set_fact: dir_name_list2_basename="{{ item.path | basename}}"
  with_items: "{{ zookeeper_data_dir.files}}"
  register: item_path_basename_list

- debug: 
    msg: "{{item_path_basename_list.results}}"

- name: debug item.path | basename as list 
  debug: 
    var: item.ansible_facts.dir_name_list2_basename
  with_items: "{{item_path_basename_list.results}}"


- debug: msg="item_path_basename_list.results {{ item_path_basename_list.results | map(attribute='ansible_facts.dir_name_list2_basename') | list }}"

- name: set fact to array 
  set_fact: basename_array="{{ item_path_basename_list.results | map(attribute='ansible_facts.dir_name_list2_basename') | list }}"

- debug: 
    msg: "basename_array &&&&&&&& {{basename_array}}"

And this is the output of the last debug:

ok: [zk3-dev] => {
    "msg": "basename_array &&&&&&&& [u'version-2_backup', u'version-2']"
}
ok: [zk2-dev] => {
    "msg": "basename_array &&&&&&&& [u'version-2_backup', u'version-2']"
}
ok: [zk1-dev] => {
    "msg": "basename_array &&&&&&&& [u'version-2_backup', u'version-2']"
}

I would like the basename_array to show up as ["version-2_backup", "version-2"] without the u prefix

How should I change my set fact to array task, so I will get the desired result?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
adbdkb
  • 1,897
  • 6
  • 37
  • 66

1 Answers1

2

Since ["version-2_backup", "version-2"] is actually a JSON array, you could use the to_json filter.

This said, your long set of tasks looks like an overcomplicated process for a requirement that can be achieved with the right set of map filters, since map can apply the same filter to all the elements of a list, you can easily fit your basename in it.

So, given:

- debug:
    msg: >-
      basename_array &&&&&&&&
      {{
        zookeeper_data_dir.files
          | map(attribute='path')
          | map('basename')
          | to_json
      }}

This yields:

ok: [localhost] => {
    "msg": "basename_array &&&&&&&& [\"version-2_backup\", \"version-2\"]"
}

Note that the double quotes are escaped because you are using the JSON stdout callback. But, if you change the callback to YAML, this would yield exactly what you expected:

ok: [localhost] => 
  msg: basename_array &&&&&&&& ["version-2_backup", "version-2"]
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Thanks. But when I ran it, I get the this error `FAILED! => {"msg": "Unexpected templating type error occurred on (basename_array {{\n zookeeper_data_dir.files\n | map(attribute='path')\n | map('basename')\n | to_json\n}}): is not JSON serializable"}` - My ansible version is 2.9.7. Is that causing me to get error for the same code that is working for you? – adbdkb Feb 09 '23 at 19:07
  • Yes, add a `| list` between the last map at the to_json. Or better, upgrade to a supported version of Ansible. – β.εηοιτ.βε Feb 09 '23 at 19:42
  • We can upgrade only to the latest of 2.9 ( 2.9.27 to be precise ), cannot go to higher version of ansible - 2.10 or higher. I have already made a request to our unix admins team. Will this work on 2.9.27 ? Also, I am assuming - if the construct works on 2.9.27 - that instead of `debug: msg:`, I could replace that with `set_fact: dir_list:` and then use the same construct. Is that correct ? – adbdkb Feb 09 '23 at 20:31
  • 1
    `cannot go to higher version of ansible - 2.10 or higher` <= what is the justification for that? Even an archaic and totally out of date OS such as centos7 has python 3.6 available in its default repos and will let you `pip3 install ansible` in a non deprecated version either globally, as a user or in a virtualenv. Note that this is the only officially supported installation method nowadays. – Zeitounator Feb 09 '23 at 20:56
  • I agree, but unfortunately, I don't have control over this. Will check with security office, if they would allow the unix admins to update to a higher version. Thanks for the reply – adbdkb Feb 09 '23 at 21:20