Search code examples
variablesansiblemanipulate

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

- 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) }}"