Hi all,
I am having some difficulty in getting my head around the use of variables, facts and dicts.
The Ansible yum module does not give me output from an equivalent yum repolist -v command, so I am using shell for that.
This is what I am trying to do:
yum repolist -v will give me a list of the enabled repos on a system, for example:
Repo-id : jenkins
Repo-name : Jenkins-stable
Repo-revision: 1585146777
Repo-updated : Wed Mar 25 15:32:57 2020
Repo-pkgs : 102
Repo-size : 6.1 G
Repo-baseurl : http://pkg.jenkins.io/redhat-stable/
Repo-expire : 21,600 second(s) (last: Tue Apr 14 08:28:35 2020)
Filter : read-only:present
Repo-filename: /etc/yum.repos.d/jenkins.repo
Repo-id : mongodb-org-4.2/7
Repo-name : MongoDB Repository
Repo-revision: 1585187010
Repo-updated : Thu Mar 26 02:43:32 2020
Repo-pkgs : 25
Repo-size : 543 M
Repo-baseurl : https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/
Repo-expire : 21,600 second(s) (last: Tue Apr 14 08:28:36 2020)
Filter : read-only:present
Repo-filename: /etc/yum.repos.d/mongodb-org-4.2.repo
I want to:
1: register, lets say for now, the Repo-id, Repo-name, Repo-baseurl, Repo-filename to variable/s, or a dictionary, not sure here ?.
2: Check if the Repo-baseurl of a Repo-id contains a certain url, if it does, register another variable, illegalurl = 1, for that Repo
3: Grab the Repo-filename where the Repo-baseurl illegalurl is 1
4: Do something with that file:
b: Check if a repo file exists in a different location with a correct Repo-baseurl
c: Change the Repo-baseurl in the originating .repo file.
== I guess, if I have the variables, dictionary, facts of the individual blocks of data, then I can use them to do other tasks that I need.
I have tried:
Individual tasks to grab the data and register a variable:
example:
tasks:
- name: Collect Repo id from yum repolist -v
shell: “yum repolist -v | grep Repo-id | awk -F": " ‘{print $2}’”
register: echo_repoids
- debug:
var: echo_repoids.stdout_lines
- name: Collect Repo Name from yum repolist -v
shell: “yum repolist -v | grep Repo-name | awk -F": " ‘{print $2}’”
register: echo_reponame
- debug:
var: echo_reponame.stdout_lines
- name: Collect Repo base url from yum repolist -v
shell: “yum repolist -v | grep Repo-baseurl | awk -F": " ‘{print $2}’”
register: echo_repobaseurl
- debug:
var: echo_repobaseurl.stdout_lines
- name: Collect Repo filename from yum repolist -v
shell: “yum repolist -v | grep Repo-filename | awk -F": " ‘{print $2}’”
register: echo_repofilename
- debug:
var: echo_repofilename.stdout_lines
I also tried:
tasks:
- name: Collect the enabled Repo information
shell: “yum repolist -v | grep {{item}} | awk -F": " ‘{print $2}’”
with_items: - “Repo-id”
- “Repo-name”
- “Repo-baseurl”
- “Repo-filename”
register: output
debug: var=output
But I do not know how to match the blocks contents of Repo-id, Repo-name, Repo-baseurl, Repo-filename as keys and values so I can use them later in the tasks/ playbooks.
I hope that helps you to understand what I am trying to accomplish.
Thanks in advance for any advice anyone can offer.
Best Regards.