代码之家  ›  专栏  ›  技术社区  ›  Chris F

在Ansible中,如何从字典中获取值并将其存储在事实中?

  •  1
  • Chris F  · 技术社区  · 7 年前

    如何修改中的答案 Search Dictionary Values in Ansible

    我有一本字典:

    filenames_for_version:
      1.0:
        file1: "Hello.txt"
        file2: "Goodbye.txt"
      2.0:
        file1: "HelloWorld.txt"
        file2: "GoodbyeWorld.txt"
    

    现在我想写一个任务,做。。。

    - name: Get the corresponding **file1** for the version
      set_fact: f1_4_ver={{ what do I put here??? }}
    - name: Get the corresponding **file2** for the version
      set_fact: f2_4_ver={{ what do I put here??? }}
    

    我希望在调用playbook时能够将版本作为CLI参数传递,例如。,

    $ ansible-playbook -i "localhost," -c local get_filename_for_version.yml -e "version=1.0"
    

    …如果我通过版本=1.0,我得到。。。

    f1_4_ver=Hello.txt
    f2_4_ver=Goodbye.txt
    

    …如果我通过版本=2.0,我得到。。。

    f1_4_ver=HelloWorld.txt
    f2_4_ver=GoodbyeWorld.txt
    

    在。。

    {{ what do I put here??? }}
    

    …我任务的一部分?

    1 回复  |  直到 7 年前
        1
  •  1
  •   techraf    7 年前

    在变量/键名中使用点是在自找麻烦,但是:

    {{ filenames_for_version[version].file1 }}
    {{ filenames_for_version[version].file2 }}