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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/83041a2b-0692-033c-1adc-e9925946b5db%40showmethesource.org.