How specify a list value as variable in ansible inventory file?

后端 未结 5 1166
栀梦
栀梦 2020-12-13 06:34

I need something like (ansible inventory file):

[example]
127.0.0.1 timezone=\"Europe/Amsterdam\" locales=\"en_US\",\"nl_NL\"

However, ansi

相关标签:
5条回答
  • 2020-12-13 06:44

    With complex variables, it's best to define them in a host_vars file rather than in the inventory file, since host_vars files support YAML syntax.

    Try creating a host_vars/127.0.0.1 file with the following content:

    timezone: Europe/Amsterdam
    locales:
        - en_US
        - nl_NL
    
    0 讨论(0)
  • 2020-12-13 06:50

    You can custom a filter, to split string to list

    Github ansible example show how to create custom filter.

    0 讨论(0)
  • 2020-12-13 06:56

    You can pass a list or object like this:

    [example]
    127.0.0.1 timezone="Europe/Amsterdam" locales='["en_US", "nl_NL"]'
    
    0 讨论(0)
  • 2020-12-13 07:00

    Ryler's answer is good in this specific case but I ran into problems using other variations with the template module.

    [example]
    127.0.0.1 timezone="Europe/Amsterdam" locales='["en_US", "nl_NL"]'
    

    Is his original example and works fine.

    The following variations work with template. Basically if it's a string you must remember to use the internal double quotes or the entire structure is parsed as a single string. If it's only numbers or "True" or "False" (not "yes") then you're fine. In this variation I couldn't make it work with template if it had external quotes.

    I haven't done an exhaustive check of which internal use cases they do and do not break other than the template module.

    I am using Ansible 2.2.1.

    [example:vars]
    # these work
    myvar1=["foo", "bar"]
    myvar2=[1,2]
    myvar3=[True,False]
    
    # These fail, they get interpreted as a single string.
    myvar4=[yes, no]
    myvar5=[foo,bar]
    myvar6='["foo", "bar"]'
    
    0 讨论(0)
  • 2020-12-13 07:06

    you can try split

    #inventory file
    [example]
    127.0.0.1 timezone="Europe/Amsterdam" locales="en_US","nl_NL"
    
    #role file
    ---
    - debug: msg="{{ item }}"
      with_items: locales.split(',')
    
    0 讨论(0)
提交回复
热议问题