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



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

即,运行以下命令后,将在命令中指定的编辑器中打开一个临时文件,sudo OC_EDITOR="nano" oc edit configmap/webconsole-config -n openshift-web-console

请注意,每次运行命令时,都会在新的临时文件中打开内容。一旦更新了更改,文件就会保存到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
.
.
.

这里stylesheetURLs需要在文件中更新如下:

.
.
scriptURLs: []
stylesheetURLs:
- http://127.0.0.1:30296/css/logo.css
features:
clusterResourceOverridesEnabled: false
.
.
.

这里,stylesheetURLs需要用如上所述的缩进来更新,并且需要保留其他内容的缩进。

如何在易理解的剧本中实现这一点?

附加信息:这是为了更新okd/openshift 3.11的网络控制台徽标,参考:https://docs.okd.io/3.11/install_config/web_console_customization.html

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

你会发现一整套ansible机制,允许你以非常精确的方式更改文本文件的内容,所以只需在你的剧本中复制,不需要"纳米">

- set_fact:
my_temp_path: /tmp/some-random-filename.yaml
- shell: >-
oc get -o yaml -n openshift-web-console 
configmap/webconsole-config >{{ my_temp_path }}
- lineinfile:
path: '{{ my_temp_path }}'
# whatever else
- command: oc -n openshift-web-console apply -f {{ my_temp_path }}
- file:
path: '{{ my_temp_path }}'
state: absent

最新更新