Re: [ansible-project] How to split string twice in ansible playbook hosts section.

2022-12-27 Thread Vladimir Botka
> On Tue, Dec 27, 2022 at 7:04 AM Know-Your-Tech  wrote:
> >
> > ansible-playbook -i /web/aes/admin/playbooks/updated.hosts
> > /web/aes/admin/playbooks/split.yml -e ENV=qa -e NODE=cluster -e
> > instance_name=APP1,APP2
> >
> > Playbook:
> >
> > ---
> > - hosts: "{{ [ENV] | product(instance_name.split(',')) | product([NODE]) |
> > product(['wladmin_mmsplit'])|map('flatten')|map('join', '_') }}"

Put the logic into the inventory plugin *constructed*. See
shell> ansible-doc -t inventory ansible.builtin.constructed

In the example below, I'll use another inventory plugin *generator*
which you don't need because you obviously have another source of the
inventory. When you try the code below in your environment use this
source instead of 02-generator.yml. See
shell> ansible-doc -t inventory ansible.builtin.generator

I've ignored the case of the parameters.

1) Create the inventory

shell> tree inventory/
inventory/
├── 01-hosts
├── 02-generator.yml
└── 03-constructed.yml

shell> cat inventory/01-hosts 
localhost

shell> cat inventory/02-generator.yml 
plugin: ansible.builtin.generator
hosts:
  name: "{{ env }}_{{ instance }}_{{ node }}_{{ ext }}"
layers:
  env:
- dev
- qa
- prod
  instance:
- app1
- app2
  node:
- cluster1
- cluster2
  ext:
- wladmin_mmsplit

shell> cat inventory/03-constructed.yml 
plugin: ansible.builtin.constructed
use_extra_vars: true
compose:
  layer_env: ENV.split(',')
  layer_node: NODE.split(',')
  layer_instance: instance_name.split(',')
groups:
  my_group: inventory_hostname.split('_').0 in layer_env and
inventory_hostname.split('_').1 in layer_instance and
inventory_hostname.split('_').2 in layer_node

2) Test inventory

shell> ansible-inventory -i inventory --list --yaml
all:
  children:
ungrouped:
  hosts:
dev_app1_cluster1_wladmin_mmsplit: {}
dev_app1_cluster2_wladmin_mmsplit: {}
dev_app2_cluster1_wladmin_mmsplit: {}
dev_app2_cluster2_wladmin_mmsplit: {}
localhost: {}
prod_app1_cluster1_wladmin_mmsplit: {}
prod_app1_cluster2_wladmin_mmsplit: {}
prod_app2_cluster1_wladmin_mmsplit: {}
prod_app2_cluster2_wladmin_mmsplit: {}
qa_app1_cluster1_wladmin_mmsplit: {}
qa_app1_cluster2_wladmin_mmsplit: {}
qa_app2_cluster1_wladmin_mmsplit: {}
qa_app2_cluster2_wladmin_mmsplit: {}

3) Test inventory group *my_group*

shell> ansible-inventory -i inventory --list --yaml -e ENV=qa -e NODE=cluster2 
-e instance_name=app1,app2
all:
  children:
my_group:
  hosts:
qa_app1_cluster2_wladmin_mmsplit:
  ENV: qa
  NODE: cluster2
  instance_name: app1,app2
  layer_env:
  - qa
  layer_instance:
  - app1
  - app2
  layer_node:
  - cluster2
qa_app2_cluster2_wladmin_mmsplit:
  ENV: qa
  NODE: cluster2
  instance_name: app1,app2
  layer_env:
  - qa
  layer_instance:
  - app1
  - app2
  layer_node:
  - cluster2
ungrouped:
  hosts:
dev_app1_cluster1_wladmin_mmsplit:
...

4) Use *my_group* in a playbook

shell> cat pb.yml 
- hosts: my_group
  tasks:
- debug:
var: ansible_play_hosts_all
  run_once: true

shell> ansible-playbook -i inventory -e ENV=qa -e NODE=cluster2 -e 
instance_name=app1,app2 pb.yml

PLAY [my_group]
*

TASK [debug]
*
ok: [qa_app1_cluster2_wladmin_mmsplit] =>
  ansible_play_hosts_all:
  - qa_app1_cluster2_wladmin_mmsplit
  - qa_app2_cluster2_wladmin_mmsplit
  ...

5) The items added to the inventory (02-generator.yml in this example)

  instance:
- app1
- app2
- app3-brazil

will be automatically available for selection

shell> ansible-playbook -i inventory -e ENV=qa -e NODE=cluster2 -e 
instance_name=app1,app3-brazil pb.yml

PLAY [my_group]
**

TASK [debug]
*
ok: [qa_app1_cluster2_wladmin_mmsplit] => ansible_play_hosts_all:
  - qa_app1_cluster2_wladmin_mmsplit
  - qa_app3-brazil_cluster2_wladmin_mmsplit
  ...

-- 
Vladimir Botka

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20221228041926.7c027306%40gmail.com.


pgpsNUXdPi3Vw.pgp
Description: OpenPGP digital signature


Re: [ansible-project] How to split string twice in ansible playbook hosts section.

2022-12-27 Thread Vladimir Botka
On Tue, 27 Dec 2022 09:47:57 -0700
Richard Megginson  wrote:

> `split` is not an ansible filter

Wrong. The filter *split* is available since 2.11. See
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#manipulating-strings

-- 
Vladimir Botka

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20221227192459.2011e8f0%40gmail.com.


pgpiQOqAKtbmU.pgp
Description: OpenPGP digital signature


Re: [ansible-project] How to split string twice in ansible playbook hosts section.

2022-12-27 Thread Know-Your-Tech
Thanks, Richard, but I was looking for a specific solution which seems to 
be tricky.

On Tuesday, December 27, 2022 at 10:18:27 PM UTC+5:30 Richard Megginson 
wrote:

> `split` is not an ansible filter - you cannot use it like `| 
> split(something)` - you have to use as a string method which is why 
> `instance_name.split(',')` works
>
> On Tue, Dec 27, 2022 at 7:04 AM Know-Your-Tech  wrote:
>
>> Below is how i call my ansible-playbook and pass application names as 
>> parameters APP1 & APP2
>>
>> ansible-playbook -i /web/aes/admin/playbooks/updated.hosts 
>> /web/aes/admin/playbooks/split.yml -e ENV=qa -e NODE=cluster -e 
>> instance_name=APP1,APP2
>>
>> Playbook:
>>
>> ---
>> - hosts: "{{ [ENV] | product(instance_name.split(',')) | product([NODE]) 
>> | product(['wladmin_mmsplit'])|map('flatten')|map('join', '_') }}"
>>   user: wladmin
>>   gather_facts: no
>>
>> I get the desired Output as below:
>>
>> PLAY [['qa_APP1_cluster_wladmin_mmsplit', 
>> 'qa_APP2_cluster_wladmin_mmsplit']]
>>
>> The problem occurs when i pass the application names as 
>> APP1-brazil & APP2-Chile
>>
>> and wish the same output as before.
>>
>> ansible-playbook -i /web/aes/admin/playbooks/updated.hosts 
>> /web/aes/admin/playbooks/split.yml -e ENV=qa -e NODE=cluster -e 
>> instance_name=APP1-brazil,APP2-Chile
>>
>> I tried the below but i get error:
>>
>> - hosts: "{{ [ENV] | product(instance_name.split(',') | split('-')[0]) | 
>> product([NODE]) | product(['wladmin_mmsplit'])|map('flatten')|map('join', 
>> '_') }}"
>>
>> Error in output:
>>
>> ERROR! template error while templating string: expected token ',', got 
>> '['. String: {{ [ENV] | product(instance_name.split(',') | split('-')[0]) | 
>> product([NODE]) | product(['wladmin_mmsplit'])|map('flatten')|map('join', 
>> '_') }}
>>
>> Can you please suggest how can i address this requirement? 
>>  
>>
>> -- 
>> 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 ansible-proje...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/da1f5460-e7c8-4e84-814a-ed3b2e3a9828n%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/5ab85cf8-d627-4092-afc6-a7bd7d883a0dn%40googlegroups.com.


Re: [ansible-project] How to split string twice in ansible playbook hosts section.

2022-12-27 Thread Richard Megginson
`split` is not an ansible filter - you cannot use it like `|
split(something)` - you have to use as a string method which is why
`instance_name.split(',')` works

On Tue, Dec 27, 2022 at 7:04 AM Know-Your-Tech  wrote:

> Below is how i call my ansible-playbook and pass application names as
> parameters APP1 & APP2
>
> ansible-playbook -i /web/aes/admin/playbooks/updated.hosts
> /web/aes/admin/playbooks/split.yml -e ENV=qa -e NODE=cluster -e
> instance_name=APP1,APP2
>
> Playbook:
>
> ---
> - hosts: "{{ [ENV] | product(instance_name.split(',')) | product([NODE]) |
> product(['wladmin_mmsplit'])|map('flatten')|map('join', '_') }}"
>   user: wladmin
>   gather_facts: no
>
> I get the desired Output as below:
>
> PLAY [['qa_APP1_cluster_wladmin_mmsplit',
> 'qa_APP2_cluster_wladmin_mmsplit']]
>
> The problem occurs when i pass the application names as
> APP1-brazil & APP2-Chile
>
> and wish the same output as before.
>
> ansible-playbook -i /web/aes/admin/playbooks/updated.hosts
> /web/aes/admin/playbooks/split.yml -e ENV=qa -e NODE=cluster -e
> instance_name=APP1-brazil,APP2-Chile
>
> I tried the below but i get error:
>
> - hosts: "{{ [ENV] | product(instance_name.split(',') | split('-')[0]) |
> product([NODE]) | product(['wladmin_mmsplit'])|map('flatten')|map('join',
> '_') }}"
>
> Error in output:
>
> ERROR! template error while templating string: expected token ',', got
> '['. String: {{ [ENV] | product(instance_name.split(',') | split('-')[0]) |
> product([NODE]) | product(['wladmin_mmsplit'])|map('flatten')|map('join',
> '_') }}
>
> Can you please suggest how can i address this requirement?
>
>
> --
> 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 ansible-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/da1f5460-e7c8-4e84-814a-ed3b2e3a9828n%40googlegroups.com
> 
> .
>

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CALF5A-JaEOy%2BNgwvLySNmtTX7kXjcyZk5UbP0aQzhVY%3De1OLXA%40mail.gmail.com.


[ansible-project] How to split string twice in ansible playbook hosts section.

2022-12-27 Thread Know-Your-Tech
Below is how i call my ansible-playbook and pass application names as 
parameters APP1 & APP2

ansible-playbook -i /web/aes/admin/playbooks/updated.hosts 
/web/aes/admin/playbooks/split.yml -e ENV=qa -e NODE=cluster -e 
instance_name=APP1,APP2

Playbook:

---
- hosts: "{{ [ENV] | product(instance_name.split(',')) | product([NODE]) | 
product(['wladmin_mmsplit'])|map('flatten')|map('join', '_') }}"
  user: wladmin
  gather_facts: no

I get the desired Output as below:

PLAY [['qa_APP1_cluster_wladmin_mmsplit', 
'qa_APP2_cluster_wladmin_mmsplit']]

The problem occurs when i pass the application names as 
APP1-brazil & APP2-Chile

and wish the same output as before.

ansible-playbook -i /web/aes/admin/playbooks/updated.hosts 
/web/aes/admin/playbooks/split.yml -e ENV=qa -e NODE=cluster -e 
instance_name=APP1-brazil,APP2-Chile

I tried the below but i get error:

- hosts: "{{ [ENV] | product(instance_name.split(',') | split('-')[0]) | 
product([NODE]) | product(['wladmin_mmsplit'])|map('flatten')|map('join', 
'_') }}"

Error in output:

ERROR! template error while templating string: expected token ',', got '['. 
String: {{ [ENV] | product(instance_name.split(',') | split('-')[0]) | 
product([NODE]) | product(['wladmin_mmsplit'])|map('flatten')|map('join', 
'_') }}

Can you please suggest how can i address this requirement? 
 

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/da1f5460-e7c8-4e84-814a-ed3b2e3a9828n%40googlegroups.com.