Closed bonecountysheriff closed 1 year ago
chezmoi uses Go'stext/template
package for templates. For loops, use the range
action.
Thank you for the quick reply, I'm going through the documentation for go templates right now and if everything works, I'll close the issue right away.
{{ range $_, $name := list "user1" "user2" "user3" -}}
{{ range $_, $host := list "gitlab" "github" -}}
Host {{ $host -}}-{{- $name }}
User git
HostName {{ $host -}}.com
IdentityFile ~/.ssh/{{- $name }}
{{ end -}}
{{ end -}}
Thanks a lot @bradenhilton, I really like this solution. The documentation on this has been a little hard to find. May I know what the hyphens and $_ are for?
It seems some of the usernames must not be present in the dotfiles. Is there a way add them from outside the config file?
The hyphens are there to control whitespace, the ones around the variables (e.g. {{ $host -}}
) aren't actually necessary in my example but I add them habitually.
$_
was used because range
can assign a index
and element
variable. It is similarly unnecessary and was also used habitually.
As for your other question, I'm sorry but I don't understand. How are you gathering the usernames currently?
I just add them manually without any template.
I meant to ask if there is a way to assign an array from the command line for example. Something like this -
prompt> name=(some array) chezmoi apply filename
I meant to ask if there is a way to assign an array from the command line for example. Something like this -
prompt> name=(some array) chezmoi apply filename
This is possible, but you should not do it.
Firstly, this is possible because you can set an environment variable and then read it in a template with the env
template function.
Secondly, you should not do this because you'll have to specify name=
every time you run a chezmoi command that evaluates the source state, like chezmoi diff
, chezmoi status
, or chezmoi apply
. If you want to store structured data in a semi-permanent way such that you don't have to specify it every time then consider using a .chezmoidata
file.
What exactly are you trying to do?
I'm trying to configure SSH for multiple users on github and gitlab and am considering using chezmoi templates for the purpose since I already use it for managing dotfiles.
The config file is located at
~/.ssh/config
. SSH configuration feels quite inflexible and doesn't support features like including an external file or looping. So everything needs to be done by chezmoi. I'm starting with a template file which looks something like this.config.tmpl
In the above template, suppose $name assumes three different values - user1, user2 and user3. Then the output (final config) should be the following.
~/.ssh/config
I have looked through chezmoi's template guide, but there was no mention of loops. I was hoping if this could be achieved in a different way.
Where else have you checked for solutions?
Output of
chezmoi doctor