Is it possible to use ellipsis in a nested for loop as shown below
existing_nsg_rules = {
for key, rules in data.azurerm_network_security_group.subnet_nsg :
key => {
for rule_key in rules.security_rule : key=> key...{
priority = rule_key.priority
name = rule_key.name
}}}
I see this error Invalid ‘for’ expression: Extra characters after the end of the ‘for’ expression.
1 Like
Hi @khandujaniket,
The ... modifier for a for expression must be placed after the expression that represents the value for each element. In your case I think that would look like this:
existing_nsg_rules = {
for key, rules in data.azurerm_network_security_group.subnet_nsg : key => {
for rule_key in rules.security_rule : key => {
priority = rule_key.priority
name = rule_key.name
}...
}
}
Alternatively, if it’s the outer of the two that you want to use in the grouping, you’d place the ... after the inner for expression, like this:
existing_nsg_rules = {
for key, rules in data.azurerm_network_security_group.subnet_nsg : key => {
for rule_key in rules.security_rule : key => {
priority = rule_key.priority
name = rule_key.name
}
}...
}
If neither of these matches what you want to achieve, it’d help if you could share an example of what data structure you are trying to generate, using some example values.
1 Like
Can you add some additional context @apparentlymart in the case where you also need to flatten first? For example when we are creating a database on an existing RDS instance and want to configure each database using a yaml config file. The example below works fine for one database but will not work for multiples because the lack of grouping, hence duplicate keys will be found.
databases:
- name: my_db
namespace: dev
readonly_users:
- name: app_reader
password: my_32_char_password
crud_users:
- name: app_writer
password: my_32_char_password
migration_users:
- name: migration_user
password: my_32_char_password
readonly_users = flatten([
for db in var.bootstrap_config.databases : [
for user in db.readonly_users : {
username = "${db.namespace}_${user.name}"
password = user.password
db_name = "${db.name}_${db.namespace}"
}
]
])
crud_users = flatten([
for db in var.bootstrap_config.databases : [
for user in db.crud_users : {
username = "${db.namespace}_${user.name}"
password = user.password
db_name = "${db.name}_${db.namespace}"
}
]
])
migration_users = flatten([
for db in var.bootstrap_config.databases : [
for user in db.migration_users : {
username = "${db.namespace}_${user.name}"
password = user.password
db_name = "${db.name}_${db.namespace}"
}
]
])
In the above example, we won’t be able to use grouping ellipses when building a tuple. Is it then required to group first, and then flatten?
Hi @adamwshero,
This is a very old topic. Please start a new topic for your new question. Thanks!
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.