Ansible-container create Docker image with java8

2019-09-07 00:03发布

问题:

I'm trying to create simple Docker image with ansible-container build command.

And I can't figure out a solution to the error I came across while doing so.

Here are most important files:

main.yml

---
- hosts: scala-akka
  roles:
    - java8

container.yml

version: "1"
services:
  scala-akka:
    image: ubuntu:14.04

registries: {}

roles/java8/tasks/main.yml

---
# Add oracle ppa to apt_key
- name: Add oracke ppa to apt_repository
  apt_repository:
    repo: 'ppa:webupd8team/java'

# Install Oracle Java8
- name: Install oracle Java8
  apt:
    name: oracle-java8-installer
    state: present

The error I'm coming across every time I try to run

ansible-container --debug build

ansible-container_1  | Host scala-akka running
ansible-container_1  | Using /ansible-container/ansible/ansible.cfg as config file
ansible-container_1  | 
ansible-container_1  | PLAYBOOK: main.yml *************************************************************
ansible-container_1  | 1 plays in main.yml
ansible-container_1  | 
ansible-container_1  | PLAY [scala-akka] **************************************************************
ansible-container_1  | 
ansible-container_1  | TASK [setup] *******************************************************************
ansible-container_1  | <ansible_scala-akka_1> ESTABLISH DOCKER CONNECTION FOR USER: root
ansible-container_1  | <ansible_scala-akka_1> EXEC ['/usr/bin/docker', 'exec', '-i', u'ansible_scala-akka_1', u'/bin/sh', '-c', u'/bin/sh -c \'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1486389293.49-274006099773087 `" && echo ansible-tmp-1486389293.49-274006099773087="` echo $HOME/.ansible/tmp/ansible-tmp-1486389293.49-274006099773087 `" ) && sleep 0\'']
ansible-container_1  | <ansible_scala-akka_1> PUT /tmp/tmpi6WTY3 TO /root/.ansible/tmp/ansible-tmp-1486389293.49-274006099773087/setup
ansible-container_1  | <ansible_scala-akka_1> EXEC ['/usr/bin/docker', 'exec', '-i', u'ansible_scala-akka_1', u'/bin/sh', '-c', u'/bin/sh -c \'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1486389293.49-274006099773087/setup; rm -rf "/root/.ansible/tmp/ansible-tmp-1486389293.49-274006099773087/" > /dev/null 2>&1 && sleep 0\'']
ansible-container_1  | fatal: [scala-akka]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_name": "setup"}, "module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false}
ansible-container_1  | 
ansible-container_1  | NO MORE HOSTS LEFT *************************************************************
ansible-container_1  | 
ansible-container_1  | PLAY RECAP *********************************************************************
ansible-container_1  | scala-akka                 : ok=0    changed=0    unreachable=0    failed=1   
ansible-container_1  | 
ansible-container_1  |  [WARNING]: Could not create retry file 'main.retry'.         [Errno 2] No such
ansible-container_1  | file or directory: ''
ansible_ansible-container_1 exited with code 1
Aborting on container exit...
Stopping ansible_scala-akka_1 ... done
Ansible playbook run failed.
Cleaning up Ansible Container builder...
Cleaning up temporary directory /tmp/tmpme3nWT...
Ansible build failed
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/container/cli.py", line 212, in commandline
    getattr(engine, u'cmdrun_{}'.format(args.subcommand))(**vars(args))
  File "/usr/local/lib/python2.7/dist-packages/container/engine.py", line 384, in cmdrun_build
    raise RuntimeError(u'Ansible build failed')
RuntimeError: Ansible build failed

It looks like some kind of python problem, still I have no clue how to fix this.

Any suggestions? Thanks!

回答1:

Most of Ansible modules require Python on target host (container in your case).
ubuntu:14.04 Docker image doesn't have one preinstalled.

Use this in your main.yml:

---
- hosts: scala-akka
  gather_facts: false

  pre_tasks:
    - raw: 'which python || (apt-get -q -y update && apt-get install -q -y python-minimal)'
    - setup:

  roles:
    - java8