Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error updating /etc/hosts file on cenotos7 #4

Closed
DerManoMann opened this issue Oct 5, 2015 · 7 comments
Closed

error updating /etc/hosts file on cenotos7 #4

DerManoMann opened this issue Oct 5, 2015 · 7 comments

Comments

@DerManoMann
Copy link

Hi there,

I've been trying to run an ansible script on centos7. Any idea why that would be?
Or is the use of ansible limited in some way?

This is my setup:

FROM williamyeh/ansible:centos7

# general update
RUN yum -y update

ADD ./ /tmp/ansible

RUN ansible-playbook --sudo --connection=local -vvv -e restart=true -e scratch_build=false -e serial=10 -i /tmp/ansible/docker-nodes /tmp/ansible/site.yml

This is the error I get:

TASK: [node | generate hosts file] ********************************************
<127.0.0.1> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1444078648.91-266229075750000 && echo $HOME/.ansible/tmp/ansible-tmp-1444078648.91-266229075750000']
<127.0.0.1> EXEC ['/bin/sh', '-c', u'rc=flag; [ -r /etc/hosts ] || rc=2; [ -f /etc/hosts ] || rc=1; [ -d /etc/hosts ] && rc=3; python -V 2>/dev/null || rc=4; [ x"$rc" != "xflag" ] && echo "${rc} "/etc/hosts && exit 0; (python -c \'import hashlib; BLOCKSIZE = 65536; hasher = hashlib.sha1();\nafile = open("\'/etc/hosts\'", "rb")\nbuf = afile.read(BLOCKSIZE)\nwhile len(buf) > 0:\n\thasher.update(buf)\n\tbuf = afile.read(BLOCKSIZE)\nafile.close()\nprint(hasher.hexdigest())\' 2>/dev/null) || (python -c \'import sha; BLOCKSIZE = 65536; hasher = sha.sha();\nafile = open("\'/etc/hosts\'", "rb")\nbuf = afile.read(BLOCKSIZE)\nwhile len(buf) > 0:\n\thasher.update(buf)\n\tbuf = afile.read(BLOCKSIZE)\nafile.close()\nprint(hasher.hexdigest())\' 2>/dev/null) || (echo \'0 \'/etc/hosts)']
<127.0.0.1> PUT /tmp/tmpce0XQc TO /root/.ansible/tmp/ansible-tmp-1444078648.91-266229075750000/source
<127.0.0.1> PUT /tmp/tmp5F2W8p TO /root/.ansible/tmp/ansible-tmp-1444078648.91-266229075750000/copy
<127.0.0.1> EXEC /bin/sh -c 'sudo -k && sudo -H -S -p "[sudo via ansible, key=xluifxkeyvhjkjvbfusnehjxzdipsqpp] password: " -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-xluifxkeyvhjkjvbfusnehjxzdipsqpp; LANG=C LC_CTYPE=C /usr/bin/python /root/.ansible/tmp/ansible-tmp-1444078648.91-266229075750000/copy; rm -rf /root/.ansible/tmp/ansible-tmp-1444078648.91-266229075750000/ >/dev/null 2>&1'"'"''
failed: [127.0.0.1] => {"failed": true}
msg: Could not replace file: /root/.ansible/tmp/ansible-tmp-1444078648.91-266229075750000/source to /etc/hosts: [Errno 16] Device or resource busy

@William-Yeh
Copy link
Owner

Hi,

Don't know the content of your docker-nodes file. It seems that there's something wrong with it.

Could you try the following simpler example, using only 2 files (Dockerfile and site.yml)?

site.yml:

- hosts: all
  tasks:
    - name: install Apache web server
      yum: name=httpd  update_cache=yes  state=present

Dockerfile:

FROM williamyeh/ansible:centos7

# general update
RUN yum -y update

ADD . /tmp/ansible

RUN ansible-playbook --sudo --connection=local -vvv  \
    /tmp/ansible/site.yml

@DerManoMann
Copy link
Author

The docker-nodes file is quite simple:

[primary]
127.0.0.1 name=t1-docker mongodb_id=0 dc=local iptables=false

[nodes]

[backup]

This is a stripped down version of how the inventory is used for real.
Your example works as expected. I am not surprised because there are some yum update tasks that get executed in my playbook before the failing template task that also do work.

@William-Yeh
Copy link
Owner

The docker build . works for me, even if I incorporate your docker-nodes file in Dockerfile:

FROM williamyeh/ansible:centos7

# general update
RUN yum -y update

ADD . /tmp/ansible

RUN ansible-playbook --sudo --connection=local -vvv  \
    -i /tmp/ansible/docker-nodes  \
    /tmp/ansible/site.yml

Could you give a more self-contained but minimal example to reproduce the failure you've encountered?

@DerManoMann
Copy link
Author

Yup - it reallty is the template task (or whatever it is doing to update a local file)

site.yml:

- hosts: all
  tasks:
    - name: /etc/hosts template test
      template: src=/tmp/ansible/hosts.j2 dest=/etc/hosts

hosts.j2: just an empty file in the right place

@DerManoMann
Copy link
Author

Ah, just remembered about this and did another experiment.

While the above template task fails, it will work if the dest file does not yet exist or is created within the Dockerfile.

Example:
site.yml

- hosts: all
  tasks:
    - name: /etc/hosts template test
      template: src=/tmp/ansible/hosts.j2 dest=/tmp/abc

Dockerfile:

...
RUN echo abc >/tmp/abc
RUN ansible-playbook --sudo --connection=local -vvv  \
    /tmp/ansible/site.yml
...

I do not know much about docker and how the intermediate container and stuff work, but it seems that the /etc/hosts file is locked for whatever reason

@William-Yeh
Copy link
Owner

The /etc/hosts file is crucial for Docker's linking system. Therefore, in the Docker world, /etc/hosts should only be manipulated manually at the image level, rather than the container level.

See:

  1. http://stackoverflow.com/questions/28327458/how-to-add-my-containers-hostname-to-etc-hosts
  2. https://docs.docker.com/userguide/dockerlinks/

@DerManoMann
Copy link
Author

Oh, man! Sorry for wasing your time and lots of thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants