首页 > 解决方案 > 木偶:“错误:无法从远程服务器检索目录”将代码添加到配置文件后

问题描述

我是puppet新手。

我有一个功能puppet安装(主和代理)。

我需要更新配置文件以检查两者certbot.timernginxsystemd 服务是否都处于活动状态;如果是,添加renew_hook = service nginx reload/etc/letsencrypt/renewal/*.conf文件中。

现在,我尝试将这些片段添加到现有配置文件中(一次一个):

exec { 'certbot_nginx' :
  command => 'for file in $(find /etc/letsencrypt/renewal -type f); do if [ "$(grep "renew_hook = service nginx reload" $file)" != "renew_hook = service nginx reload" ]; then sed -i 's/\(\[renewalparams\]\)/\1\nrenew_hook = service nginx reload/' $file; fi; done',
  onlyif  => 'systemctl -q is-active certbot.timer && systemctl -q is-active nginx',
}
exec { "bash -c "if [ \"$(systemctl -q is-active certbot.timer && echo $?)\" = \"0\" ] && [ \"$(systemctl -q is-active nginx && echo $?)\" = \"0\" ]; then for file in $(find /etc/letsencrypt/renewal -type f); do if [ \"$(grep \"renew_hook = service nginx reload\" $file)\" != \"renew_hook = service nginx reload\" ]; then sed -i 's/\(\[renewalparams\]\)/\1\nrenew_hook = service nginx reload/' $file; fi; done; fi"" :
  provider => shell,
  command => 'bash -c "if [ \"$(systemctl -q is-active certbot.timer && echo $?)\" = \"0\" ] && [ \"$(systemctl -q is-active nginx && echo $?)\" = \"0\" ]; then for file in $(find /etc/letsencrypt/renewal -type f); do if [ \"$(grep \"renew_hook = service nginx reload\" $file)\" != \"renew_hook = service nginx reload\" ]; then sed -i 's/\(\[renewalparams\]\)/\1\nrenew_hook = service nginx reload/' $file; fi; done; fi"',
}

无论我做什么,我都会收到此错误(仅当这些片段在配置文件中时;否则puppet agent -t将成功运行):

# puppet agent -t
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Syntax error at 's' at /some/path/profile.pp:19:186 on node my.server.com
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

旁注

(1)傀儡代理版本:

# puppet --version
5.4.0

(2)傀儡服务器版本:

# puppetserver --version
puppetserver version: 2.8.0

(3) 服务器操作系统:Ubuntu 16.04.3 LTS

(4) 客户端操作系统:(Debian 9我也遇到了错误Debian 10。)

标签: bashpuppet

解决方案


尝试(也在@john bollinger的评论中提供):

exec { 'certbot_nginx' :
  command => 'for file in $(find /etc/letsencrypt/renewal -type f); do if [ "$(grep "renew_hook = service nginx reload" $file)" != "renew_hook = service nginx reload" ]; then sed -i "s/\(\[renewalparams\]\)/\1\nrenew_hook = service nginx reload/" $file; fi; done',
  onlyif  => 'systemctl -q is-active certbot.timer && systemctl -q is-active nginx',
}

和:

exec { 'renew_hook = service nginx reload' :
  provider => shell,
  command => 'bash -c "if [ \"$(systemctl -q is-active certbot.timer && echo $?)\" = \"0\" ] && [ \"$(systemctl -q is-active nginx && echo $?)\" = \"0\" ]; then for file in $(find /etc/letsencrypt/renewal -type f); do if [ \"$(grep \"renew_hook = service nginx reload\" $file)\" != \"renew_hook = service nginx reload\" ]; then sed -i "s/\(\[renewalparams\]\)/\1\nrenew_hook = service nginx reload/" $file; fi; done; fi"',
}

这意味着您有一个字符串,例如:

'some command; sed 's/x/y/''

有关puppet lang 数据字符串的更多文档

在 Puppet 语言中有四种编写文字字符串的方法:

  • 空话
  • 单引号字符串
  • 双引号字符串
  • Heredocs

这意味着您的字符串将在第二个之后终止'


推荐阅读