atlantis icon indicating copy to clipboard operation
atlantis copied to clipboard

atlantis.yaml should support YAML anchors and aliases

Open tomharrisonjr opened this issue 2 years ago • 2 comments

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request. Searching for pre-existing feature requests helps us consolidate datapoints for identical requirements into a single place, thank you!
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.

Overview of the Issue

We deploy to multiple environments, using the same general patterns in atlantis.yaml: only dir, environment (terraform workspace) change. We have 15 or 20 projects x 3 environments, so lots and lots of repeated code.

So we tried using YAML anchors and aliases to make our config more concise:

version: 3
definitions:
  - standard_options: &standard_options
      autoplan:
        when_modified: [ "*.tf" ]
        enabled: true
      apply_requirements: [ mergeable, approved ]
projects:
  - dir: vpc
    workspace: dev
    standard_options: *standard_options
  - dir: vpc
    workspace: staging
    standard_options: *standard_options
  - dir: vpc
    workspace: prod
    standard_options: *standard_options
  - dir: eks
    workspace: dev
    standard_options: *standard_options
  - dir: eks
    workspace: staging
    standard_options: *standard_options
  - dir: eks
    workspace: prod
    standard_options: *standard_options

and so on. While the above YAML is valid (e.g. https://codebeautify.org/yaml-validator) it fails when parsed by Atlantis.

Reproduction Steps

Modify standard atlantis.yaml similarly to example above, adding anchors in common section, and referring to aliases in standard project section.

Logs

parsing atlantis.yaml: yaml: unmarshal errors:
  line 2: field definitions not found in type raw.RepoCfg
  line 18: field standard_options not found in type raw.Project
  line 22: field standard_options not found in type raw.Project
  line 26: field standard_options not found in type raw.Project
  line 30: field standard_options not found in type raw.Project
  line 34: field standard_options not found in type raw.Project

and so on

Environment details

Atlantis version: 0.18.4 (and prior)

runatlantis/atlantis:v0.18.4

Repo atlantis.yaml file:

see above

All works fine if each section of the atlantis.yaml is expanded, but is substantially longer and more prone to error: e.g.

version: 3
projects:
  - dir: vpc
    workspace: dev
    autoplan:
      when_modified: [ "*.tf" ]
      enabled: true
    apply_requirements: [ mergeable, approved ]
  - dir: vpc
    workspace: staging
    autoplan:
      when_modified: [ "*.tf" ]
      enabled: true
    apply_requirements: [ mergeable, approved ]
  - dir: vpc
    workspace: prod
    autoplan:
      when_modified: [ "*.tf" ]
      enabled: true
    apply_requirements: [ mergeable, approved ]
  - dir: eks
    workspace: dev
    autoplan:
      when_modified: [ "*.tf" ]
      enabled: true
    apply_requirements: [ mergeable, approved ]
  - dir: eks
    workspace: staging
    autoplan:
      when_modified: [ "*.tf" ]
      enabled: true
    apply_requirements: [ mergeable, approved ]
  - dir: eks
    workspace: prod
    autoplan:
      when_modified: [ "*.tf" ]
      enabled: true
    apply_requirements: [ mergeable, approved ]

Additional Context

n/a

tomharrisonjr avatar May 18 '22 09:05 tomharrisonjr

The underlying library does support YAML anchors, but there are a couple of problems with the atlantis.yaml you've provided.

The first issue is that Atlantis uses strict YAML parsing, so unknown keys are an error rather than being silently ignored. In this case, your top-level definitions is not something Atlantis recognizes. Same goes for the standard_options field you've added.

To get this to work, you could either set the anchor to your first real project and override the dir/workspace per-item, or create a placeholder element. Here are two options that may work for you:

Placeholder:

version: 3                                                                                                                                                              
projects:                                                                                                                                                               
  - &standard_options                                                                                                                                                   
    dir: DUMMY_PLACEHOLDER                                                                                                                                              
    apply_requirements: [mergeable, approved]                                                                                                                           
    autoplan:                                                                                                                                                           
      when_modified: ["*.tf"]                                                                                                                                           
      enabled: true                                                                                                                                                     
  -                                                                                                                                                                     
    <<: *standard_options                                                                                                                                               
    workspace: staging                                                                                                                                                  
    dir: eks                                                                                                                                                            
  -                                                                                                                                                                     
    <<: *standard_options                                                                                                                                               
    dir: eks                                                                                                                                                            
    workspace: prod     
# rest of projects...

Alternatively, you can just anchor your first project (may be less desirable because it will pick up the workspace/dir should you forget to override per-project)

version: 3                                                                                                                                                              
projects:                                                                                                                                                               
  - &standard_options                                                                                                                                                   
    dir: vpc                                                                                                                                                            
    workspace: dev                                                                                                                                                      
    apply_requirements: [mergeable, approved]                                                                                                                           
    autoplan:                                                                                                                                                           
      when_modified: ["*.tf"]                                                                                                                                           
      enabled: true                                                                                                                                                     
  -                                                                                                                                                                     
    <<: *standard_options                                                                                                                                               
    workspace: staging                                                                                                                                                  
    dir: eks                                                                                                                                                            
  -                                                                                                                                                                     
    <<: *standard_options                                                                                                                                               
    dir: eks                                                                                                                                                            
    workspace: prod 
# rest of projects...

ascandella avatar May 19 '22 16:05 ascandella

Hi @ascandella I like your anchor the common part of these projects. Could you also recommend me if I want to inherit common setting just extend one path in when_modified block? Here is my example

---
version: 3
projects:
 - &common
   dir: ./A-dev
   apply_requirements: [mergeable, approved]
   workflow: A
   autoplan:
     when_modified:
       - "**/*.tf*"
 - 
   <<: *common
   dir: ./A-prod
 - 
   <<: *common
   dir: ./B-dev
   autoplan:
     when_modified:
       - "**/*.tf*"
       - "*.hcl"

titanlien avatar Jul 18 '22 07:07 titanlien

@titanlien did you find a way to extend one path or more in when_modified block?

amohamedhey avatar Dec 19 '22 11:12 amohamedhey

extend one path or more in when_modified block?

@amohamedhey I think this is no solution to add an item into a list in this module right now. My current solution is overwrite the whole list.

titanlien avatar Dec 29 '22 08:12 titanlien