How to get the first element of a list from the output of setup module in Ansible?

前端 未结 2 640
轻奢々
轻奢々 2020-11-30 13:30

I received the following data from the setup module:

\"ansible_nodename\": \"3d734bc2a391\",
\"ansible_os_family\": \"RedHat\",
\"ansible_pkg_mgr\": \"yum\",         


        
相关标签:
2条回答
  • 2020-11-30 14:05

    To get first item of the list:

    - debug:
        msg: "First item: {{ ansible_processor[0] }}"
    

    Or:

    - debug:
        msg: "First item: {{ ansible_processor | first }}"
    
    0 讨论(0)
  • 2020-11-30 14:11

    Try this for common handle this situation:

    Ref: get-first-n-elements-of-a-list-in-jinja2-template-in-ansible

    # from list
    - debug:
        msg: "First item: {{ ansible_processor[0] }}"
    # from output, like 'https://xxx.xx/xxx/xxx.git'
    - debug:
        msg: "git repo's name: {{ (item| urlsplit('path')| basename | splitext)[0] }}"
    
    0 讨论(0)
提交回复
热议问题