Skip to main content

ansible_inventory/script_output/
list.rs

1//! [--list](https://docs.ansible.com/ansible/latest/cli/ansible-inventory.html#cmdoption-ansible-inventory-list)
2
3use indexmap::{IndexMap, IndexSet};
4use serde::{Deserialize, Serialize};
5use serde_json::Map;
6
7use crate::{
8    group::{GroupName, GroupVars},
9    host::{HostName, HostVars},
10};
11
12//
13#[derive(Deserialize, Serialize, Debug, Clone, Default)]
14pub struct List {
15    #[serde(rename = "_meta", default)]
16    pub meta: ListMeta,
17    #[serde(flatten)]
18    pub groups: IndexMap<GroupName, ListGroup>,
19}
20
21#[derive(Deserialize, Serialize, Debug, Clone, Default)]
22pub struct ListMeta {
23    pub hostvars: IndexMap<HostName, HostVars>,
24}
25
26#[derive(Deserialize, Serialize, Debug, Clone, Default)]
27pub struct ListGroup {
28    #[serde(default, skip_serializing_if = "IndexSet::is_empty")]
29    pub children: IndexSet<GroupName>,
30    #[serde(default, skip_serializing_if = "IndexSet::is_empty")]
31    pub hosts: IndexSet<HostName>,
32    #[serde(default, skip_serializing_if = "Map::is_empty")]
33    pub vars: GroupVars,
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    use serde_json::Value;
41
42    #[test]
43    fn test_de_list() {
44        match serde_json::from_str::<List>(include_str!(
45            "../../tests/script_output_list_json_files/sample_1.json"
46        )) {
47            Ok(list) => {
48                assert_eq!(list.meta.hostvars.len(), 6);
49                assert_eq!(
50                    list.meta
51                        .hostvars
52                        .get(&HostName("leaf01".into()))
53                        .and_then(|x| x.get("ansible_host")),
54                    Some(&Value::from("10.16.10.11"))
55                );
56                assert_eq!(
57                    list.groups
58                        .get(&GroupName::Other("leafs".into()))
59                        .map(|x| x.hosts.len()),
60                    Some(2)
61                );
62            }
63            Err(err) => panic!("{err}"),
64        }
65
66        match serde_json::from_str::<List>(include_str!(
67            "../../tests/script_output_list_json_files/sample_2_1.json"
68        )) {
69            Ok(list) => {
70                assert!(list.groups.is_empty());
71            }
72            Err(err) => panic!("{err}"),
73        }
74        match serde_json::from_str::<List>(include_str!(
75            "../../tests/script_output_list_json_files/sample_2_2.json"
76        )) {
77            Ok(list) => {
78                assert!(list.meta.hostvars.is_empty());
79                assert!(list.groups.is_empty());
80            }
81            Err(err) => panic!("{err}"),
82        }
83        match serde_json::from_str::<List>(include_str!(
84            "../../tests/script_output_list_json_files/sample_2_3.json"
85        )) {
86            Ok(list) => {
87                assert!(list.meta.hostvars.is_empty());
88            }
89            Err(err) => panic!("{err}"),
90        }
91
92        match serde_json::from_str::<List>(include_str!(
93            "../../tests/script_output_list_json_files/sample_3.json"
94        )) {
95            Ok(list) => {
96                assert!(list.meta.hostvars.is_empty());
97            }
98            Err(err) => panic!("{err}"),
99        }
100    }
101}