Skip to main content
added 446 characters in body
Source Link

The excellent answers on here have been working for me for ages but suddenly I started getting the dreaded

{"changed": false, "msg": "Failed to lock apt for exclusive operation"}

from Ansible again

I'm running an Ubuntu 20.04 AWS image in EC2, the issue turned out to be that since 16.04 the AWS SSM-agent is included by default as a snap package and snap runs on it's own update schedule. SSM-agent was trying to update at the exact time I was trying to do an apt update. There are various options to change the update schedule for snap or put it on hold for up to 60 days but it seems like it ignores whatever you say for the first update and will still cause instance provisioning to fail.

The only thing that worked for me was to run:

snap disable amazon-ssm-agent

as the first command in the instance user-data

and:

snap enable amazon-ssm-agent

as the last

It took me a long time to work this out

[ update ]

disabling snap will fail if the snap update is already running, this worked for me

i=0
while ! snap disable amazon-ssm-agent; do
  echo 'attempting to disable snap...' $i
  sleep 2
  ((i=i+1))
  if [ $i -gt 10 ]; then
    echo unable to disable snap..
    exit 1
  fi
done

what a mess

The excellent answers on here have been working for me for ages but suddenly I started getting the dreaded

{"changed": false, "msg": "Failed to lock apt for exclusive operation"}

from Ansible again

I'm running an Ubuntu 20.04 AWS image in EC2, the issue turned out to be that since 16.04 the AWS SSM-agent is included by default as a snap package and snap runs on it's own update schedule. SSM-agent was trying to update at the exact time I was trying to do an apt update. There are various options to change the update schedule for snap or put it on hold for up to 60 days but it seems like it ignores whatever you say for the first update and will still cause instance provisioning to fail.

The only thing that worked for me was to run:

snap disable amazon-ssm-agent

as the first command in the instance user-data

and:

snap enable amazon-ssm-agent

as the last

It took me a long time to work this out

The excellent answers on here have been working for me for ages but suddenly I started getting the dreaded

{"changed": false, "msg": "Failed to lock apt for exclusive operation"}

from Ansible again

I'm running an Ubuntu 20.04 AWS image in EC2, the issue turned out to be that since 16.04 the AWS SSM-agent is included by default as a snap package and snap runs on it's own update schedule. SSM-agent was trying to update at the exact time I was trying to do an apt update. There are various options to change the update schedule for snap or put it on hold for up to 60 days but it seems like it ignores whatever you say for the first update and will still cause instance provisioning to fail.

The only thing that worked for me was to run:

snap disable amazon-ssm-agent

as the first command in the instance user-data

and:

snap enable amazon-ssm-agent

as the last

It took me a long time to work this out

[ update ]

disabling snap will fail if the snap update is already running, this worked for me

i=0
while ! snap disable amazon-ssm-agent; do
  echo 'attempting to disable snap...' $i
  sleep 2
  ((i=i+1))
  if [ $i -gt 10 ]; then
    echo unable to disable snap..
    exit 1
  fi
done

what a mess

Source Link

The excellent answers on here have been working for me for ages but suddenly I started getting the dreaded

{"changed": false, "msg": "Failed to lock apt for exclusive operation"}

from Ansible again

I'm running an Ubuntu 20.04 AWS image in EC2, the issue turned out to be that since 16.04 the AWS SSM-agent is included by default as a snap package and snap runs on it's own update schedule. SSM-agent was trying to update at the exact time I was trying to do an apt update. There are various options to change the update schedule for snap or put it on hold for up to 60 days but it seems like it ignores whatever you say for the first update and will still cause instance provisioning to fail.

The only thing that worked for me was to run:

snap disable amazon-ssm-agent

as the first command in the instance user-data

and:

snap enable amazon-ssm-agent

as the last

It took me a long time to work this out