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