1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Packer]Provisionersでaptがエラーになるときの対処法

Posted at

背景

Ubuntu 18.04ベースのAMI(Redash用のAMI)をベースにPackerでローカルAMIを作成しようとしたところ、Provisionersで以下のエラーが発生しビルドが失敗した。

    amazon-ebs: E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
    amazon-ebs: E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
==> amazon-ebs: Provisioning step had errors: Running the cleanup provisioner, if present...

原因

ubuntuでは定期的ないしサーバ起動時にaptが自動実行される設定があり、provisionersでaptを実行したときに重複起動によりエラー終了してしまう事がある。

対策

aptの自動起動を停止するとともに、万が一aptが動作している場合はwaitするようにすることで重複が発生しないようにする。

  "provisioners":[{
    "type": "shell",
    "inline": [
      "sudo sed -i '/APT::Periodic::Update-Package-Lists/s/1/0/' /etc/apt/apt.conf.d/20auto-upgrades",
      "sudo sed -i '/APT::Periodic::Unattended-Upgrade/s/1/0/' /etc/apt/apt.conf.d/20auto-upgrades",
      "sudo systemctl stop apt-daily.timer",
      "sudo systemctl disable apt-daily.timer",
      "sudo systemctl mask apt-daily.service",
      "sudo systemctl daemon-reload",
      "sudo sed -i '/APT::Periodic::Update-Package-Lists/s/1/0/' /etc/apt/apt.conf.d/10periodic",
      "while pgrep -f apt.systemd.daily > /dev/null; do echo 'waiting: apt.systemd.daily; done' >&2; sleep 1; done",
      "sudo rm -f /var/lib/dpkg/lock-frontend",
      "sudo apt update",
      "sudo apt -o Dpkg::Progress-Fancy=false -y install software-properties-common",
      "echo 'end apt install software-properties-common' >&2",
      "sudo apt-add-repository --yes --update ppa:ansible/ansible",
      "sudo apt -o Dpkg::Progress-Fancy=false -y install ansible",
      "mkdir -p /tmp/ansible-local",
      "sudo apt -o Dpkg::Progress-Fancy=false -y install chrony"
    ]

詳細解説

      "sudo sed -i '/APT::Periodic::Update-Package-Lists/s/1/0/' /etc/apt/apt.conf.d/20auto-upgrades",
      "sudo sed -i '/APT::Periodic::Unattended-Upgrade/s/1/0/' /etc/apt/apt.conf.d/20auto-upgrades",
      "sudo systemctl stop apt-daily.timer",
      "sudo systemctl disable apt-daily.timer",
      "sudo systemctl mask apt-daily.service",
      "sudo systemctl daemon-reload",

自動アップデートと自動アップグレードを無効

      "while pgrep -f apt.systemd.daily > /dev/null; do echo 'waiting: apt.systemd.daily; done' >&2; sleep 1; done",

起動している自動アップデート処理が終了するまで待つ

      "sudo rm -f /var/lib/dpkg/lock-frontend",

元となったAMIにlockファイルが残ってしまっていた場合を考慮して削除

      "sudo apt update",
      "sudo apt -o Dpkg::Progress-Fancy=false -y install software-properties-common",
      "echo 'end apt install software-properties-common' >&2",
      "sudo apt-add-repository --yes --update ppa:ansible/ansible",
      "sudo apt -o Dpkg::Progress-Fancy=false -y install ansible",
      "mkdir -p /tmp/ansible-local",
      "sudo apt -o Dpkg::Progress-Fancy=false -y install chrony"

以降は通常のaptを利用したインストール作業。

Packerの場合はプログレスバーの出力を抑制するために -o Dpkg::Progress-Fancy=false と yes/no 入力待ちで引っかからないようにするために -y オプションを利用する。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?