# Composer Issue with HTTP auth

**URL:** https://discourse.roots.io/t/composer-issue-with-http-auth/24696
**Category:** trellis
**Created:** 2023-02-01T02:52:10Z
**Posts:** 9

## Post 1 by @adleviton — 2023-02-01T02:52:10Z

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

---

## Post 2 by @adleviton — 2023-02-01T03:14:34Z

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 }}"
```

---

## Post 3 by @swalkinshaw — 2023-02-01T04:43:09Z

: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.

---

## Post 4 by @slowrush — 2023-02-02T19:16:21Z

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!

---

## Post 5 by @dalepgrant — 2023-02-03T05:21:07Z

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

---

## Post 6 by @TangRufus — 2023-02-03T15:14:28Z

PR created - [Composer Authentications: Fix `loop_var` already in use issue by TangRufus · Pull Request #1469 · roots/trellis · GitHub](https://github.com/roots/trellis/pull/1469)

Can you give it a try please?

---

## Post 7 by @adleviton — 2023-02-28T20:59:56Z

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 }}"
```

---

## Post 8 by @TangRufus — 2023-03-02T18:53:57Z

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](https://github.com/roots/trellis/pull/1469)  
(I think it makes sense to rename it again for avoiding variable collision)

---

## Post 9 by @adleviton — 2023-03-02T19:11:06Z

ansible 2.10.16

Python 3.10.6
