satya
satya

Reputation: 131

Need to find conf file on the system and need to replace a line

Everyone, I am using the below script to find all the httpd.conf file in /tmp using the find module and after that, i need to change the server root for all the httpd.conf files that I got from the find module. I am using the Below code. getting the below error while executing the lineinline module.

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Destination {'files': [{'uid': 0, 'woth': False, 'mtime': 1554392266.9903164, 'inode': 4232796, 'isgid': False, 'size': 11753, 'roth': True, 'isuid': False, 'isreg': True, 'pw_name': 'root', 'gid': 0, 'ischr': False, 'wusr': True, 'xoth': False, 'rusr': True, 'nlink': 1, 'issock': False, 'rgrp': True, 'gr_name': 'root', 'path': '/tmp/httpd.conf', 'xusr': False, 'atime': 1554391744.8432574, 'isdir': False, 'ctime': 1554392266.9903164, 'wgrp': False, 'xgrp': False, 'dev': 51714, 'isblk': False, 'isfifo': False, 'mode': '0644', 'islnk': False}], 'changed': False, 'failed': False, 'examined': 14, 'msg': '', 'matched': 1} does not exist !", "rc": 257} to retry, use: --limit @/etc/ansible/findnew.retry

---
- name: Recursively find httpd.conf file in /tmp 
  connection: local
  hosts: localhost
  tasks:
   - find:
       paths: /tmp
       patterns: '*.conf'
       recurse: yes
     register: filestoser
   - debug: var=filestoser
   - lineinfile:
       path: '{{ filestoser }}'
       state: present
       regexp: '^ServerRoot'
       line: 'ServerRoot_new'

Upvotes: 0

Views: 353

Answers (1)

larsks
larsks

Reputation: 311238

Your problem is that you're trying to use the variable filestoser as a filename, but it's not: it's the result of the find task. If you take a look at the output of your debug task you'll see something like this:

TASK [debug] **********************************************************************************
ok: [localhost] => {
    "filestoser": {
        "changed": false,
        "examined": 44,
        "failed": false,
        "files": [
            {
                "atime": 1554394659.885133,
                "ctime": 1554394659.885133,
                "dev": 45,
                "gid": 21937,
                "gr_name": "lars",
                "inode": 172846,
                "isblk": false,
                "ischr": false,
                "isdir": false,
                "isfifo": false,
                "isgid": false,
                "islnk": false,
                "isreg": true,
                "issock": false,
                "isuid": false,
                "mode": "0644",
                "mtime": 1554394659.885133,
                "nlink": 1,
                "path": "/tmp/etc/httpd/httpd.conf",
                "pw_name": "lars",
                "rgrp": true,
                "roth": true,
                "rusr": true,
                "size": 11753,
                "uid": 21937,
                "wgrp": false,
                "woth": false,
                "wusr": true,
                "xgrp": false,
                "xoth": false,
                "xusr": false
            }
        ],
        "matched": 1,
        "msg": ""
    }
}

In other words, filestoser is a dictionary. The files key contains a list of files that were matched by the files task. You could rewrite your lineinfile task like this, if you want to operate on the first file found:

- lineinfile:
    path: '{{ filestoser.files.0.path }}'
    state: present
    regexp: '^ServerRoot'
    line: 'ServerRoot_new'

Upvotes: 2

Related Questions