Ansible 为什么我的任务挂起了?

Ansible 为什么我的任务挂起了?,ansible,ansible-playbook,Ansible,Ansible Playbook,我有以下ansible剧本: - hosts: node1 sudo: yes gather_facts: no tasks: - name: update apt apt: update_cache=yes - name: install python-setuptools apt: name=python-setuptools update_cache=yes - name: easy_install pexpect module easy_

我有以下ansible剧本:

- hosts: node1
  sudo: yes
  gather_facts: no

  tasks:
  - name: update apt
    apt: update_cache=yes
  - name: install python-setuptools
    apt: name=python-setuptools update_cache=yes
  - name: easy_install pexpect module
    easy_install: name=pexpect state=latest
  - name: add geonode repo
    apt_repository: repo='ppa:geonode/stable' state=present
  - name: update apt
    apt: update_cache=yes
  - name: install geonode
    apt: name=geonode update_cache=yes
  - expect:
        command: geonode createsuperuser
        responses:
          (?i)username: 'test'
          (?i)email: 'test@test.com'
当我运行它时,我得到:

PLAY [node1] *******************************************************************

TASK [update apt] **************************************************************
ok: [node1]

TASK [install python-setuptools] ***********************************************
changed: [node1]

TASK [easy_install pexpect module] *********************************************
changed: [node1]

TASK [add geonode repo] ********************************************************
changed: [node1]

TASK [update apt] **************************************************************
ok: [node1]

TASK [install geonode] *********************************************************
然后它无限期地挂起。 在远程节点(node1)中,我检查了dir

/家里/流浪汉/.ansible/tmp/ansible-tmp-1470059145.13-122191240803512/

运行里面的文件,看看为什么我的任务挂起

vagrant@node1:~/.ansible/tmp/ansible-tmp-1470059145.13-122191240803512$python apt

并获得:

{"msg": "Failed to lock apt for exclusive operation", "failed": true, "invocation": {"module_args": {"dpkg_options": "force-confdef,force-confold", "autoremove": false, "force": false, "name": "geonode", "install_recommends": null, "package": ["geonode"], "purge": false, "allow_unauthenticated": false, "state": "present", "upgrade": null, "update_cache": true, "default_release": null, "only_upgrade": false, "deb": null, "cache_valid_time": null}}}
你有什么见解吗

编辑1:


我整天都在发布这个脚本,但从来没有让它工作过。当我发布这个问题时,很明显,脚本在15分钟内成功执行到最后。我在今天午饭前启动了它,一个小时后它仍然挂着。为什么我会有如此不同的行为?有什么方法可以控制它吗?

此问题可能是由空的
/var/lib/apt文件夹引起的

Vagrant可能需要一段时间来填充这些文件夹,这可能会导致apt锁定

此外,由于多次使用
update\u cache
,playbook效率低下。我建议您使用以下内容:

- hosts: node1
  sudo: yes
  gather_facts: no

  tasks:
    # Pause for 5 minutes to make sure vagrant does not hold apt lock.
    - pause:
        minutes: 5

    - name: add geonode repo
      apt_repository:
        repo: 'ppa:geonode/stable'
        state: present

    - name: Install apt packages.
      apt:
        name: "{{ item }}"
        state: present
        update_cache: true
      with_items:
        - python-setuptools
        - geonode

  - name: Create geonode superuser.
    expect:
      command: geonode createsuperuser
      responses:
        (?i)username: 'test'
        (?i)email: 'test@test.com'        

这样Ansible就不会在播放过程中多次更新存储库。

因为您最后看到的是
任务[install geonode]
,这就是它被卡住的地方

您要求它运行
geonode createsuperuser
,您希望它会提示输入用户名和密码

但可能发生的情况是,该命令产生了一个错误,
expect
任务没有处理错误,只是挂起

您可以登录到运行此操作的服务器,并手动运行
geonode createsuperuser
命令以查看产生了什么错误

在我的例子中,这是由于我已经在这台机器上成功运行了该命令,所以已经使用了用户名

错误:该用户名已被使用。

即使使用了
echo:yes
参数,ansible似乎也不会传递响应以使发生的事情变得明显。而且它不接受
ignore\u errors
,因此似乎无法使用
expect
模块处理错误

为了解决这个问题,我在createsuperuser任务之后添加了另一个任务,该任务在项目中放置了一个文件,指示用户创建过一次,然后将
creates:{{path}}/superuser_exists.txt
添加到
createsuperuser
任务中,这样如果该文件已经存在,它就不会运行

这是一个黑客攻击,但很容易,在模块得到更好的错误处理之前,它将工作得足够好

- name: Create the django superuser
  expect:
    command: "{{ virtualenv_path }}/bin/python3 {{ project_path }}/{{ api_app_name }}/manage.py createsuperuser"
    creates: "{{ project_path }}/{{ api_app_name }}/superuser_exists.txt"
    responses:
      (?i)username: "{{ superuser_username }}"
      (?i)email: "{{ superuser_email }}"
      (?i)password: "{{ superuser_password }}"
      (?i)again: "{{ superuser_password }}"

- name: Create a file to indicate that the superuser was already created
  file: path="{{ project_path }}/{{ api_app_name }}/superuser_exists.txt" state=touch

是否另一个
apt
命令也在已经获得锁的系统上运行(不是从本手册触发的)?否,没有其他apt运行。.旁注:不需要多次运行
update\u cache=yes
。这不是问题所在,但它会减慢你的行动计划。每次运行
update\u cache=yes
Ansible将运行
apt get update