... and you can add the exclude attribute and filter out items where
exclude is false ...
+++
---
- 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:
- 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') }}"
* excl_list: "{{ server_groups | dict2items | map(attribute='value')
| map(attribute='exclude') }}"*
- set_fact:
server_order: "{{ server_order + [ { 'name': item.0, 'halt':
item.1, 'boot': item.2*, 'excl': item.3* } ] }}"
with_together:
- "{{ name_list }}"
- "{{ halt_list }}"
- "{{ boot_list }}"
* - "{{ excl_list }}"*
- name: Shutting down servers
debug: msg="Shutting down server {{ item.name }}."
loop: "{{ server_order* | json_query('[?excl==`false`]')* |
sort(attribute='halt') }}"
- name: Starting up servers
debug: msg="Starting up server {{ item.name }}."
loop: "{{ server_order* | json_query('[?excl==`false`]')* |
sort(attribute='boot') }}"
+++
Thanks for humoring me .. nice challenge.
--
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 9:21:45 AM UTC-4 Walter Rowe wrote:
> Just to re-enforce what I already provided ...
>
> +++
> ---
> - 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:
> - 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') }}"
>
> - set_fact:
> server_order: "{{ server_order + [ { 'name': item.0, 'halt':
> item.1, 'boot': item.2 } ] }}"
> with_together:
> - "{{ name_list }}"
> - "{{ halt_list }}"
> - "{{ boot_list }}"
>
>
> * - name: Shutting down servers* debug: msg="Shutting down server
> {{ item.name }}."
>
> * loop: "{{ server_order | sort(attribute='halt') }}"*
>
> * - name: Starting up servers* debug: msg="Starting up server {{
> item.name }}."
> * loop: "{{ server_order | sort(attribute='boot') }}"*
> +++
>
> +++
> % 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 [Shutting down servers]
> ********************************************************************************************ok:
>
> [localhost] => (item={'name': 'print_server_01', *'halt': 1*, 'boot': 2})
> => {
> "msg": "Shutting down server print_server_01."
> }
> ok: [localhost] => (item={'name': 'file_server_01', *'halt': 2*, 'boot':
> 3}) => {
> "msg": "Shutting down server file_server_01."
> }
> ok: [localhost] => (item={'name': 'database_server_01', *'halt': 3*,
> 'boot': 1}) => {
> "msg": "Shutting down server database_server_01."
> }
> ok: [localhost] => (item={'name': 'database_server_02', *'halt': 3*,
> 'boot': 2}) => {
> "msg": "Shutting down server database_server_02."
> }
>
>
> *TASK [Starting up servers]
> **********************************************************************************************ok:
>
> [localhost] => (item={'name': 'database_server_01', 'halt': 3, *'boot': 1*})
> => {
> "msg": "Starting up server database_server_01."
> }
> ok: [localhost] => (item={'name': 'database_server_02', 'halt': 3, *'boot':
> 2*}) => {
> "msg": "Starting up server database_server_02."
> }
> ok: [localhost] => (item={'name': 'print_server_01', 'halt': 1, *'boot':
> 2*}) => {
> "msg": "Starting up server print_server_01."
> }
> ok: [localhost] => (item={'name': 'file_server_01', 'halt': 2, *'boot': 3*})
> => {
> "msg": "Starting up server file_server_01."
> }
>
> PLAY RECAP
> *************************************************************************************************************
> localhost : ok=4 changed=0 unreachable=0
> failed=0 skipped=0 rescued=0 ignored=0
> +++
>
> --
> 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:44:35 AM UTC-4 Walter Rowe wrote:
>
>> 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/5d2e30ef-a420-4fc6-ae9c-6acaf4794654n%40googlegroups.com.