Ansible 子文件夹中多个文件中的易变字符串

Ansible 子文件夹中多个文件中的易变字符串,ansible,Ansible,我有一个包含文件和其他文件夹的文件夹。 我在这些文件夹中有一些JSON文件,我需要在所有这些文件夹中将这些文件中的字符串“logs”更改为“LOGINS” 我目前可以在主文件夹中的文件中更改此字符串,但无法在子文件夹中更改此字符串。 我还能够列出所有的文件,即使是在子文件夹中,但我不知道如何更改子文件夹中文件的字符串 这是我目前的剧本: - name: Find all files with extension .json under folder and all sub folders f

我有一个包含文件和其他文件夹的文件夹。
我在这些文件夹中有一些JSON文件,我需要在所有这些文件夹中将这些文件中的字符串“logs”更改为“LOGINS”

我目前可以在主文件夹中的文件中更改此字符串,但无法在子文件夹中更改此字符串。
我还能够列出所有的文件,即使是在子文件夹中,但我不知道如何更改子文件夹中文件的字符串

这是我目前的剧本:

- name: Find all files with extension .json under folder and all sub folders
  find:
    paths: /var/log/conf/login/
    patterns: '*.json'
    recurse: yes

- name: Replace string in files
  replace:
    dest: /var/log/conf/login/ # I have try with path instead dest but nothing cannot make is work
    regexp: 'logs'
    replace: 'LOGINS'
正如您所指出的,该模块能够根据此结果向您提供文件列表,为了对该列表采取行动,您需要注册
查找
模块的结果

- 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
模块生成的所有文件多次执行相同的步骤。
这将通过循环完成:

完整的工作手册插图,包括:

---
- 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   

您好,谢谢您的快速回答,我的问题是我不知道子文件夹的名称,这将由其他开发人员创建,可以是任何名称,所以我需要在文件夹和子文件夹中获取所有具有给定名称的文件,比如app-conf.json,并替换其中的字符串。所以,我需要循环遍历我不知道名称的文件夹。这就是查找所做的,是的。你们测试过了吗?一旦你们从开发者那个里得到了目录名,只需在你们的playbook中添加额外的变量,如下所示。“ansible剧本-e path_loc=“。在此之前,同样的变量需要放在playook的“查找”任务中
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   
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