Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ansible define new variable with value from other

I need some help with Ansible variables.

---
- name: create remote ansible account
  hosts: all
  gather_facts: false
  remote_user: admin
  vars:
    ansible_ssh_pass: mypassword
    ansible_become_pass: mypassword
    publickey: "{{ inputvalue }}"

  vars_files:
    - publickey_file.yml

roles:
  - create account

publickey_file.yml looks like this:

entry1: ssh-rsa AAAAB3....
entry2: ssh-rsa AAAAC3....

Specific task in role looks like this:
aml

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

- name: install SSH Key
  authorized_key: 
    user: ansible
    key: '{{ publickey }}'
  become: yes

I would like to push a specific public key when specifying a variables with ansible-playbook.

I tried this, but it does not work:

ansible-playbook -i inventory.yml myplaybook.yml -e 'inputvalue=entry1'

This does not insert the value "{{ entry1 }}" but only the word 'entry1', so, inserted the key are not correct in module the authorized_key.

How can I insert, in publickey, the variable value "{{ entry1 }}" instead of 'entry1'?

>Solution :

You need the vars lookup in order to find a variable named as the string contained in the variable inputvalue:

publickey: "{{ lookup('vars', inputvalue) }}"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading