Jenkins:Groovy解析文件-可分析清单

inb24sb2  于 2022-12-11  发布在  Jenkins
关注(0)|答案(1)|浏览(155)

我正在尝试在本地重新构建Groovy脚本,以使其满足我的要求。我在这里找到了初始脚本,它应该从Ansible清单中提取所有主机名。为了实现这些更改,我在本地设置了Visual Studio代码。遗憾的是,我无法运行该脚本。我的代码如下所示:

def getHostList(def appName, def filePath) {

    def hosts = []
    def content = readFile(file: filePath)
    def startCollect = false
    for(def line : content.split('\n')) {
        if(line.contains("["+ appName +"]")){
            startCollect = true
            continue
        } else if(startCollect) {
            if(!line.allWhitespace && !line.contains('[')){
                hosts.add(line.trim())
            } else {
                break
            }
        } 
    }
    return hosts
}

def hosts = getHostList("client-app", "/absoulte/path/hosts2")

我尝试处理的文件具有以下结构:

[Clients]
Client1 ansible_host=192.168.1.2
Client2 ansible_host=192.168.1.3
Client3 ansible_host=192.168.1.4
[Clients:vars]
ansible_ssh_port=5985
ansible_winrm_transport=basic
ansible_winrm_server_cert_validation=ignore

[Server]
Server1 ansible_host=192.168.10.2
Server2 ansible_host=192.168.10.3
Server3 ansible_host=192.168.10.4
[Server:vars]
ansible_ssh_port=5985
ansible_winrm_transport=basic
ansible_winrm_server_cert_validation=ignore

[Linux]
Linux1 ansible_host=192.168.20.1
....

在执行脚本时,我收到以下错误消息
Caught: groovy.lang.MissingMethodException: No signature of method: test.readFile() is applicable for argument types: (LinkedHashMap) values: [[file:/absoulte/path/hosts2]] groovy.lang.MissingMethodException: No signature of method: test.readFile() is applicable for argument types: (LinkedHashMap) values: [[file:/absoulte/path/hosts2]] at test.getHostList(test.groovy:7) at test.run(test.groovy:2)
你们中有谁知道我如何解决这个问题,或者我如何将组(方括号中的组)下的所有主机名存储到一个变量中?
感谢与问候
更改了路径。在Jenkins中运行脚本时,其工作完美无瑕。

gr8qqesn

gr8qqesn1#

鉴于库存

shell> cat hosts
[Clients]
Client1 ansible_host=192.168.1.2
Client2 ansible_host=192.168.1.3
Client3 ansible_host=192.168.1.4
[Clients:vars]
ansible_ssh_port=5985
ansible_winrm_transport=basic
ansible_winrm_server_cert_validation=ignore

[Server]
Server1 ansible_host=192.168.10.2
Server2 ansible_host=192.168.10.3
Server3 ansible_host=192.168.10.4
[Server:vars]
ansible_ssh_port=5985
ansible_winrm_transport=basic
ansible_winrm_server_cert_validation=ignore

[Linux]
Linux1 ansible_host=192.168.20.1

问:***“将组下的所有主机名存储到一个变量中。"***
答:还有更多选择:

  1. Ansible自动创建所有组的字典。例如,
- hosts: all
  gather_facts: false
  tasks:
    - debug:
        var: groups
      run_once: true

给予

groups:
    Clients:
    - Client1
    - Client2
    - Client3
    Linux:
    - Linux1
    Server:
    - Server1
    - Server2
    - Server3
    all:
    - Client1
    - Client2
    - Client3
    - Server1
    - Server2
    - Server3
    - Linux1
    ungrouped: []

所有主机都会自动列在 groups.all 中。
1.如果您想自己解析清单,请使用 ansible-inventory

shell> ansible-inventory -i hosts --list --yaml
all:
  children:
    Clients:
      hosts:
        Client1:
          ansible_host: 192.168.1.2
          ansible_ssh_port: 5985
          ansible_winrm_server_cert_validation: ignore
          ansible_winrm_transport: basic
        Client2:
          ansible_host: 192.168.1.3
          ansible_ssh_port: 5985
          ansible_winrm_server_cert_validation: ignore
          ansible_winrm_transport: basic
        Client3:
          ansible_host: 192.168.1.4
          ansible_ssh_port: 5985
          ansible_winrm_server_cert_validation: ignore
          ansible_winrm_transport: basic
    Linux:
      hosts:
        Linux1:
          ansible_host: 192.168.20.1
    Server:
      hosts:
        Server1:
          ansible_host: 192.168.10.2
          ansible_ssh_port: 5985
          ansible_winrm_server_cert_validation: ignore
          ansible_winrm_transport: basic
        Server2:
          ansible_host: 192.168.10.3
          ansible_ssh_port: 5985
          ansible_winrm_server_cert_validation: ignore
          ansible_winrm_transport: basic
        Server3:
          ansible_host: 192.168.10.4
          ansible_ssh_port: 5985
          ansible_winrm_server_cert_validation: ignore
          ansible_winrm_transport: basic
    ungrouped: {}
  • ansible-inventory* 的默认格式为JSON
shell> ansible-inventory -i hosts --list
{
    "Clients": {
        "hosts": [
            "Client1",
            "Client2",
            "Client3"
        ]
    },
    "Linux": {
        "hosts": [
            "Linux1"
        ]
    },
    "Server": {
        "hosts": [
            "Server1",
            "Server2",
            "Server3"
        ]
    },
    "_meta": {
        "hostvars": {
            "Client1": {
                "ansible_host": "192.168.1.2",
                "ansible_ssh_port": 5985,
                "ansible_winrm_server_cert_validation": "ignore",
                "ansible_winrm_transport": "basic"
            },
            "Client2": {
                "ansible_host": "192.168.1.3",
                "ansible_ssh_port": 5985,
                "ansible_winrm_server_cert_validation": "ignore",
                "ansible_winrm_transport": "basic"
            },
            "Client3": {
                "ansible_host": "192.168.1.4",
                "ansible_ssh_port": 5985,
                "ansible_winrm_server_cert_validation": "ignore",
                "ansible_winrm_transport": "basic"
            },
            "Linux1": {
                "ansible_host": "192.168.20.1"
            },
            "Server1": {
                "ansible_host": "192.168.10.2",
                "ansible_ssh_port": 5985,
                "ansible_winrm_server_cert_validation": "ignore",
                "ansible_winrm_transport": "basic"
            },
            "Server2": {
                "ansible_host": "192.168.10.3",
                "ansible_ssh_port": 5985,
                "ansible_winrm_server_cert_validation": "ignore",
                "ansible_winrm_transport": "basic"
            },
            "Server3": {
                "ansible_host": "192.168.10.4",
                "ansible_ssh_port": 5985,
                "ansible_winrm_server_cert_validation": "ignore",
                "ansible_winrm_transport": "basic"
            }
        }
    },
    "all": {
        "children": [
            "Clients",
            "Linux",
            "Server",
            "ungrouped"
        ]
    }
}

您可以使用 jq 解析JSON。

shell> ansible-inventory -i hosts --list | jq '._meta.hostvars | keys[]'
"Client1"
"Client2"
"Client3"
"Linux1"
"Server1"
"Server2"
"Server3"

相关问题