ansible - Ansible:如何在运行命令时更新在 vim 编辑器中打开的文件
问题描述
我需要使用 ansible 更新文件。在手动更新过程中,当运行编辑命令时,文件会在 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
需要用上面提到的缩进更新,其他内容的缩进需要保留。
如何在 ansible playbook 中实现这一点?
附加信息:这样做的目的是更新 okd/openshift 3.11 的 webconsole 徽标,参考:https ://docs.okd.io/3.11/install_config/web_console_customization.html
解决方案
你问错问题了。目标不是“我如何禁用$EDITOR
”,而是“我如何编辑配置然后重新配置apply
它们”,这正是对您有用的:kubectl edit
oc 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
推荐阅读
- laravel - 在 cpanel 中找到基于 laravel 和 voyager 构建的网站的主网站(首页)文件
- html - 标签的含义
- css-selectors - 如何找到href/a/button的css选择器
- linux - 如何在树莓派 3b+ 中同时使用静态以太网 ip 和 Wi-Fi
- javascript - 如何从包含多个对象数组的 Json 数据中提取多个对象数组?
- html - Selenium html.text 返回不完整的结果
- php - 登录后如何自定义重定向
- ruby - 尝试在 Ruby 中实例化自定义对象时抛出(参数错误)
- r - 随机游走自相关函数的 3D 图
- symfony - easyadmin 实体字段的动态自定义选择