# List manipulation questions

**URL:** <https://forum.ansible.com/t/list-manipulation-questions/35790>\
**Category:** Ansible Project\
**Created:** [September 27, 2021, 10:03pm UTC](https://forum.ansible.com/t/list-manipulation-questions/35790 "2021-09-27T22:03:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [September 27, 2021, 10:03pm UTC](https://forum.ansible.com/t/list-manipulation-questions/35790/1 "2021-09-27T22:03:51Z")

</div>

Hello,

I understand this very basic, but I’m still learning… (And my apologies for the long message.)

I built the playbook below so I can check which server is still in need of an OS update based on its current kernel version:

---

<div class="post-metadata">

**Author:** ![Sandip\_Bhattacharya](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Sandip\_Bhattacharya](https://forum.ansible.com/u/Sandip_Bhattacharya)\
**Post date:** [September 27, 2021, 11:07pm UTC](https://forum.ansible.com/t/list-manipulation-questions/35790/2 "2021-09-27T23:07:20Z")

</div>

Hi,  
It would be helpful to us if you could explain a bit more clearly about:

a. What you want to do here  
b. What are all the inputs here

Questions:

- What are the variables 'my\_host' and 'server' here?  
- Do you want a file created on every host? Or only on the control-host?

In any case, using an ansible playbook for the kind of reporting you are trying  
to do, seems a bit of an overkill.

If you do want to use ansible as a remote execution tool, it is easier to  
just use the 'ansible' command to print what you want, and post-process the output.

&nbsp;&nbsp;&nbsp;ansible all\_hosts -m setup -a "filter=ansible\_kernel"

If you really want a file created in one place in a playbook, use  
"connection: local" in your lineinfile so that the file is created on your  
local machine, and make sure "throttle: 1" is also set, so that it is  
not clobbered by other threads.

Something like this would work:  
[https://gist.github.com/sandipb/8ad80e6af9d471b04e2ec7948224ff3d](https://gist.github.com/sandipb/8ad80e6af9d471b04e2ec7948224ff3d)

Also, when using lineinfile, it is really important that you make it as much  
idempotent as possible. Else, if you run your playbook on different days, it  
would be filled with all the possible values of every host all this while. The  
regex parameter is really important here, making sure that every host has  
exactly one line, or none if it is not desired.

HTH,  
&nbsp;&nbsp;&nbsp;Sandip

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [September 28, 2021, 3:02pm UTC](https://forum.ansible.com/t/list-manipulation-questions/35790/3 "2021-09-28T15:02:34Z")

</div>

Hi Sandip,

What I want is to print/create a clean, well formatted list server\_name:kernel\_version of servers that are not at a certain kernel version (that’s why “when” is excluding “4.18.0-305.10.2.el8\_4.x86\_64”).

The input is whichever server name, list of servers, inventory group that I pass on as a parameter to the playbook by using the “my\_host” variable (it was a poorly chosen variable name, I admit… 🙂 ).  
This is how the playbook is called: playbook.yml -e “my\_host=\<server|server\_list|inventory\_group\>”.

After looking again, “server” is actually unnecessary. I should just go with: serverlist: “{{ serverlist | default({}) | combine( {ansible\_hostname: ansible\_kernel} ) }}”.

I know I can create that same list using something else like shell script, which I’m much more proficient at. The idea of using ansible came as a way of learning the basics of working with lists using a very basic example.

“lineinfile” will keep appending to that file indefinitely, I know. But my real intention was learning how to write those lines the way I want.

That all being said, thank you very much for taking the time to help/advise/question… (Questioning leads to better/more thinking.)  
I realized that it will be much better if I read and practice more on my own and leave to consume people’s time when I’m in trouble with a real-life playbook.

All the best,

Alex

---

<div class="post-metadata">

**Author:** ![Sandip\_Bhattacharya](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Sandip\_Bhattacharya](https://forum.ansible.com/u/Sandip_Bhattacharya)\
**Post date:** [September 28, 2021, 3:12pm UTC](https://forum.ansible.com/t/list-manipulation-questions/35790/4 "2021-09-28T15:12:26Z")

</div>

Hi Alex,

One additional tip.

The best way to target is using the inventory names. You can use hostname or a group name in the "hosts: ", but we generally don’t use a variable there.

If you have specified a group in the "hosts: " field, and then at the command line you want to restrict the playbook to a single host, you can use the “–limit” parameter to specify the specific host.

Thanks,  
Sandip
