accessing nested variable variables in ansible

后端 未结 1 1758
死守一世寂寞
死守一世寂寞 2021-02-19 11:55

Here is my group_vars/all file:

app_env: staging

staging:
  app_a:
    db_host: localhost
  app_b:
    db_host: localhost
production:
  app_a:
    db_host: app_         


        
1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-19 12:29

    You would make your life easier with your 'environment dict' not being at the root, like so:

    app_env: staging
    
    app_environments:
      staging:
        app_a:
          db_host: localhost
        app_b:
          db_host: localhost
      production:
        app_a:
          db_host: app_a-db.example.net
        app_b:
          db_host: app_b-db.example.com
    

    Then, you should be able to use {{app_environments[app_env].app_a.db_host}} or {{app_environments[app_env]['app_a']['db_host']}} everywhere (Jinja templates, tasks).

    Watch out for too much "nestiness" though !

    0 讨论(0)
提交回复
热议问题