Composer Issue with HTTP auth

I can’t figure out what I’m missing here. I tried this on 2 different Trellis installs. And manually running the command below works fine, creates auth.json, and I can then run composer update without issue.

composer config --auth http-basic.composer.deliciousbrains.com XXX XXX

vault_wordpress_sites:
  mysite.test:
    composer_authentications:
      - { hostname: composer.deliciousbrains.com, username: XXX, password: XXX }
    admin_password: admin
    env:
      db_password: example_dbpassword

TASK [wordpress-install : Setup composer authentications (HTTP Basic) - mysite.test] ***
[WARNING]: The loop variable ‘item’ is already in use. You should set the
loop_var value in the loop_control option for the task to something else to
avoid variable collisions and unexpected behavior.
fatal: [default]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: {{ www_root }}/{{ item.key }}/{{ item.value.current_path | default(‘current’) }}/: ‘dict object’ has no attribute ‘key’\n\nThe error appears to be in ‘/mysite/trellis/roles/wordpress-install/tasks/composer-authentications.yml’: line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n—\n- name: "Setup composer authentications (HTTP Basic) - {{ site }}"\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”}

I got this to work by using loop_var.

  composer:
    command: config
    arguments: --auth http-basic.{{ c.hostname | quote }} {{ c.username | quote }} {{ c.password | default("") | quote }}
    working_dir: "{{ working_dir }}"
  become: no
  no_log: false
  changed_when: false
  when:
    - c.hostname is defined and c.hostname != ""
    - c.username is defined and c.username != ""
  loop: "{{ composer_authentications_using_basic_auth }}"
  loop_control:
    loop_var: c
    label: "{{ c.type | default('default-type') }}.{{ c.hostname }}"
1 Like

:thinking: yeah I think this is a bug. I’m assuming that warning is the issue:

[WARNING]: The loop variable ‘item’ is already in use.

That task is included within another loop directly so item is shadowed. It should be renamed like you did. I’ll fix this.

One of our devs is currently running into this issue. Weirdly it doesn’t affect my machine - not sure what we’re doing differently, but if you need some more testers, count us in!

Also hit this a few days ago, we did a quick solve by changing roles/wordpress-install/tasks/main.yml:

 - include_tasks: tasks/composer-authentications.yml
   vars:
-    site: "{{ item.key }}"
-    working_dir: "{{ www_root }}/{{ item.key }}/{{ item.value.current_path | default('current') }}/"
+    site: "{{ site.key }}"
+    working_dir: "{{ www_root }}/{{ site.key }}/{{ site.value.current_path | default('current') }}/"
   no_log: true
   loop: "{{ wordpress_sites | dict2items }}"
   loop_control:
-    label: "{{ item.key }}"
+    loop_var: site # Edit: We need a loop var other than the default 'item'.
+    label: "{{ site.key }}"

I meant to stick a PR up with the suggestion but time ran away from me. Would be happy to if this helps

3 Likes

PR created - Composer Authentications: Fix `loop_var` already in use issue by TangRufus · Pull Request #1469 · roots/trellis · GitHub

Can you give it a try please?

4 Likes

I’m still seeing an issue here. This all seemed to work fine for a fresh install of everything (for whatever reason). But I when I attempted to update a plugin behind HTTP Basic Auth, composer failed … and then I noticed the auth.json file was never generated, and not being generated via trellis commands.

I see this warning:

[WARNING]: The loop variable 'site' is already in use. You should set the
`loop_var` value in the `loop_control` option for the task to something else to
avoid variable collisions and unexpected behavior.

And simply changed ‘site’ to something else as the warning advised, and now my auth.json file is being generated as expected.

- include_tasks: tasks/composer-authentications.yml
  vars:
    site: "{{ site2.key }}"
    working_dir: "{{ www_root }}/{{ site2.key }}/{{ site2.value.current_path | default('current') }}/"
  no_log: false
  loop: "{{ wordpress_sites | dict2items }}"
  loop_control:
    loop_var: site2
    label: "{{ site2.key }}"
2 Likes

Which python and ansible versions you using?

I cannot reproduce the issue even before Composer Authentications: Fix `loop_var` already in use issue by TangRufus · Pull Request #1469 · roots/trellis · GitHub
(I think it makes sense to rename it again for avoiding variable collision)

ansible 2.10.16

Python 3.10.6