Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible-playbook extra vars with a space in a value

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?

like image 845
user1325696 Avatar asked Jan 05 '17 00:01

user1325696


People also ask

How do you pass extra vars in Ansible playbook?

To pass a value to nodes, use the --extra-vars or -e option while running the Ansible playbook, as seen below. This ensures you avoid accidental running of the playbook against hardcoded hosts.

What is extra Vars?

Ansible extra vars is a feature that allows you to include more flexibility in your Ansible playbooks by providing you with the ability to specify dynamic values when executing the playbook. Ansible extra vars are helpful when: You have a variable whose value may change more than once when running the playbook.

Can we pass variables in playbook?

You can define variables when you run your playbook by passing variables at the command line using the --extra-vars (or -e ) argument.

How do you define multiple variables in Ansible?

Variable Setting Options & Precedence As we have previously seen, the most straightforward way is to define variables in a play with the vars section. Another option is to define variables in the inventory file. We can set variables per host or set shared variables for groups.


1 Answers

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
like image 110
techraf Avatar answered Sep 21 '22 06:09

techraf