13

I've got a problem with providing a value via extra vars when I run my playbook using:

ansible-playbook gitolite-docker.yml -e "GITOLITE_SSH_KEY=$(cat roles/gitolite-docker/files/john_rsa.pub)" --ask-vault-pass

Here is the extract of the gitolite-docker.yml

- name: logging admin.pub
  shell: echo "{{GITOLITE_SSH_KEY}}" > /home/ansusersu/gitoliteadmin.pub

- name: create gitolite--docker container
  docker_container: 
    name: gitolite
    image: alex2357/docker-gitolite
    state: started
    ports:
      - "8081:22"
    volumes:
      - "/docker/volumes/gitoliterepositories:/home/git/repositories"
    env:
      SSH_KEY: "{{GITOLITE_SSH_KEY}}"
      KEEP_USERS_KEYS: "dummytext"      
  become: yes 

The problem is that I get only first few characters "ssh-rsa" from the SSH key.

john@john-VirtualBox:~$ sudo cat /home/ansusersu/gitoliteadmin.pub
ssh-rsa
john@john-VirtualBox:~$ 

I get exactly the same value in both usages of {{GITOLITE_SSH_KEY}}. In the Docker container I have exactly the same value in log files.

For Docker similar line works fine:

docker run -d -p 8081:22 --name gitolite -e SSH_KEY="$(cat ~/.ssh/id_rsa.pub)" -v /docker/volumes/gitoliterepositories:/home/git/repositories alex2357/docker-gitolite

When I saw that it seems to me I won't be able to achieve the same behavior with Ansible-playbook as with Docker as it considers the remaining staff as another extra var. Is there way to make it work?

1 Answer 1

17

Proper quoting should resolve the issue:

ansible-playbook gitolite-docker.yml -e "GITOLITE_SSH_KEY='$(cat roles/gitolite-docker/files/john_rsa.pub)'" --ask-vault-pass

Eventually with double-quotes:

ansible-playbook gitolite-docker.yml -e "GITOLITE_SSH_KEY=\"$(cat roles/gitolite-docker/files/john_rsa.pub)\"" --ask-vault-pass
6
  • Many thanks for quick answer, but 2nd option doesn't work at all. john@john-VirtualBox:/etc/ansible$ ansible-playbook gitolite-docker.yml -e GITOLITE_SSH_KEY=\"$(cat roles/gitolite-docker/files/john_rsa.pub)\" --ask-vault-pass ERROR! the playbook: AAAAB3NzaC1...5dw== could not be found Jan 5, 2017 at 0:54
  • first option still have same problem only first few characters Jan 5, 2017 at 0:55
  • Many thanks!!! Indeed, the last edit worked fine! ansible-playbook gitolite-docker.yml -e "GITOLITE_SSH_KEY=\"$(cat roles/gitolite-docker/files/john_rsa.pub)\"" --ask-vault-pass Jan 5, 2017 at 1:00
  • 2
    You can also use the JSON format with -e, which probably isn't that useful in this situation, but it's very good if you want to pass a boolean or a integer to Ansible, because with var=value you can only pass strings. It would look like this: ansible-playbook gitolite-docker.yml -e "{ 'GITOLITE_SSH_KEY': '$(cat roles/gitolite-docker/files/john_rsa.pub)' }" --ask-vault-pass. Jan 7, 2017 at 20:41
  • 1
    Thanks, additional escaped quotes seem to work for strings that contain spaces. What is the black magic behind that?
    – Dmitriusan
    Dec 21, 2018 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.