How to add os based variables into group_vars

31 views
Skip to first unread message

sriman dharba

unread,
Feb 1, 2018, 2:10:29 PM2/1/18
to ansible...@googlegroups.com
Hello,

I wanted to manage repo names from group_vars based on OS type. I have a requirement to set reponames for different OS & versions like Centos/Redhat/Ubuntu with all stable versions. The goal is to manage future repo names from group_vars

My variables looks like this

group_vars/all.yml

---
rhel_5_repo: redhat-5-rpms-stable <-- name of the repo which i will use to install latest kernel patches
rhel_6_repo: redhat-6-rpms-stable
rhel_7_repo: redhat-7-rpms-stable
ubuntu_12_repo: ubuntu-12-deb-stable

I would like to use use repo keys into roles based on fact  ansible_distribution_major_version and fetch the variable from group_vars

- set_fact: repo_name="rhel_{{ ansible_distribution_major_version }}_repo"

- name: 'Upgrading packages to latest'
  yum:
     name: "*"
     state: latest
     enablerepo: "{{ repo_name }}"
  when: ansible_distribution == "RedHat" or ansible__distribution == "CentOS"

How can i achieve this..Please suggest.

Thanks
Sri

Kai Stian Olstad

unread,
Feb 1, 2018, 2:22:50 PM2/1/18
to ansible...@googlegroups.com
On Wednesday, 31 January 2018 05.43.36 CET sriman dharba wrote:
> - set_fact: repo_name="rhel_{{ ansible_distribution_major_version }}_repo"
>
>
> - name: 'Upgrading packages to latest'
> yum:
> name: "*"
> state: latest
> enablerepo: "{{ repo_name }}"
> when: ansible_distribution == "RedHat" or ansible__distribution ==
> "CentOS"

- name: 'Upgrading packages to latest'
yum:
name: "*"
state: latest
enablerepo: "{{ vars[repo_name] }}"
when: ansible_distribution == "RedHat" or ansible__distribution == "CentOS"

or just drop the set_fact and use

- name: 'Upgrading packages to latest'
yum:
name: "*"
state: latest
enablerepo: "{{ vars['repo_' ~ ansible_distribution_major_version ~ '_repo'] }}"
when: ansible_distribution == "RedHat" or ansible__distribution == "CentOS"


--
Kai Stian Olstad

sriman dharba

unread,
Feb 2, 2018, 1:51:53 AM2/2/18
to ansible...@googlegroups.com
Thanks Kai! Came across vars & hostvars from FAQ section of ansible. Missed it, thanks for the response.

Regards
Sri

--
Kai Stian Olstad

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/12765257.BGxFR1VXkv%40x1.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages