Ansible:如何在运行命令时更新在vim编辑器中打开的文件

Ansible:如何在运行命令时更新在vim编辑器中打开的文件,ansible,openshift,openshift-origin,okd,Ansible,Openshift,Openshift Origin,Okd,我需要用ansible更新一个文件。在手动更新过程中,当运行edit命令时,文件将在vim、nano等编辑器中打开,在那里更新和保存更改 i、 e, 运行以下命令后,将在命令中指定的编辑器中打开临时文件, sudo OC_EDITOR=“nano”OC edit configmap/webconsole config-n openshift web控制台 请注意,每次运行命令时,内容都会在新的临时文件中打开。更新更改后,文件将保存到docker容器中 由于在上述命令中,编辑器被指定为nano,因

我需要用ansible更新一个文件。在手动更新过程中,当运行edit命令时,文件将在vim、nano等编辑器中打开,在那里更新和保存更改

i、 e, 运行以下命令后,将在命令中指定的编辑器中打开临时文件,
sudo OC_EDITOR=“nano”OC edit configmap/webconsole config-n openshift web控制台

请注意,每次运行命令时,内容都会在新的临时文件中打开。更新更改后,文件将保存到docker容器中

由于在上述命令中,编辑器被指定为
nano
,因此文件内容在nano编辑器中打开,内容如下:

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
apiVersion: v1
data:
  webconsole-config.yaml: |
    apiVersion: webconsole.config.openshift.io/v1
    clusterInfo:
      adminConsolePublicURL: https://console.router.default.svc.cluster.local/
      consolePublicURL: https://master.novalocal:8443/console/
      masterPublicURL: https://master.novalocal:8443
    extensions:
      properties: {}
      scriptURLs: []
      stylesheetURLs:[]
    features:
      clusterResourceOverridesEnabled: false
      inactivityTimeoutMinutes: 0
.
.
.
.
.
      scriptURLs: []
      stylesheetURLs:
      - http://127.0.0.1:30296/css/logo.css
    features:
      clusterResourceOverridesEnabled: false
.
.
.

此处需要在文件中更新
样式表URL
,如下所示:

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
apiVersion: v1
data:
  webconsole-config.yaml: |
    apiVersion: webconsole.config.openshift.io/v1
    clusterInfo:
      adminConsolePublicURL: https://console.router.default.svc.cluster.local/
      consolePublicURL: https://master.novalocal:8443/console/
      masterPublicURL: https://master.novalocal:8443
    extensions:
      properties: {}
      scriptURLs: []
      stylesheetURLs:[]
    features:
      clusterResourceOverridesEnabled: false
      inactivityTimeoutMinutes: 0
.
.
.
.
.
      scriptURLs: []
      stylesheetURLs:
      - http://127.0.0.1:30296/css/logo.css
    features:
      clusterResourceOverridesEnabled: false
.
.
.

这里,
stylesheeturl
需要如上所述使用缩进进行更新,并且需要保留其他内容的缩进

如何在ansible剧本中实现这一点


其他信息:此操作的目的是更新okd/openshift 3.11的webconsole徽标,参考:

您问错了问题。目标不是“如何禁用
$EDITOR
”,而是“如何编辑配置,然后重新应用它们,”这正是
kubectl edit
oc edit
为您所做的:
oc get-o yaml-n openshift web控制台configmap/webconsole config>$TMPDIR/some-file.yaml&$EDITOR$TMPDIR/some-file.yaml&&oc-n openshift web控制台apply-f$TMPDIR/some-file.yaml&&rm$TMPDIR/some-file.yaml

您将发现一整套ansible机制,允许您以非常精确的方式更改文本文件的内容,因此只需在您的剧本中复制,无需“nano”

-设置事实:
我的临时路径:/tmp/some-random-filename.yaml
-外壳:>-
oc get-o yaml-n openshift web控制台
configmap/webconsole配置>{my_temp_path}
-线条填充:
路径:“{my_temp_path}”
#还有别的吗
-命令:oc-n openshift web控制台apply-f{{my\u temp\u path}
-文件:
路径:“{my_temp_path}”
国家:缺席

你问错了问题。目标不是“如何禁用
$EDITOR
”,而是“如何编辑配置,然后重新应用它们,”这正是
kubectl edit
oc edit
为您所做的:
oc get-o yaml-n openshift web控制台configmap/webconsole config>$TMPDIR/some-file.yaml&$EDITOR$TMPDIR/some-file.yaml&&oc-n openshift web控制台apply-f$TMPDIR/some-file.yaml&&rm$TMPDIR/some-file.yaml

您将发现一整套ansible机制,允许您以非常精确的方式更改文本文件的内容,因此只需在您的剧本中复制,无需“nano”

-设置事实:
我的临时路径:/tmp/some-random-filename.yaml
-外壳:>-
oc get-o yaml-n openshift web控制台
configmap/webconsole配置>{my_temp_path}
-线条填充:
路径:“{my_temp_path}”
#还有别的吗
-命令:oc-n openshift web控制台apply-f{{my\u temp\u path}
-文件:
路径:“{my_temp_path}”
国家:缺席