ansible: How to run 'composer install' on production?

Viewed 3212

First: I am very new to ansible—I just started yesterday. Most things work quite well (installing packages, cloning my git repo, etc.), but I have not been able to get composer install to run to save me.

Here's the setup:

  • Laravel 8.x project.
  • Dev environment runs under vagrant (Ubuntu 20.04)
  • ansible version 2.9.6
  • Remote is a Ubuntu 20.04 EC2 instance.
  • Project root is /var/www/roster

I have temporarily set the the remote vendor directory to 0777 and the owner to ubuntu (the EC2 default ssh user) but reset it to 0755 and root when done.

I've tried several variations, but when I run the playbook with ansible-playbook -i ./hosts composer_install.yml, it just hangs. Here are the things I've tried:

  tasks:
    - name: Composer install
      shell: composer install
      args:
        chdir: /var/www/roster

...and:

  tasks:    
    - name: Composer install
      composer: command=install working_dir=/var/www/roster optimize_autoloader=no

...and (with the ansible-composer plugin installed):

  tasks:
    - name: Composer install
      community.general.composer:
        command: install
        working_dir: /var/www/roster

I'm sure this is something that can be done, but how?

3 Answers

Problem is in root user, when you run

sudo php /usr/local/bin/composer --working-dir=/path/to/project install

Composer ask you

Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]?

Because of that ansible stack on this task.
You must user next construct to fix it

- name: "Composer install"
  become: yes
  become_user: not_root_user
  composer:
    command: install
    global_command: false
    working_dir: /path/to/project

As correctly noted by Vlad Gor in his answer, it is most likely the case that you run the command as root (or with sudo).

In case you cannot/don't want to switch users, this can be circumvented by using the parameter --no-interaction or a little simpler with ansible the corresponding environment variable COMPOSER_NO_INTERACTION: "1"

For example:

 tasks:    
    - name: Composer install
      composer: 
        command: install 
        working_dir: /var/www/roster
      environment:
        COMPOSER_NO_INTERACTION: "1"

Okay, this is not really an "answer" but I was able to successfully run composer install via ansible. The issue in my case was with the latest version composer. I was on 2.0.4 and, when I rolled back to 1.10.17, all was well.

Related