我有一个包含文件以及其他文件夹的文件夹。
我在这些文件夹中有一些JSON文件,我需要在所有这些文件夹中将这些文件中的字符串“ logs ”更改为“ LOGINS ”。>
我目前能够在主文件夹中的文件中更改此字符串,但无法在子文件夹中进行更改。
即使在子文件夹中,我也能够列出所有文件,但是我不知道如何更改子文件夹中文件的字符串。
这是我当前的剧本:
if (it == StreamState.STOPPED) {
this.stopForeground(false)
}
else if (it == StreamState.STARTED) {
val channelId = "media_playback_channel"
exoNotificationListener = ExoNotificationListener(onNotificationPosted, onNotificationCancelled)
playerNotificationManager = playerNotificationManager ?: PlayerNotificationManager.createWithNotificationChannel(
application.applicationContext,
channelId,
R.string.media_playback_notification,
R.string.media_playback_notification_id,
mcNotificationManager,
exoNotificationListener
)
playerNotificationManager?.setFastForwardIncrementMs(0)
playerNotificationManager?.setRewindIncrementMs(0)
playerNotificationManager?.setUseNavigationActions(false)
// Here I am setting the color which works fine when content is playing.
playerNotificationManager?.setColor(notificationColor)
bindPlayer(playerNotificationManager)
}
答案 0 :(得分:0)
find
模块能够根据您的指示为您提供文件列表,根据此结果,要对该列表进行操作,您需要register
的结果find
模块。
- name: Find all files with extension .json under folder and all sub folders
find:
paths: /var/log/conf/login/
patterns: '*.json'
recurse: yes
register: files_to_change
然后,在注册了此文件列表之后,您可以对find
模块产生的所有文件创建循环并多次执行相同的步骤。
这可以通过with_items
循环来完成:
- name: Replace string in files
replace:
dest: "{{ item.path }}"
regexp: 'logs'
replace: 'LOGINS'
with_items: "{{ files_to_change.files }}"
完整的剧本说明如下:
---
- hosts: localhost
connection: local
tasks:
######
# Mind that this step is just there to make this example relevant,
# I am creating folders, as your users would do, here
######
- name: Creating folders structure
file:
dest: "{{ item }}"
state: directory
with_items:
- /var/log/conf/login/some
- /var/log/conf/login/sub
- /var/log/conf/login/path/to
######
# Mind that this step is just there to make this example relevant,
# I am just creating some JSON files, here, as your users would do
######
- name: Creating file with the content to replace
copy:
dest: "{{ item }}"
content: "The quick brown fox jumps over the lazy logs ;)"
with_items:
- /var/log/conf/login/some/test.json
- /var/log/conf/login/sub/hello.json
- /var/log/conf/login/path/to/file.json
- name: Find all files with extension .json under folder and all sub folders
find:
paths: /var/log/conf/login/
patterns: '*.json'
recurse: yes
register: files_to_change
######
# Mind that this is looping on the result of the find module,
# so it is fully dynamic based on the folders that your end-users might create
######
- name: Replace string in files
replace:
dest: "{{ item.path }}"
regexp: 'logs'
replace: 'LOGINS'
loop_control:
label: "{{ item.path }}"
with_items: "{{ files_to_change.files }}"
- name: Display files content
debug:
msg: "{{ lookup('file', item.path) }}"
loop_control:
label: "{{ item.path }}"
with_items: "{{ files_to_change.files }}"
这将输出:
PLAY [localhost] **************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************
ok: [localhost]
TASK [Creating folders structure] *********************************************************************************************************************************************************
changed: [localhost] => (item=/var/log/conf/login/some)
changed: [localhost] => (item=/var/log/conf/login/sub)
changed: [localhost] => (item=/var/log/conf/login/path/to)
TASK [Creating file with the content to replace] ******************************************************************************************************************************************
changed: [localhost] => (item=/var/log/conf/login/some/test.json)
changed: [localhost] => (item=/var/log/conf/login/sub/hello.json)
changed: [localhost] => (item=/var/log/conf/login/path/to/file.json)
TASK [Find all files with extension .json under folder and all sub folders] ***************************************************************************************************************
ok: [localhost]
TASK [Replace string in files] ************************************************************************************************************************************************************
changed: [localhost] => (item=/var/log/conf/login/path/to/file.json)
changed: [localhost] => (item=/var/log/conf/login/sub/hello.json)
changed: [localhost] => (item=/var/log/conf/login/some/test.json)
TASK [Display files content] **************************************************************************************************************************************************************
ok: [localhost] => (item=/var/log/conf/login/path/to/file.json) => {
"msg": "The quick brown fox jumps over the lazy LOGINS ;)"
}
ok: [localhost] => (item=/var/log/conf/login/sub/hello.json) => {
"msg": "The quick brown fox jumps over the lazy LOGINS ;)"
}
ok: [localhost] => (item=/var/log/conf/login/some/test.json) => {
"msg": "The quick brown fox jumps over the lazy LOGINS ;)"
}
PLAY RECAP ********************************************************************************************************************************************************************************
localhost : ok=6 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
答案 1 :(得分:0)
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": [
{
"atime": 1569861712.723093,
"ctime": 1569861712.723093,
"dev": 104,
"gid": 0,
"inode": 5268,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0755",
"mtime": 1569861712.723093,
"nlink": 1,
"path": "/test/configuration/nginx/trials/Zvone/trial-admin-app/app-config.json",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 12,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": true,
"xoth": true,
"xusr": true
},
{
"atime": 1569861712.7270927,
"ctime": 1569861712.7270927,
"dev": 104,
"gid": 0,
"inode": 5271,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0755",
"mtime": 1569861712.7270927,
"nlink": 1,
"path": "/test/configuration/nginx/trials/Zvone/trial-admin-app/Test1/app-config.json",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 12,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": true,
"xoth": true,
"xusr": true
},
{
"atime": 1569861712.7320926,
"ctime": 1569861712.7320926,
"dev": 104,
"gid": 0,
"inode": 5274,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0755",
"mtime": 1569861712.7320926,
"nlink": 1,
"path": "/test/configuration/nginx/trials/Zvone/trial-admin-app/Test2/app-config.json",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 12,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": true,
"xoth": true,
"xusr": true
}
]
}
TASK [Replace string inside file] **********************************************
failed: [localhost] (item={u'uid': 0, u'woth': False, u'mtime': 1569861712.723093, u'inode': 5268, u'isgid': False, u'size': 12, u'roth': True, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': True, u'rusr': True, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/test/configuration/nginx/trials/Zvone/trial-admin-app/app-config.json', u'xusr': True, u'atime': 1569861712.723093, u'isdir': False, u'ctime': 1569861712.723093, u'isblk': False, u'xgrp': True, u'dev': 104, u'wgrp': False, u'isfifo': False, u'mode': u'0755', u'islnk': False}) => {"failed": true, "item": {"atime": 1569861712.723093, "ctime": 1569861712.723093, "dev": 104, "gid": 0, "inode": 5268, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0755", "mtime": 1569861712.723093, "nlink": 1, "path": "/test/configuration/nginx/trials/Zvone/trial-admin-app/app-config.json", "rgrp": true, "roth": true, "rusr": true, "size": 12, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": true, "xoth": true, "xusr": true}, "msg": "Destination /test/configuration/nginx/trials/Zvone/trial-admin-app/.app-config.json does not exist !", "rc": 257}
failed: [localhost] (item={u'uid': 0, u'woth': False, u'mtime': 1569861712.7270927, u'inode': 5271, u'isgid': False, u'size': 12, u'roth': True, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': True, u'rusr': True, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/test/configuration/nginx/trials/Zvone/trial-admin-app/Test1/app-config.json', u'xusr': True, u'atime': 1569861712.7270927, u'isdir': False, u'ctime': 1569861712.7270927, u'isblk': False, u'xgrp': True, u'dev': 104, u'wgrp': False, u'isfifo': False, u'mode': u'0755', u'islnk': False}) => {"failed": true, "item": {"atime": 1569861712.7270927, "ctime": 1569861712.7270927, "dev": 104, "gid": 0, "inode": 5271, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0755", "mtime": 1569861712.7270927, "nlink": 1, "path": "/test/configuration/nginx/trials/Zvone/trial-admin-app/Test1/app-config.json", "rgrp": true, "roth": true, "rusr": true, "size": 12, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": true, "xoth": true, "xusr": true}, "msg": "Destination /test/configuration/nginx/trials/Zvone/trial-admin-app/.app-config.json does not exist !", "rc": 257}
failed: [localhost] (item={u'uid': 0, u'woth': False, u'mtime': 1569861712.7320926, u'inode': 5274, u'isgid': False, u'size': 12, u'roth': True, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': True, u'rusr': True, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/test/configuration/nginx/trials/Zvone/trial-admin-app/Test2/app-config.json', u'xusr': True, u'atime': 1569861712.7320926, u'isdir': False, u'ctime': 1569861712.7320926, u'isblk': False, u'xgrp': True, u'dev': 104, u'wgrp': False, u'isfifo': False, u'mode': u'0755', u'islnk': False}) => {"failed": true, "item": {"atime": 1569861712.7320926, "ctime": 1569861712.7320926, "dev": 104, "gid": 0, "inode": 5274, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0755", "mtime": 1569861712.7320926, "nlink": 1, "path": "/test/configuration/nginx/trials/Zvone/trial-admin-app/Test2/app-config.json", "rgrp": true, "roth": true, "rusr": true, "size": 12, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": true, "xoth": true, "xusr": true}, "msg": "Destination /test/configuration/nginx/trials/Zvone/trial-admin-app/.app-config.json does not exist !", "rc": 257}
NO MORE HOSTS LEFT *************************************************************
to retry, use: --limit @./releases/1.0-SNAPSHOT/ansible/newtest.retry
PLAY RECAP *********************************************************************
localhost : ok=16 changed=10 unreachable=0 failed=1