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?

Community
  • 1
  • 1
user1325696
  • 616
  • 1
  • 8
  • 16

1 Answers1

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
techraf
  • 64,883
  • 27
  • 193
  • 198
  • 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 – user1325696 Jan 05 '17 at 00:54
  • first option still have same problem only first few characters – user1325696 Jan 05 '17 at 00: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 – user1325696 Jan 05 '17 at 01:00
  • Sorry for confusion with multiple edits. Glad it worked for you. – techraf Jan 05 '17 at 01:01
  • 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`. – Strahinja Kustudic Jan 07 '17 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 '18 at 17:04