adding to the playbook I already showed ... you now can sort this list by
the 'down_group' (halt) or 'up_group' (boot) attribute to get your ordered
lists.
- debug: msg="{{ server_order | sort(attribute='halt') }}"
- debug: msg="{{ server_order | sort(attribute='boot') }}"
I think that gets you to where you want.
--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce
On Thursday, August 18, 2022 at 8:40:42 AM UTC-4 Walter Rowe wrote:
> I would create one vars file per customer and use a variable that sources
> the customer's vars file at run time. You also could have a folder per
> customer that holds all customer specific items.
>
> % ansible-playbook -e customer_name='customer_A'
>
> In the playbook source customer_A's vars file.
>
> vars:
> server_groups: "{{ lookup('files', 'path/to/vars/' + {{ customer_name }}
> + '.yml') | from_yaml }}"
>
> OR
>
> vars:
> server_groups: "{{ lookup('files', 'path/to/vars/' + {{ customer_name }}
> + '/server_groups.yml') | from_yaml }}"
>
> This loads your customer-specific YAML file as a dictionary into
> server_groups. It gives you enormous flexibility to add/remove customers
> over time without changing your playbook.
>
> ---
> - name: read yaml vars into dictionary
> hosts: localhost
> become: no
> gather_facts: no
> vars:
> server_groups: "{{ lookup('file','./' + customer_name + '.yml') |
> from_yaml }}"
> tasks:
> - debug: var=server_groups
>
> Then use filters to massage the dictionary ...
>
> +++
> ---
> - name: read yaml vars into dictionary
> hosts: localhost
> become: no
> gather_facts: no
> vars:
> server_groups: "{{ lookup('file','./' + customer_name + '.yml') |
> from_yaml }}"
> server_order: []
> tasks:
> # build parallel lists of name, down_group, up_group
> - set_fact:
> name_list: "{{ server_groups.keys() | list }}"
> halt_list: "{{ server_groups | dict2items | map(attribute='value')
> | map(attribute='down_group') }}"
> boot_list: "{{ server_groups | dict2items | map(attribute='value')
> | map(attribute='up_group') }}"
>
> # merge parallel lists into JSON list with server, down_group, up_group
> - set_fact:
> server_order: "{{ server_order + [ { 'name': item.0, 'halt':
> item.1, 'boot': item.2 } ] }}"
> with_together:
> - "{{ name_list }}"
> - "{{ halt_list }}"
> - "{{ boot_list }}"
>
> - debug: var=server_order
> +++
>
> ... and you get a simple JSON list with server name, down_group, up_group
> ... I bet others know slicker ways get to this in a single task of filter
> transformations ...
>
> +++
> % ansible-playbook foo.yml -e customer_name=customer
> PLAY [read yaml vars into dictionary]
> **********************************************************************************
>
> TASK [set_fact]
> ********************************************************************************************************
> ok: [localhost]
>
> TASK [set_fact]
> ********************************************************************************************************
> ok: [localhost] => (item=['database_server_01', 3, 1])
> ok: [localhost] => (item=['database_server_02', 3, 2])
> ok: [localhost] => (item=['file_server_01', 2, 3])
> ok: [localhost] => (item=['print_server_01', 1, 2])
>
> TASK [debug]
> ***********************************************************************************************************
> ok: [localhost] => {
> "server_order": [
> {
> "boot": 1,
> "halt": 3,
> "name": "database_server_01"
> },
> {
> "boot": 2,
> "halt": 3,
> "name": "database_server_02"
> },
> {
> "boot": 3,
> "halt": 2,
> "name": "file_server_01"
> },
> {
> "boot": 2,
> "halt": 1,
> "name": "print_server_01"
> }
> ]
> }
>
> PLAY RECAP
> *************************************************************************************************************
> localhost : ok=3 changed=0 unreachable=0
> failed=0 skipped=0 rescued=0 ignored=0
> +++
>
> Hopefully from here you can craft the rest.
> --
> Walter Rowe, Chief
> Infrastructure Services
> Office of Information Systems Management
> National Institute of Standards and Technology
> United States Department of Commerce
>
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/8b2f8194-dcba-46bc-905e-61f5b951c7b5n%40googlegroups.com.