@@ -253,6 +253,16 @@ func applyMain(cmd *cobra.Command, opts *cmdOpts.ApplyOpts) error {
253253 }
254254 }()
255255
256+ needsConfirmation := ! opts .AlwaysConfirm && ! cfg .Confirmation .Always
257+
258+ // If no confirmation is needed, then manually override the host
259+ // key verification type so that host keys can always be added to
260+ // known_hosts.
261+ hostKeyVerification := cfg .SSH .HostKeyVerification
262+ if hostKeyVerification == settings .HostKeyVerificationAsk && ! needsConfirmation {
263+ hostKeyVerification = settings .HostKeyVerificationAcceptNew
264+ }
265+
256266 var targetHost system.System
257267
258268 if opts .TargetHost != "" {
@@ -261,7 +271,7 @@ func applyMain(cmd *cobra.Command, opts *cmdOpts.ApplyOpts) error {
261271 var sshCfg * system.SSHConfig
262272 sshCfg , err = system .NewSSHConfig (stopCtx , opts .TargetHost , log , system.SSHConfigOptions {
263273 AgentManager : sshAgent ,
264- HostKeyVerification : cfg . SSH . HostKeyVerification ,
274+ HostKeyVerification : hostKeyVerification ,
265275 KnownHostsFiles : cfg .SSH .KnownHostsFiles ,
266276 PrivateKeyCmd : cfg .SSH .PrivateKeyCmd ,
267277 })
@@ -335,7 +345,7 @@ func applyMain(cmd *cobra.Command, opts *cmdOpts.ApplyOpts) error {
335345 var sshCfg * system.SSHConfig
336346 sshCfg , err = system .NewSSHConfig (stopCtx , opts .BuildHost , log , system.SSHConfigOptions {
337347 AgentManager : sshAgent ,
338- HostKeyVerification : cfg . SSH . HostKeyVerification ,
348+ HostKeyVerification : hostKeyVerification ,
339349 KnownHostsFiles : cfg .SSH .KnownHostsFiles ,
340350 PrivateKeyCmd : cfg .SSH .PrivateKeyCmd ,
341351 })
0 commit comments