ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Ansible facts nested variable syntax preference

    IT Discussion
    ansible yaml ansible facts
    2
    3
    1.2k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • EddieJenningsE
      EddieJennings
      last edited by

      Consider the below example playbook.

      For those that use Ansible regularly, do you have a syntax preference when referencing nested variables within Ansible facts? From what I've gathered there doesn't seem to be a technical difference between these formats:

      ansible_cmdline.BOOT_IMAGE
      ansible_cmdline['BOOT_IMAGE']
      ansible_facts.cmdline.BOOT_IMAGE
      ansible_facts['cmdline']['BOOT_IMAGE']

      The example in the documentation shows using ansible_facts and mentions using either dots or brackets, but it appears just starting with ansible_cmdline in this example appears to work fine.

      ---
      - name: Testing Ansible facts variable syntax
        hosts: ansible-client-5
        become: yes
        tasks:
          - name: Display BOOT_IMAGE using variable name with dots.
            debug:
              msg: "The ansible_cmdline.BOOT_IMAGE is {{ ansible_cmdline.BOOT_IMAGE }}."
          - name: Display BOOT_IMAGE using variable name with brackets.
            debug:
              msg: "The ansible_cmdline['BOOT_IMAGE'] is {{ ansible_cmdline['BOOT_IMAGE'] }}."
          - name: Display BOOT_IMAGE using ansible facts variable with dots.
            debug:
              msg: "The ansible_facts.cmdline.BOOT_IMAGE is {{ ansible_facts.cmdline.BOOT_IMAGE }}."
          - name: Display BOOT_IMAGE using ansible facts variable with brackets.
            debug:
              msg: "The ansible_facts['cmdline']['BOOT_IMAGE'] is {{ ansible_facts['cmdline']['BOOT_IMAGE'] }}."
      ...
      
      
      1 Reply Last reply Reply Quote 1
      • black3dynamiteB
        black3dynamite
        last edited by

        Looks like Bracket notation is best choice instead of dot.

        Bracket notation always works. Dot notation can cause problems because some keys collide with attributes and methods of python dictionaries. Use bracket notation if you use keys which start and end with two underscores (which are reserved for special meanings in python) or are any of the known public attributes:

        EddieJenningsE 1 Reply Last reply Reply Quote 2
        • EddieJenningsE
          EddieJennings @black3dynamite
          last edited by

          @black3dynamite said in Ansible facts nested variable syntax preference:

          Looks like Bracket notation is best choice instead of dot.

          Bracket notation always works. Dot notation can cause problems because some keys collide with attributes and methods of python dictionaries. Use bracket notation if you use keys which start and end with two underscores (which are reserved for special meanings in python) or are any of the known public attributes:

          Overlooked that in the documentation. While it's less efficient to type [' and '] rather than ., I'll live with it. 🙂

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post