The ansible documentation clearly states that fetch, fetches a file, not a list of files. Although one can program an application to deal with both a scalar and a sequence loaded from a YAML document, that is not automatic and would almost certainly have been reflected in the documentation.

Since you already have a sequence at a higher level, just extend that.

- name: Register env Type
  shell: facter configured_setup
  register: setup

- name: transparency tasks
  shell: {{some_script}} -t -a {{hosts}} -i {{inventory_hostname}}
  register: test
  when: setup.stdout == "something"

- name: fetch group_vars
  fetch:
    src: { "{{ item }}", when: setup.stdout == "something" }
    dest: "{{group_vars}}"
    flat: yes
  with_items:
    - "{{ test.stdout_lines[0] }}"
    - "{{ test.stdout_lines[1] }}"

- name: fetch group_vars2
  fetch:
    src: { filename, when setup.stdout =="something else" }
    dest: "{{group_vars}}"
    flat: yes
  with_items:
    - "{{ test.stdout_lines[0] }}"
    - "{{ test.stdout_lines[1] }}"

You might be able to reduce the repetitiveness somewhat by using YAML's anchor and merge:

- name: Register env Type
  shell: facter configured_setup
  register: setup

- name: transparency tasks
  shell: {{some_script}} -t -a {{hosts}} -i {{inventory_hostname}}
  register: test
  when: setup.stdout == "something"

- &fetchtask
  name: fetch group_vars
  fetch: &fetchsrc
    src: { "{{ item }}", when: setup.stdout == "something" }
    dest: "{{group_vars}}"
    flat: yes
  with_items:
    - "{{ test.stdout_lines[0] }}"
    - "{{ test.stdout_lines[1] }}"

- <<: *fetchtask
  name: fetch group_vars2
  fetch:
    <<: *fetchsrc
    src: { filename, when setup.stdout =="something else" }

Ansible probably expands the {{...}} before handing the document to the YAML parser, otherwise the value for shell in the "transparency task" would throw an error. But you should probably still quote that like you do with the value for dest

Answer from Anthon on Stack Overflow
Top answer
1 of 2
2

The ansible documentation clearly states that fetch, fetches a file, not a list of files. Although one can program an application to deal with both a scalar and a sequence loaded from a YAML document, that is not automatic and would almost certainly have been reflected in the documentation.

Since you already have a sequence at a higher level, just extend that.

- name: Register env Type
  shell: facter configured_setup
  register: setup

- name: transparency tasks
  shell: {{some_script}} -t -a {{hosts}} -i {{inventory_hostname}}
  register: test
  when: setup.stdout == "something"

- name: fetch group_vars
  fetch:
    src: { "{{ item }}", when: setup.stdout == "something" }
    dest: "{{group_vars}}"
    flat: yes
  with_items:
    - "{{ test.stdout_lines[0] }}"
    - "{{ test.stdout_lines[1] }}"

- name: fetch group_vars2
  fetch:
    src: { filename, when setup.stdout =="something else" }
    dest: "{{group_vars}}"
    flat: yes
  with_items:
    - "{{ test.stdout_lines[0] }}"
    - "{{ test.stdout_lines[1] }}"

You might be able to reduce the repetitiveness somewhat by using YAML's anchor and merge:

- name: Register env Type
  shell: facter configured_setup
  register: setup

- name: transparency tasks
  shell: {{some_script}} -t -a {{hosts}} -i {{inventory_hostname}}
  register: test
  when: setup.stdout == "something"

- &fetchtask
  name: fetch group_vars
  fetch: &fetchsrc
    src: { "{{ item }}", when: setup.stdout == "something" }
    dest: "{{group_vars}}"
    flat: yes
  with_items:
    - "{{ test.stdout_lines[0] }}"
    - "{{ test.stdout_lines[1] }}"

- <<: *fetchtask
  name: fetch group_vars2
  fetch:
    <<: *fetchsrc
    src: { filename, when setup.stdout =="something else" }

Ansible probably expands the {{...}} before handing the document to the YAML parser, otherwise the value for shell in the "transparency task" would throw an error. But you should probably still quote that like you do with the value for dest

2 of 2
1

So I ended up doing this (which is working):

  - name: fetch group_vars test
    fetch:
      src: "{{ item }}"
      dest: "{{group_vars}}"
      flat: yes
    with_items:
      - "{{ test.stdout_lines[0] }}"
      - "{{ test.stdout_lines[1] }}"
    when: setup.stdout == "something" and {{something_else}} == True

I also noticed that there might be a bug in ansible related to registers. while using the "when" statement, even if the condition is not met, the register statement takes affect:

  - name: test tasks something enabled
    shell: /tmp/{{populate_script}} -u -a {{hosts}} -r
    register: variable
    when: setup.stdout == "test" and something == True

  - name: test tasks something disabled
    shell: /tmp/{{populate_script}} -u -a {{hosts}}
    register: variable
    when: setup.stdout == "test" and something == False

only one of these conditions will be met, in case the first one is met the second condition will override "variable"

🌐
GitHub
github.com › ansible › ansible › issues › 14369
2.0.0.2 Loop Error AttributeError: 'list' object has no attribute 'startswith' · Issue #14369 · ansible/ansible
February 8, 2016 - This code worked correctly with 1.7.2+dfsg-1~ubuntu14.04.1 ansible version. But after upgrade to 2.0.0.2-1ppa~trusty I got error: fatal: [test.machine.com]: FAILED! => {"failed": true, "stdout": ""} An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'list' object has no attribute 'startswith' Full strace: fatal: [test.machine.com]: FAILED!
Author   diarworld
Discussions

AttributeError: 'list' object has no attribute 'startswith'
TASK: [test | Try to install something] ... File "/usr/local/lib/python2.7/dist-packages/ansible/utils/__init__.py", line 172, in check_conditional if conditional.startswith("jinja2_compare"): AttributeError: 'list' object has no attribute 'startswith'... More on github.com
🌐 github.com
6
October 31, 2013
python - AttributeError: 'list' object has no attribute 'startswith' when Configuring interfaces using with_items (ansible) - Stack Overflow
I would like to use with_items to configure my interfaces in my switch but I have this error when I run my playbook. Can someone help me please ? Here is the output with -vvvv options: AttributeErr... More on stackoverflow.com
🌐 stackoverflow.com
'list' object has no attribute 'startswith'\r\n", "msg": "MODULE FAILURE"
ISSUE TYPE Bug Report ANSIBLE VERSION ansible 2.1.0.0 CONFIGURATION Using https://github.com/openstack-ansible-galaxy/vagrant-ansible-openstack OS / ENVIRONMENT Centos 7 SUMMARY & STEPS TO REPR... More on github.com
🌐 github.com
1
June 20, 2016
find module is unable to process lists received as paths parameter
ISSUE TYPE Bug Report COMPONENT NAME find module ANSIBLE VERSION ansible 2.4.3.0 CONFIGURATION OS / ENVIRONMENT N/A SUMMARY Despite the fact that documentation of file module states that paths para... More on github.com
🌐 github.com
7
February 9, 2018
🌐
Ansible
forum.ansible.com › archives › ansible project
dig lookup exception: 'list' object has no attribute 'startswith' - Ansible Project - Ansible
December 10, 2018 - Hey all- I’m having more problems w/ the dig lookup. If I statically define a list of hosts to lookup as a variable, it works. But if I return the list of hosts from another script, it fails. Here’s a simple example: ` $ cat test.py #!/usr/bin/env python import json hostList = hostList.append((‘www.google.com’)) j = {} j[‘hosts’] = hostList print json.dumps(j) $ cat test.yml
🌐
GitHub
github.com › ansible › ansible › issues › 4745
AttributeError: 'list' object has no attribute 'startswith' · Issue #4745 · ansible/ansible
October 31, 2013 - TASK: [test | Try to install something] ... File "/usr/local/lib/python2.7/dist-packages/ansible/utils/__init__.py", line 172, in check_conditional if conditional.startswith("jinja2_compare"): AttributeError: 'list' object has no attribute 'startswith'...
Author   jurgenhaas
🌐
Stack Overflow
stackoverflow.com › questions › 70018746 › attributeerror-list-object-has-no-attribute-startswith-when-configuring-int
python - AttributeError: 'list' object has no attribute 'startswith' when Configuring interfaces using with_items (ansible) - Stack Overflow
AttributeError: 'list' object has no attribute 'startswith' failed: [DTY] (item={u'int_name': u'GigabitEthernet1/0/46', u'vlan_id': 400, u'desc': u'Internet vers Routeur'}) => { "ansible_loop_var": "item", "changed": false, "item": { "desc": "Internet vers Routeur", "int_name": "GigabitEthernet1/0/1", "vlan_id": 400 }, "module_stderr": "Traceback (most recent call last):\n File \"/root/.ansible/tmp/ansible-local-26021FmRvka/ansible-tmp-1637232459.72-26034-173735680808565/AnsiballZ_ios_config.py\", line 102, in <module>\n _ansiballz_main()\n File \"/root/.ansible/tmp/ansible-local-26021FmRvka/a
🌐
GitHub
github.com › ansible › ansible › issues › 16369
'list' object has no attribute 'startswith'\r\n", "msg": "MODULE FAILURE" · Issue #16369 · ansible/ansible
June 20, 2016 - failed: [controller] (item={u'vhost': u'/', u'password': u'Uawaiyiz6tee7chietoC', u'user': u'openstack-glance'}) => {"failed": true, "item": {"password": "Uawaiyiz6tee7chietoC", "user": "openstack-glance", "vhost": "/"}, "module_stderr": "", "module_stdout": "Traceback (most recent call last):\r\n File \"/tmp/ansible_otGN8Y/ansible_module_rabbitmq_user.py\", line 302, in <module>\r\n main()\r\n File \"/tmp/ansible_otGN8Y/ansible_module_rabbitmq_user.py\", line 274, in main\r\n if rabbitmq_user.get():\r\n File \"/tmp/ansible_otGN8Y/ansible_module_rabbitmq_user.py\", line 155, in get\r\n users =
Author   oussemos
🌐
GitHub
github.com › ansible › ansible › issues › 35990
find module is unable to process lists received as paths parameter · Issue #35990 · ansible/ansible
February 9, 2018 - The error was: AttributeError: 'list' object has no attribute 'startswith' fatal: [localhost]: FAILED! => { "changed": false, "module_stderr": "Traceback (most recent call last):\n File \"/var/folders/br/99tfdvcs3vvfwdk69z7f0xmc0000gn/T/ansible_6kcUps/ansible_module_find.py\", line 366, in <module>\n main()\n File \"/var/folders/br/99tfdvcs3vvfwdk69z7f0xmc0000gn/T/ansible_6kcUps/ansible_module_find.py\", line 312, in main\n npath = os.path.expanduser(os.path.expandvars(npath))\n File \"/Users/ssbarnea/.pyenv/versions/2.7.14/lib/python2.7/posixpath.py\", line 254, in expanduser\n if not path.startswith('~'):\nAttributeError: 'list' object has no attribute 'startswith'\n", "module_stdout": "", "rc": 0 } MSG: MODULE FAILURE ·
Author   ssbarnea
Find elsewhere
🌐
CopyProgramming
copyprogramming.com › howto › ansible-attributeerror-list-object-has-no-attribute-startswith
Ansible: AttributeError in Ansible: 'startswith' attribute cannot be applied to a 'list' object
April 18, 2023 - While executing Ansible, I am attempting to accomplish a specific task. However, it is encountering an error and failing to complete. During task execution, an exception arose which resulted in failure. To view the complete traceback, -vvv can be used. The error that occurred was an AttributeError where list' object did not have attribute 'startswith ...
🌐
GitHub
github.com › DataDog › ansible-datadog-callback › issues › 23
Bug: v2_playbook_on_start - 'list' object has no attribute 'startswith' · Issue #23 · DataDog/ansible-datadog-callback
June 3, 2017 - 2017-06-03 09:15:16,728 p=25592 u=root | [WARNING]: Failure using method (v2_playbook_on_start) in callback plugin (</tmp/ansible/callback_plugins/datadog_callback.CallbackModule object at 0x7f38477cef90>): 'list' object has no attribute 'startswith' Happy to provide any other information that might be useful here.
Author   jaredledvina
🌐
Blogger
julene-lharudhar.blogspot.com › 2011 › 05 › ansible-attributeerror-object-has-no.html
Julee: ansible AttributeError: 'list' object has no attribute 'startswith' -
May 15, 2011 - an exception occurred during task execution. full traceback is: traceback (most recent call last): file "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 96, in run item_results = self._run_loop(items) file "/usr/lib/python2.7/site-packages/ansible/executor/task_ex...
🌐
GitHub
github.com › ansible › ansible › issues › 79676
AttributeError: 'int' object has no attribute 'startswith' · Issue #79676 · ansible/ansible
task path: /etc/ansible/devel/sysops/code/ansible/Update-splunkuf-spl.yml:54 The full traceback is: Traceback (most recent call last): File "/home/svc-ansiblemgmt/.local/lib/python3.9/site-packages/ansible/executor/task_executor.py", line 158, in run res = self._execute() File "/home/svc-ansiblemgmt/.local/lib/python3.9/site-packages/ansible/executor/task_executor.py", line 556, in _execute plugin_vars = self._set_connection_options(cvars, templar) File "/home/svc-ansiblemgmt/.local/lib/python3.9/site-packages/ansible/executor/task_executor.py", line 1038, in _set_connection_options if k.startswith('ansible_%s_' % self._connection._load_name) and k not in options: AttributeError: 'int' object has no attribute 'startswith' fatal: [windows-host1]: FAILED!
🌐
Server Fault
serverfault.com › questions › 742046 › ansible-vca-vapp-attributeerror-nonetype-object-has-no-attribute-startswith
vmware vcloud director - ansible vca_vapp AttributeError: 'NoneType' object has no attribute 'startswith' - Server Fault
December 10, 2015 - The error was: AttributeError: 'NoneType' object has no attribute 'startswith' fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "parsed": false} ... Solution: you need to set the (undocumented) 'host' variable.
🌐
Red Hat
bugzilla.redhat.com › show_bug.cgi
924889 – [abrt] ansible-1.1-0.git201303011750.fc18: __init__.py:192:path_dwim:AttributeError: 'dict' object has no attribute 'startswith'
March 22, 2013 - Red Hat Bugzilla – Bug 924889 · This site requires JavaScript to be enabled to function correctly, please enable it · Privacy Contact FAQ Legal
🌐
GitHub
github.com › ansible › ansible › issues › 60309
archive: AttributeError: 'dict' object has no attribute 'startswith' · Issue #60309 · ansible/ansible
August 9, 2019 - ansible 2.8.1 config file = /srv/ansible-deploy/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /bin/ansible python version = 2.7.5 (default, Apr 9 2019, 14:30:50) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] ... An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'dict' object has no attribute 'startswith' failed: [localhost] (item={u'version': u'1.6.5', u'name': u'apr'}) =
Author   OPSTime
🌐
GitHub
github.com › ansible › ansible › issues › 55400
ansible_facts failure - 'NoneType' object has no attribute 'startswith' · Issue #55400 · ansible/ansible
April 16, 2019 - SUMMARY Using Ansible 2.7.10 When running a custom module which uses ansible_facts(module), the module fails with "module_stderr": "AttributeError(\"'NoneType' object has no attribute 'startswith'\". Failure ispresent with this commit. P...
Author   mtnbikenc
🌐
GitHub
github.com › azavea › ansible-graphite › issues › 8
AttributeError: 'dict' object has no attribute 'startswith' · Issue #8 · azavea/ansible-graphite
August 21, 2015 - I get this error: Traceback (most recent call last): File "/usr/bin/ansible-playbook", line 324, in sys.exit(main(sys.argv[1:])) File "/usr/bin/ansible-playbook", line 264, in main pb.run() File "/usr/lib/pymodules/python2.7/ans...
Author   insanity54