On Thursday, January 26, 2017 at 7:59:34 AM UTC-8, Jinesh Choksi wrote:
>
> Hi Mark,
>
> I found the following 
> https://relativkreativ.at/articles/how-to-use-ansibles-lineinfile-module-in-a-bulletproof-way
>  
> very useful when I needed to find a reliable way to edit lines in a 
> configuration file with Ansible tasks.
>
> See if it helps you understand how to use lineinfile in a bulletproof way.
>
> Regards,
> Jinesh 
>


    Your article had interesting information, but it doesn't look like it 
will help.  I tried using negative look-ahead, but got nowhere with it.
    Now I am trying to use egrep to scan the file, and then use lineinfile 
to act upon the results.  I put together the below snippet for testing:

  - shell: chdir=~/tmp egrep "^.*\s*{{item}}\s+.*$" mytestfile
    register: result
    changed_when: false
    failed_when: false
    with_items:
        - "string2"
        - "string6"
        - "string7"

  - debug: var={{item}}
    with_items:
        - result

    This returns the following:

TASK [debug] 
*******************************************************************
ok: [localhost] => (item=result) => {
    "item": "result",
    "result": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": false,
                "cmd": "egrep \"^.*\\s*string2\\s+.*$\" mytestfile",
                "delta": "0:00:00.003006",
                "end": "2017-01-26 22:33:27.722164",
                "failed": false,
                "failed_when_result": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "egrep \"^.*\\s*string2\\s+.*$\" 
mytestfile",
                        "_uses_shell": true,
                        "chdir": "/home/pdxmft/tmp",
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "warn": true
                    },
                    "module_name": "command"
                },
                "item": "string2",
                "rc": 0,
                "start": "2017-01-26 22:33:27.719158",
                "stderr": "",
                "stdout": "string1 string2 string3",
                "stdout_lines": [
                    "string1 string2 string3"
                ],
                "warnings": []
            },
            {
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": false,
                "cmd": "egrep \"^.*\\s*string6\\s+.*$\" mytestfile",
                "delta": "0:00:00.003093",
                "end": "2017-01-26 22:33:27.837867",
                "failed": false,
                "failed_when_result": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "egrep \"^.*\\s*string6\\s+.*$\" 
mytestfile",
                        "_uses_shell": true,
                        "chdir": "/home/pdxmft/tmp",
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "warn": true
                    },
                    "module_name": "command"
                },
                "item": "string6",
                "rc": 1,
                "start": "2017-01-26 22:33:27.834774",
                "stderr": "",
                "stdout": "",
                "stdout_lines": [],
                "warnings": []
            },
            {
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": false,
                "cmd": "egrep \"^.*\\s*string7\\s+.*$\" mytestfile",
                "delta": "0:00:00.002986",
                "end": "2017-01-26 22:33:27.951455",
                "failed": false,
                "failed_when_result": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "egrep \"^.*\\s*string7\\s+.*$\" 
mytestfile",
                        "_uses_shell": true,
                        "chdir": "/home/pdxmft/tmp",
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "warn": true
                    },
                    "module_name": "command"
                },
                "item": "string7",
                "rc": 0,
                "start": "2017-01-26 22:33:27.948469",
                "stderr": "",
                "stdout": "string7 string8 string9",
                "stdout_lines": [
                    "string7 string8 string9"
                ],
                "warnings": []
            }
        ]
    }
}


    What I would like to do now is walk through the results[] list and 
extract the "rc" and "item" values, something along the line of this:

  - debug: var={{item.rc}}
    with_items:
        - result.results[*]

    But of course that does not work.  So how can I walk through a list in 
this manner?  I looked at 'with_subelements' but was unable to put 
something together there.
    -Mark
 

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/8533f2c0-19ec-4558-9245-c56e7b935706%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to