首页 > 解决方案 > Formatting set_fact variable

问题描述

I need to have all the userid in a single variable, all separated by \n. Code is as below.

  - name: Retrieve the user id and instance
    shell: ls -l {{item}} | grep -v total| awk '{print $3}'
    register: find_result_userid
    with_items:
     - /tmp/log/logs/log1
     - /tmp/log/logs/log2
     - /tmp/log/logs/log3


  - name: Combine all userid
    set_fact:
     server_names: "{{ find_result_userid.results | map(attribute='stdout_lines')|list }}"

The output is as below.

ok: [localhost] => {
    "ansible_facts": {
        "server_names": [
            [
                "root",
                "root",
                "root"
            ],
            [
                "root",
                "root",
                "root"
            ],
            [
                "root",
                "root",
                "root"
            ]
        ]
    },
    "changed": false
}

I need something like below: i.e all ids separated by a line in a single variable.

 "server_names": [
            [
                "root",
                "root",
                "root",
                "root",
                "root",
                "root",
                "root",
                "root",
                "root"
            ]

Kindly advise.

标签: dictionaryjoinansible

解决方案


展平列表

- set_fact:
    server_names: "{{ server_names|flatten }}"

推荐阅读