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

On Mon, Sep 27, 2021 at 5:07 PM Sandip Bhattacharya <
sand...@showmethesource.org> wrote:

> 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.
>
>    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
>
> 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,
>    Sandip
>
>
>
>
>
> On 28.09.21 00:03, Alex Wanderley wrote:
> > 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:
> > ---
> >      - hosts: "{{ my_host }}"
> >        remote_user: xxxxxx
> >
> >        vars:
> >           server:
> >               srvname: "{{ ansible_hostname }}"
> >               kernel: "{{ ansible_kernel }}"
> >
> >        tasks:
> >
> >          - block:
> >              - name: create list
> >                set_fact:
> >                   serverlist: "{{ serverlist | default({}) | combine(
> {item.srvname: item.kernel} ) }}"
> >                with_items:
> >                     - "{{ server }}"
> >
> >              - name: print list
> >                debug:
> >                   msg: "{{ serverlist }}"
> >
> >              - name: send list to file
> >                lineinfile:
> >                   create: yes
> >                   line: "{{ serverlist | string }}"
> >                   path: "/shared/kernel.list"
> >            when: (ansible_distribution == "OracleLinux") and
> (hostvars[inventory_hostname].ansible_kernel !=
> "4.18.0-305.10.2.el8_4.x86_64")
> >
> > It's working, but I'd like to improve the way it presents the list of
> servers in need of update... So, could you help with some hints on how I
> could:
> >
> > Make the "print list" task work in a way that instead of printing this:
> > TASK [print list]
> **************************************************************************
> > ok: [xxxxxxxx] => {
> >      "msg": {
> >          "xxxxxxxxx": "4.18.0-240.10.1.el8_3.x86_64"
> >      }
> > }
> > ok: [yyyyyyyy] => {
> >      "msg": {
> >          "yyyyyyyy": "4.18.0-240.10.1.el8_3.x86_64"
> >      }
> > ===> It would print something like (or closer to) this:
> > "msg": {
> > "xxxxxxxxx": "4.18.0-240.10.1.el8_3.x86_64'
> > "yyyyyyyy": "4.18.0-240.10.1.el8_3.x86_64
> > }
> >
> > And, on the "send list to file" task, how would I have the file content
> changed from this:
> > {'xxxxxxxxx ': '4.18.0-240.10.1.el8_3.x86_64'}
> > {' yyyyyyyy ': '4.18.0-240.10.1.el8_3.x86_64'}
> > ===> To something like this:
> > xxxxxxxxx: 4.18.0-240.10.1.el8_3.x86_64
> > {yyyyyyyy: 4.18.0-240.10.1.el8_3.x86_64
> > I mean, without single quotes and curly braces.
> >
> > I understand I could build another task to parse that file an "clean"
> it, but, is there a way to do that while writing to the file?
>
>
> --
> https://blog.sandipb.net
> https://twitter.com/sandipb
>
> --
> 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 ansible-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/83041a2b-0692-033c-1adc-e9925946b5db%40showmethesource.org
> .
>


-- 



[image: Edmonton_sig_RGB_S.jpg]

Alex Wanderley

Application and Infrastructure Analyst II
Acting Team Lead Storage and Backup Solutions

Financial and Corporate Services | Open City and Technology



780-496-4156  Office

780-819-0273  Mobile



City of Edmonton

Century Place, 12th Floor

9803 102A Avenue NW

Edmonton AB, T5J 3A3



All information contained in this email post is proprietary to the City of
Edmonton, confidential and intended only for the addressed recipient. If
you have received this post in error, please disregard the contents, inform
the sender of the misdirection, and remove it from your system. The
copying, dissemination or distribution of this email, if misdirected, is
strictly prohibited.

-- 
*The contents of this message and any attachment(s) are confidential, 
proprietary to the City of Edmonton, and are intended only for the 
addressed recipient. If you have received this in error, please disregard 
the contents, inform the sender of the misdirection, and remove it from 
your system. The copying, dissemination, or distribution of this message, 
if misdirected, is strictly prohibited.*

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CADp8UUSCeLezpQ1%3DC2Leg8XYwiRzbW6r1X18Lg992xZKwqfH4A%40mail.gmail.com.

Reply via email to