1

This is my script:

---
- name: Update Red Hat system and lock version
  hosts: "{{host}}"
  become: true
  tasks:
    - debug: msg="Host is {{ ansible_fqdn }}"
    
    - name: install dnf versionlock plugin
      dnf:
        name: dnf-plugin-versionlock
        state: latest

    - name: unlock all packages
      shell: dnf versionlock delete "*"

    - name: Update all packages
      dnf:
        name: '*'
        state: latest
  
    - name: lock all packages
      shell: dnf versionlock add "*"
        
    - name: send list of locked packages to remote host
      fetch:
        src: /etc/dnf/plugins/versionlock.list
        dest: ansible/inventory/dnf_lockFiles/versionlock.list
        flat: yes

In the last step I would like to create a folder under ansible/inventory/dnf_lockFiles/ that has the name of the inventory file.

For example if the inventory being used has the name test then the path created would look like this: ansible/inventory/dnf_lockFiles/test/versionlock.list.

How do I use the name of the inventory as a variable?

U880D
  • 8,601
  • 6
  • 24
  • 40
  • 3
    What do you mean by _"the inventory name"_? Is it the name of the inventory file? Is it the hostname of that host in the inventory? Else? – β.εηοιτ.βε Mar 12 '23 at 20:34
  • 2
    As reported by β.εηοιτ.βε, there's no such thing as an inventory name as multiple inventory directories containing multiple inventory files can be used at once. The closest you can get is a list of all inventory sources being currently used inside the variable `ansible_inventory_sources` – Zeitounator Mar 12 '23 at 20:55
  • thank you both! it was the name of the inventory file i was after. – The_No1_CousCous Mar 13 '23 at 10:46

1 Answers1

2

Since it seems you are interested in the name of the inventory file, have a look into the following minimal example playbook

---
- hosts: test
  become: false
  gather_facts: false

  vars:

    INVENTORY_FILE_NAME: "{{ ansible_inventory_sources | first | basename }}"

  tasks:

  - debug:
      msg: "{{ INVENTORY_FILE_NAME }}"

resulting into an output of

TASK [debug] ************
ok: [test.example.com] =>
  msg: hosts.ini

for a inventory file of hosts.ini.

To account for the comment from @Zeitounator

The closest you can get is a list of all inventory sources being currently used inside the variable ansible_inventory_sources

the filter | first will provide the first list element, and

... there's no such thing as an inventory name as multiple inventory directories containing multiple inventory files can be used at once

the filter | basename will provide only the filename from that path.

Note: The given example can provide a correct result only if there is no more than one single inventory file.

Further Documentation


In the last step I would like to create a folder under ansible/inventory/dnf_lockFiles/ that has the name of the inventory file.

- name: Create directory
  ansible.builtin.file:
    path: "ansible/inventory/dnf_lockFiles/{{ INVENTORY_FILE_NAME }}"
    state: directory
    mode: 0755

Similar Q&A

U880D
  • 8,601
  • 6
  • 24
  • 40