简体   繁体   English

Ansible 用其他值定义新变量

[英]Ansible define new variable with value from other

I need some help with Ansible variables.我需要一些有关 Ansible 变量的帮助。

---
- 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: publickey_file.yml看起来像这样:

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

Specific task in role looks like this: aml角色中的具体任务如下所示: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 .我想在使用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 .这不会插入值"{{ entry1 }}" ,而只会插入单词'entry1' ,因此,插入的密钥在authorized_key模块中不正确。

How can I insert, in publickey , the variable value "{{ entry1 }}" instead of 'entry1' ?如何在publickey中插入变量值"{{ entry1 }}"而不是'entry1'

You need the vars lookup in order to find a variable named as the string contained in the variable inputvalue :您需要vars查找才能找到名为变量inputvalue中包含的字符串的变量:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM