我有一个.yml文件存在于远程服务器中,我想使用python结构对其进行更改。如果可以与其他python库完成,请随意分享。谢谢
您正在尝试编辑文件中间的一行,这是不可能的。您可以做的是,使用您想要更改的所需值在本地计算机上制作远程文件的副本,然后将其发送回远程服务器。
from fabric import Connection as connection, task
@task
def executeTask(ctx):
with connection(host=dev_server, user=myuser) as c:
c.put('PATH_TO_YOUR_YML_FILE_LOCALLY', 'PATH_TO_YOUR_REMOTE_YML_FILE')
别忘了:
dev_server
和myuser
fabfile.py
的文件中,然后从命令行fab executeTask
运行上面的代码是fabric 2.4
兼容的
编辑:由于权限问题,您可以执行以下操作:
@task
def executeTask(ctx):
with connection(host=dev_server, user=myuser) as c:
c.put("PATH_TO_YOUR_YML_FILE_LOCALLY") # implicit to remote $HOME
c.sudo("mv YOUR_FILE_NAME YOUR_DESIRED_LOCATION") # again implicitly with a CWD of $HOME
c.sudo("chown root:root YOUR_REMOTE_FILE")
参考:qazxsw poi
如果您只需要更改端口号,可以像这样使用https://github.com/fabric/fabric/issues/1750#issuecomment-406043571
sed