Создание словаря имен хостов для IP-адресов с помощью Ansible

Вместо того, чтобы пытаться использовать сценарий оболочки, мы можем написать короткую программу на C, которая позволит нам вернуться к обычному экрану, когда программа остановлена ​​:

#define _GNU_SOURCE 1

#include <stdbool.h>
#include <stdio.h>


#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

/* Quick hack; todo: use terminfo instead */
#include <stdlib.h>
static void enter_alt_screen(void)
{
    system("tput smcup");
}
static void leave_alt_screen(void)
{
    system("tput rmcup");
}

int main(int argc, char **argv)
{
    if (argc < 2) {
        fprintf(stderr, "Usage: %s command args...", argv[0]);
        return 1;
    }

    if (!isatty(fileno(stdout))) {
        /* not a terminal; act normally */
        execvp(argv[1], argv+1);
    }

    enter_alt_screen();
    const pid_t child = fork();
    switch (child) {
    case -1:
        leave_alt_screen();
        perror("fork");
        return 1;
    case 0:
        /* child */
        execvp(argv[1], argv+1);
        leave_alt_screen();
        perror("exec");
        return 1;
    }

    int status;
    while (waitpid(child, &status, WCONTINUED) == child) {
        if (WIFSTOPPED(status)) {
            leave_alt_screen();
        } else if (WIFCONTINUED(status)) {
            enter_alt_screen();
        } else if (WIFSIGNALED(status)) {
            leave_alt_screen();
            signal(WTERMSIG(status), signal(SIGTERM, SIG_DFL));
            raise(WTERMSIG(status));
        } else if (WIFEXITED(status)) {
            leave_alt_screen();
            return WEXITSTATUS(status);
        }
    }
    return 1;
}
1
22.09.2021, 20:17
1 ответ

Извлеките словари swarm _ips из hostvars и объедините их, например.

    - set_fact:
        swarm_ips: "{{ ansible_play_hosts|
                       map('extract', hostvars, 'swarm_ips')|
                       combine }}"
      run_once: true

дает

  swarm_ips:
    manager.ip: 10.0.1.203
    worker0.ip: 10.0.1.42
    worker1.ip: 10.0.1.201
    worker2.ip: 10.0.1.252
1
22.09.2021, 23:39

Теги

Похожие вопросы