我正在尝试通过packer
及其ansible
的供应者根据AWS提供的官方Ubuntu 18.04
AMI来提供自定义AMI。
我用于搜索适当基本ami的打包程序设置如下:
"source_ami_filter": {
"filters": {
"virtualization-type": "hvm",
"name": "*{{user `ami-os`}}*",
"root-device-type": "ebs",
"architecture": "x86_64"
},
"owners": ["amazon"],
"most_recent": true
},
我即时传递变量的地方:
-var "ami-os=ubuntu-bionic-18.04-amd64-server"
由于该AMI未安装python2
,并且我想使用ansible
进行配置,因此我必须通过raw
执行该操作:
- name: pre_tasks --> Install python2 for Ansible
raw: bash -c "test -e /usr/bin/python || (apt -qqy update && apt install -qqy python-minimal)"
become: yes
register: output
changed_when: output.stdout != ""
when: ansible_isbionic
但是,在大多数情况下,上述操作失败并显示消息消息apt
已被锁定:
amazon-ebs: fatal: [default]: FAILED! => {"changed": true, "msg": "non-zero return code", "rc": 100, "stderr": "\nWARNING: apt does not have a stable CLI interface. Use with caution in scripts.\n\n\nWARNING: apt does not have a stable CLI interface. Use with caution in scripts.\n\nE: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)\nE: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?\nShared connection to 127.0.0.1 closed.\r\n", "stderr_lines": ["", "WARNING: apt does not have a stable CLI interface. Use with caution in scripts.", "", "", "WARNING: apt does not have a stable CLI interface. Use with caution in scripts.", "", "E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)", "E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?", "Shared connection to 127.0.0.1 closed."], "stdout": "190 packages can be upgraded. Run 'apt list --upgradable' to see them.\n", "stdout_lines": ["190 packages can be upgraded. Run 'apt list --upgradable' to see them."]}
为克服这一点,我在明确执行:
- name: pre_tasks.yml --> Kill any apt commnds running
raw: bash -c "killall apt apt-get || echo 'no apt-related processes found'"
become: yes
when: ansible_isbionic
- name: pre_tasks.yml --> Remove apt lock files
raw: bash -c "rm -f /var/lib/apt/lists/lock"
become: yes
when: ansible_isbionic
- name: pre_tasks.yml --> Remove apt cache lock files
raw: bash -c "rm -f /var/cache/apt/archives/lock"
become: yes
when: ansible_isbionic
- name: pre_tasks.yml --> Remove apt cache lock files
raw: bash -c "rm -f /var/lib/dpkg/lock"
become: yes
when: ansible_isbionic
您知道apt
进程为何被锁定吗?
答案 0 :(得分:1)
我可以建议您等待启动/更新完成,但不要杀死锁定文件。
我们在云配置脚本中使用了这些技巧:
while [ ! -f /var/lib/cloud/instance/boot-finished ]; do
# wait...
done
while fuser /var/lib/apt/lists/lock >/dev/null 2>&1 ; do
# wait...
done
使用Ansible时,您可以将此指令打包到bash脚本中,并使用script
模块而不是许多raw
执行。 script
模块也不需要Python出现在托管主机上。
答案 1 :(得分:0)
我认为这也许是一个更现代的答案,尽管我不确定它是否更正确
cloud-init status -w