Hi ansible-users,
I have a little problem with my use case.
- I use ansible to create a docker instance with an random ssh port open
- I use ansible to customize the docker instance newly created
I can’t specify the ssh port for the next playbook…
Here is my playbook :
-
hosts: local
vars:
temporary_folder_for_building_docker: /tmp/docker
tasks:
-
shell: docker port developer_base_image_instance | grep 22 | sed ‘s/.:([0-9])$/\1/’
register: docker_ssh_port
-
debug: msg=“{{ docker_ssh_port.stdout }}”
-
set_fact: ansible_ssh_port={{ docker_ssh_port.stdout }}
-
hosts: docker_base
user: root
tasks:
-
command: ls -lha
In other words I want to get the ssh port from docker_ssh_port.stdout and use it as the ansible_ssh_port variable for the hosts docker_base.
Is there something that I’m missing?
Is this a twisted case, or not in ansible’s philosophy?
I’m using ansible 1.7.2
Regards,
Michaël
You need to set the fact on the docker host, but you can reference the hostvar of localhost where the stdout lives.
- hosts: docker_base
user: root
tasks:
- name: set ssh port
set_fact: ansible_ssh_port={{ hostvars[‘localhost’].docker_ssh_port.stdout }}
- command: ls -lha
-jlk
Thanks Jesse,
But this is not exactly what i’m trying to do.
I want to tell the hosts ‘docker_base’ to connect via ssh to a given random port which is extracted from the docker_ssh_port.stdout
I don’t know if this is even possible, it is very similar to this thread : https://groups.google.com/forum/#!topic/ansible-project/XPdPtHqEruo
Michaël
I was able to make this work thanks to you Jesse!
I added port: “{{ hostvars[‘local_docker_builder’].docker_ssh_port.stdout }}” and this worked.
I tried port: “{{ docker_ssh_port.stdout }}” yesterday but the error message was :
“{‘msg’: u’FAILED: Configured port “{{ docker_ssh_port.stdout }}” is not a valid port, expected integer’, ‘failed’: True}”
so i assumed that the port: could not be parameterized (i expected to have a “docker_ssh_port.stdout” si not a valid variable instead.
All parameters are parameterizable in Ansible.
do this:
- debug: var=docker_ssh_port
and will tell you what that variable contains
I’d need to see more in context to say for sure what you have.
In fact the thing that I find confusing is this:
If I have this playbook:
- hosts: docker_base
user: root
port: {{ bla_bla }}
tasks:
if bla_bla is not defined it will fail with the given message: “{‘msg’: u’FAILED: Configured port “{{ bla_bla }}” is not a valid port, expected integer’, ‘failed’: True}”
Seing this I assumed that port was not parameterizable.
I expected an error message more like “{‘msg’: u’FAILED: var “{{ bla_bla }}” is undefined’, ‘failed’: True}”
Regards,
Michaël
Sounds like you might have “fail on undefined variables” turned off on your ansible.cfg.
The default would be to fail.
I have ansible 1.7.2 and the only variable that I override is host_key_checking=False, the error_on_undefined_vars is True.
This is what puzzled me, I expected it to fail because of the undefined variable but it didnt, it gave me a confusing message instead.
Hmm, curious, feel free to file a ticket on that one w/ steps to reproduce it.
That shouldn’t be the case and should be easy to close off.
I do the same kind of thing to test my playbooks on a docker instance instead of a real VM (which is slower since my VMs are remote on AWS).
I use a dynamic group for my docker containers which is initially empty, but populated with the results on the task that starts the docker containers (the IP and port are in the output).
Example: https://github.com/nuxeo/nuxeo-tools-cloud/blob/master/ansible/jenkins/tasks/docker_start.yml
If you only start one container with a static group name, you can simplify this a bit.