[ansible-project] Re: How to remove a single item from a list within a nested dictionary

2023-08-25 Thread jean-christophe manciot
OK, I got it, but I'm sure there is a more elegant and simpler way to 
achieve the same result:
- name: Removing a  from the dictionary matching the 

  vars:
vpc:
provider:
- net_type: 'db'
  net_ipv4_subnet: '10.0.0.0/16'
  net_ipv6_subnet: 'FD00::/64'
  net_id: '0'
  nic_ids:
- 11
- 22
- net_type: 'dns'
  net_ipv4_subnet: '10.1.0.0/16'
  net_ipv6_subnet: 'FD01::/64'
  net_id: '1'
  nic_ids:
- 33
- 44
- net_type: 'ns'
  net_ipv4_subnet: '10.2.0.0/16'
  net_ipv6_subnet: 'FD02::/64'
  net_id: '2'
  nic_ids:
- 55
- 66
nic_ids_:
- 44
new_nic_ids: "{{ vpc.provider | selectattr('net_id', 'eq', 
'1') | map(attribute= 'nic_ids') | first | difference(nic_ids_) }}"
seed_vpc:
provider:
- net_id: '1'
  nic_ids: "{{ new_nic_ids }}"
new_vpc:
provider:
"{{ [vpc.provider, seed_vpc.provider] | 
community.general.lists_mergeby('net_id', recursive=True, 
list_merge='replace') }}"
  debug:
msg: "{{ new_vpc }}"

On Friday, August 25, 2023 at 4:22:09 PM UTC+2 jean-christophe manciot 
wrote:

> Here is how to get the new list:
> - name: Removing a  from the list inside the dictionary 
> matching the 
>   vars:
>
> vpc:
> provider:
> - net_type: 'db'
>   net_ipv4_subnet: '10.0.0.0/16'
>   net_ipv6_subnet: 'FD00::/64'
>   net_id: '0'
>   nic_ids:
> - 11
> - 22
> - net_type: 'dns'
>   net_ipv4_subnet: '10.1.0.0/16'
>   net_ipv6_subnet: 'FD01::/64'
>   net_id: '1'
>   nic_ids:
> - 33
> - 44
> - net_type: 'ns'
>   net_ipv4_subnet: '10.2.0.0/16'
>   net_ipv6_subnet: 'FD02::/64'
>   net_id: '2'
>   nic_ids:
> - 55
> - 66
> nic_ids_:
> - 44
> new_nic_ids: "{{ vpc.provider | selectattr('net_id', 'eq', 
> '1') | map(attribute= 'nic_ids') | first | difference(nic_ids_) }}"
>   debug: 
> msg: "{{ new_nic_ids }}"
>
> But I don't know how to get the updated .
>

-- 
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/3b585de9-dcaa-467d-bd2c-9708cbaa496cn%40googlegroups.com.


[ansible-project] Re: How to remove a single item from a list within a nested dictionary

2023-08-25 Thread jean-christophe manciot
OK, I got it, although I'm sure there is a more elegant and simpler way to 
achieve the same result:
- name: Removing a  from the dictionary matching the 

  vars:
vpc:
provider:
- net_type: 'db'
  net_ipv4_subnet: '10.0.0.0/16'
  net_ipv6_subnet: 'FD00::/64'
  net_id: '0'
  nic_ids:
- 11
- 22
- net_type: 'dns'
  net_ipv4_subnet: '10.1.0.0/16'
  net_ipv6_subnet: 'FD01::/64'
  net_id: '1'
  nic_ids:
- 33
- 44
- net_type: 'ns'
  net_ipv4_subnet: '10.2.0.0/16'
  net_ipv6_subnet: 'FD02::/64'
  net_id: '2'
  nic_ids:
- 55
- 66
nic_ids_:
- 44
new_nic_ids: "{{ vpc.provider | selectattr('net_id', 'eq', 
'1') | map(attribute= 'nic_ids') | first | difference(nic_ids_) }}"
seed_vpc:
provider:
- net_id: '1'
  nic_ids: "{{ new_nic_ids }}"
new_vpc:
provider:
"{{ [vpc.provider, seed_vpc.provider] | 
community.general.lists_mergeby('net_id', recursive=True, 
list_merge='replace') }}"
  debug:
msg: "{{ new_vpc }}"

-- 
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/9242158f-7d17-4135-be26-a0b52aacb94bn%40googlegroups.com.


[ansible-project] Re: How to remove a single item from a list within a nested dictionary

2023-08-25 Thread jean-christophe manciot
OK, I got it, but I'm sure there is a more elegant and simpler way to 
achieve the same result:
- name: Removing a  from the dictionary matching the 

  vars:
vpc:
provider:
- net_type: 'db'
  net_ipv4_subnet: '10.0.0.0/16'
  net_ipv6_subnet: 'FD00::/64'
  net_id: '0'
  nic_ids:
- 11
- 22
- net_type: 'dns'
  net_ipv4_subnet: '10.1.0.0/16'
  net_ipv6_subnet: 'FD01::/64'
  net_id: '1'
  nic_ids:
- 33
- 44
- net_type: 'ns'
  net_ipv4_subnet: '10.2.0.0/16'
  net_ipv6_subnet: 'FD02::/64'
  net_id: '2'
  nic_ids:
- 55
- 66
nic_ids_:
- 44
new_nic_ids: "{{ vpc.provider | selectattr('net_id', 'eq', 
'1') | map(attribute= 'nic_ids') | first | difference(nic_ids_) }}"
seed_vpc:
provider:
- net_id: '1'
  nic_ids: "{{ new_nic_ids }}"
new_vpc: 
provider:
"{{ [vpc.provider, seed_vpc.provider] | 
community.general.lists_mergeby('net_id', recursive=True, 
list_merge='replace') }}"
  debug: 
msg: "{{ new_vpc }}"
.

-- 
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/7443d479-7589-41c8-90c6-f720dba053d5n%40googlegroups.com.


[ansible-project] Re: How to remove a single item from a list within a nested dictionary

2023-08-25 Thread jean-christophe manciot
Here is how to get the new list:
- name: Removing a  from the list inside the dictionary 
matching the 
  vars:
vpc:
provider:
- net_type: 'db'
  net_ipv4_subnet: '10.0.0.0/16'
  net_ipv6_subnet: 'FD00::/64'
  net_id: '0'
  nic_ids:
- 11
- 22
- net_type: 'dns'
  net_ipv4_subnet: '10.1.0.0/16'
  net_ipv6_subnet: 'FD01::/64'
  net_id: '1'
  nic_ids:
- 33
- 44
- net_type: 'ns'
  net_ipv4_subnet: '10.2.0.0/16'
  net_ipv6_subnet: 'FD02::/64'
  net_id: '2'
  nic_ids:
- 55
- 66
nic_ids_:
- 44
new_nic_ids: "{{ vpc.provider | selectattr('net_id', 'eq', 
'1') | map(attribute= 'nic_ids') | first | difference(nic_ids_) }}"
  debug: 
msg: "{{ new_nic_ids }}"

But I don't know how to get the updated .

-- 
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/5ea6c6a0-8f22-49fa-9dfd-acf1ff3e11dcn%40googlegroups.com.


[ansible-project] How to remove a single item from a list within a nested dictionary

2023-08-25 Thread jean-christophe manciot
The source nested dictionary is for instance the following:
vpc:
provider:
- net_type: 'db'
  net_ipv4_subnet: '10.0.0.0/16'
  net_ipv6_subnet: 'FD00::/64'
  net_id: '0'
  nic_ids:
- 11
- 22
- net_type: 'dns'
  net_ipv4_subnet: '10.1.0.0/16'
  net_ipv6_subnet: 'FD01::/64'
  net_id: '1'
  nic_ids:
- 33
- 44
- net_type: 'ns'
  net_ipv4_subnet: '10.2.0.0/16'
  net_ipv6_subnet: 'FD02::/64'
  net_id: '2'
  nic_ids:
- 55
- 66

We need to remove the item ''44  from the list  located 
inside the dictionary matched by the key/value  net_id=='1'.

Selecting the  list inside the right dictionary is easy with:
vpc.provider | selectattr('net_id', 'eq', '1') | map(attribute= 'nic_ids'), 
but I have no idea how to remove the item from list and build the new 
vpc.provider?

Any suggestion?

-- 
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/ea7107ba-2836-49ac-b3fb-a3a1e26fad20n%40googlegroups.com.


Re: [ansible-project] How to replace only a specific entry in a nested dictionary

2023-08-21 Thread jean-christophe manciot
Very nice :-)
As it turns out, there is only one entry of type 'dns' , so both solutions 
work perfectly.


-- 
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/cb1edde1-1618-48d1-a3d3-9cf6cac45e61n%40googlegroups.com.


[ansible-project] How to replace only a specific entry in a nested dictionary

2023-08-18 Thread jean-christophe manciot
The source nested dictionary is for instance the following:
vpc:
provider:
- net_type: 'db'
  net_ipv4_subnet: '10.0.0.0/16'
  net_ipv6_subnet: 'FD00::/64'
  net_id: 0
  nic_ids:
- 11
- 22
- net_type: 'dns'
  net_ipv4_subnet: '10.1.0.0/16'
  net_ipv6_subnet: 'FD01::/64'
  net_id: 1
  nic_ids:
- 33
- 44
- net_type: 'ns'
  net_ipv4_subnet: '10.2.0.0/16'
  net_ipv6_subnet: 'FD02::/64'
  net_id: 2
  nic_ids:
- 55
- 66

We need to replace only the whole entry indexed by "net_type: 'dns'" with:
- net_type: 'dns'
  net_ipv4_subnet: '10.3.0.0/16'
  net_ipv6_subnet: 'FD03::/64'
  net_id: 3
  nic_ids: []

so the result would look like:
vpc:
provider:
- net_type: 'db'
  net_ipv4_subnet: '10.0.0.0/16'
  net_ipv6_subnet: 'FD00::/64'
  net_id: 0
  nic_ids:
- 11
- 22
- net_type: 'dns'
  net_ipv4_subnet: '10.3.0.0/16'
  net_ipv6_subnet: 'FD03::/64'
  net_id: 3
  nic_ids: []
- net_type: 'ns'
  net_ipv4_subnet: '10.2.0.0/16'
  net_ipv6_subnet: 'FD02::/64'
  net_id: 2
  nic_ids:
- 55
- 66

I've tried the following task using the *combine* filter:
- name: Combining 2  dictionaries with 
combine/list_merge='replace'
  vars:
vpc:
provider:
- net_type: 'db'
  net_ipv4_subnet: '10.0.0.0/16'
  net_ipv6_subnet: 'FD00::/64'
  net_id: 0
  nic_ids:
- 11
- 22
- net_type: 'dns'
  net_ipv4_subnet: '10.1.0.0/16'
  net_ipv6_subnet: 'FD01::/64'
  net_id: 1
  nic_ids:
- 33
- 44
- net_type: 'ns'
  net_ipv4_subnet: '10.2.0.0/16'
  net_ipv6_subnet: 'FD02::/64'
  net_id: 2
  nic_ids:
- 55
- 66
net_id: 3
net_ipv4_subnet: '10.3.0.0/16'
net_ipv6_subnet: 'FD03::/64'
new_vpc: "{{ vpc | combine({'provider': [{'net_type': 
'dns', 'net_id': net_id, 'net_ipv4_subnet': net_ipv4_subnet, 
'net_ipv6_subnet': net_ipv6_subnet, 'nic_ids': []}]}, recursive=True, 
list_merge='replace') }}"
  debug:
msg: "{{ new_vpc }}"

which unfortunately removes the other 2 entries:
ok: [localhost] =>
  msg:
provider:
- net_id: 3
  net_ipv4_subnet: 10.3.0.0/16
  net_ipv6_subnet: FD03::/64
  net_type: dns
  nic_ids: []

Same result with community.general.*lists_mergeby* filter:
- name: Combining 2  dictionaries with 
lists_mergeby/list_merge='replace'
  vars:
vpc:
provider:
- net_type: 'db'
  net_ipv4_subnet: '10.0.0.0/16'
  net_ipv6_subnet: 'FD00::/64'
   

[ansible-project] Re: How to unquote a json structure

2022-07-28 Thread jean-christophe manciot
It turns out there is no need for unquoting.
In my original issue, the returned structure was much more complex and 
included some strange *new lines*.
Removing all new lines with:
return.stdout |  regex_replace('\\n', '')
solves this issue.
On Thursday, July 28, 2022 at 11:59:54 AM UTC+2 jean-christophe manciot 
wrote:

> One shell command returns a json structure, but ansible converts it into a 
> string in the registered variable.
> In order to access some attributes, this string needs to be unquoted, but 
> how?
> There is a quote filter, but *there is no unquote filter in ansible or 
> jinja2*.
>
> For instance, let's suppose a shell module returns the following 
> registered variable:
> return:
>   changed: true
>   cmd: |-
> cmd_returning_json
>   delta: '0:00:00.002283'
>   end: '2022-07-28 11:09:17.921175'
>   failed: false
>   msg: ''
>   rc: 0
>   start: '2022-07-28 11:09:17.918892'
>   stderr: ''
>   stderr_lines: []
>   stdout: '{"class": "data", "id": 
> "a61b96e7-427e-493a-993c-c3efc8a16aa1", "size": "500GB", "type": "ssd"}'
>
> How do we convert return.stdout into:
> {"class": "data", "id": "a61b96e7-427e-493a-993c-c3efc8a16aa1", "size": 
> "500GB", "type": "ssd"}
> so that we can access for instance: 
> return.stdout.id
>

-- 
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/0e05fd79-b241-4ecb-935e-a24c823c47f8n%40googlegroups.com.


[ansible-project] How to unquote a json structure

2022-07-28 Thread jean-christophe manciot
One shell command returns a json structure, but ansible converts it into a 
string in the registered variable.
In order to access some attributes, this string needs to be unquoted, but 
how?
There is a quote filter, but *there is no unquote filter in ansible or 
jinja2*.

For instance, let's suppose a shell module returns the following registered 
variable:
return:
  changed: true
  cmd: |-
cmd_returning_json
  delta: '0:00:00.002283'
  end: '2022-07-28 11:09:17.921175'
  failed: false
  msg: ''
  rc: 0
  start: '2022-07-28 11:09:17.918892'
  stderr: ''
  stderr_lines: []
  stdout: '{"class": "data", "id": 
"a61b96e7-427e-493a-993c-c3efc8a16aa1", "size": "500GB", "type": "ssd"}'

How do we convert return.stdout into:
{"class": "data", "id": "a61b96e7-427e-493a-993c-c3efc8a16aa1", "size": 
"500GB", "type": "ssd"}
so that we can access for instance: 
return.stdout.id

-- 
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/0375e9fe-4e94-4c6c-ae3b-9773fe4ce287n%40googlegroups.com.


Re: [ansible-project] How to save a list of dictionaries to a file with a correct indentation

2022-07-28 Thread jean-christophe manciot
@Todd Lewis
Thanks for pointing me to the right direction. :-)
Your suggestion is a bit too short:
volumes:
-   class: data
id: a61b96e7-427e-493a-993c-c3efc8a16aa1
size: 500GB
type: ssd

Strangely, it indents the first attribute of the list despite 'first=false'.
With:
volumes:
{{ volumes | to_nice_yaml | indent(width=8, first=false) }}

we get a correct result:
volumes:
-   class: data
id: a61b96e7-427e-493a-993c-c3efc8a16aa1
size: 500GB
type: ssd

which is close enough, although I don't understand why we can't get the 
original expected result.
On Wednesday, July 27, 2022 at 8:02:19 PM UTC+2 uto...@gmail.com wrote:

> You've misunderstood what the indent parameter of to_nice_yaml does. It 
> doesn't shift everything to the right by that much. Rather, it tells how 
> much to indent those things which must be indented, like dictionaries 
> within dictionaries. You don't have any so it has no effect.
>
> Use the indent filter instead.
>
> volumes:
> {{ volumes | to_nice_yaml() | indent(width=4, first=false) }}
>
> On 7/27/22 1:19 PM, jean-christophe manciot wrote:
>
> The goal seems to be simple, but I cannot manage to get the indentation 
> right. 
>
> *Playbook*:
> - name: Saving a list of dictionaries to a file
>   gather_facts: false
>   hosts:
> - localhost
>   strategy: debug
>   tasks:
> - name: Saving a list of dictionaries to a file
>   vars:
> volumes:
> - class: 'data'
>   id: 'a61b96e7-427e-493a-993c-c3efc8a16aa1'
>   size: '500GB'
>   type: 'ssd'
>   template:
> src: "volumes.j2"
> dest: "../files/volumes.yml"
>
> *Template*:
> volumes:
> {{ volumes | to_nice_yaml(indent=10) }}
>
> *Result*:
> volumes:
> - class: data
>   id: a61b96e7-427e-493a-993c-c3efc8a16aa1
>   size: 500GB
>   type: ssd
>
> *Expected result*:
> volumes:
> - class: data
>   id: a61b96e7-427e-493a-993c-c3efc8a16aa1
>   size: 500GB
>   type: ssd
>
> What is strange is that the indent value has no effect on the result.
> Am I doing something wrong or is this an issue with ansible?
>
> -- 
> 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/9147b9cf-c1ab-4102-905c-7f720c4401c0n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/ansible-project/9147b9cf-c1ab-4102-905c-7f720c4401c0n%40googlegroups.com?utm_medium=email_source=footer>
> .
>
> ​
>
> -- 
> Todd
>
>

-- 
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/f3f7e8e5-b3b3-4cb7-a3a3-76ab5efcda08n%40googlegroups.com.


[ansible-project] How to save a list of dictionaries to a file with a correct indentation

2022-07-27 Thread jean-christophe manciot
The goal seems to be simple, but I cannot manage to get the indentation 
right.

*Playbook*:
- name: Saving a list of dictionaries to a file
  gather_facts: false
  hosts:
- localhost
  strategy: debug
  tasks:
- name: Saving a list of dictionaries to a file
  vars:
volumes:
- class: 'data'
  id: 'a61b96e7-427e-493a-993c-c3efc8a16aa1'
  size: '500GB'
  type: 'ssd'
  template:
src: "volumes.j2"
dest: "../files/volumes.yml"

*Template*:
volumes:
{{ volumes | to_nice_yaml(indent=10) }}

*Result*:
volumes:
- class: data
  id: a61b96e7-427e-493a-993c-c3efc8a16aa1
  size: 500GB
  type: ssd

*Expected result*:
volumes:
- class: data
  id: a61b96e7-427e-493a-993c-c3efc8a16aa1
  size: 500GB
  type: ssd

What is strange is that the indent value has no effect on the result.
Am I doing something wrong or is this an issue with ansible?

-- 
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/9147b9cf-c1ab-4102-905c-7f720c4401c0n%40googlegroups.com.


Re: [ansible-project] How to convert a dictionary to a list of dictionaries

2022-06-14 Thread jean-christophe manciot
So simple! 
Nice! :-)

On Tuesday, June 14, 2022 at 3:35:46 PM UTC+2 vbo...@gmail.com wrote:

> On Tue, 14 Jun 2022 04:25:06 -0700 (PDT)
> jean-christophe manciot  wrote:
>
> > var:
> > key_1: value_11
> > key_2: value_21
> > key_3: value_31
> > 
> > So the goal is to transform the first var into:
> > var:
> > - key_1: value_11
> > key_2: value_21
> > key_3: value_31
>
> Simply close it in the brackets. For example,
>
> - debug:
> msg: "{{ [var] }}"
>
> gives
>
> msg:
> - key_1: value_11
> key_2: value_21
> key_3: value_31
>
> , or
>
> var2: "{{ [var] + [var] }}"
>
> gives
>
> var2:
> - key_1: value_11
> key_2: value_21
> key_3: value_31
> - key_1: value_11
> key_2: value_21
> key_3: value_31
>
> -- 
> 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/a7204db4-7ab8-4cbd-8b83-ed616261685fn%40googlegroups.com.


[ansible-project] How to convert a dictionary to a list of dictionaries

2022-06-14 Thread jean-christophe manciot
I'm faced with a corner case where some devices are expected to return a 
list of dictionaries but instead return a single dictionary. This creates 
an error later on in ansible when a list is expected.
For instance, the device returns the file:
var:
key_1: value_11
key_2: value_21
key_3: value_31

instead of for instance:
var:
- key_1: value_11
  key_2: value_21
  key_3: value_31
- key_1: value_12
  key_2: value_22
  key_3: value_32
  ...

So the goal is to transform the first var into:
var:
- key_1: value_11
  key_2: value_21
  key_3: value_31

AFAIK, there is no ansible filters 

 
to accomplish that; for instance, 'list' does not give the expected result. 
Also, we cannot count on the name or number of keys as they may change.
I suppose the solution may revolve around something like that:
- set_fact:
list_var: >-
{% for key, value in var.items() %}
{{ [{key: value}] }}
{% endfor %}
  when: >
(var | type_debug != 'list')

This does not give the expected result either.

Any suggestion?

-- 
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/3b78d9b8-6bcd-417c-ad4c-c89c89ebef96n%40googlegroups.com.


Re: [ansible-project] Re: How to use include_vars with a file needing elevated privileges

2022-03-17 Thread jean-christophe manciot
@uto...@gmail.com
I just need to let the user running the playbook access files which cannot 
be accessed to any other user (except root of course). 
I'm very surprised that this limitation exist with that module.

@Brian Coca
I just don't understand why the ansible team has decided that become 
keywords cannot affect include_vars. It's just a module that always runs on 
the controller, but what's the difference with all other modules which are 
affected by become keywords but are delegated on the localhost? It just 
does not make any sense to me. Or maybe I'm missing a fundamental piece of 
the inner workings.

-- 
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/4c001a4b-9958-44b0-8cc1-b81e18e8914en%40googlegroups.com.


[ansible-project] Re: How to use include_vars with a file needing elevated privileges

2022-03-17 Thread jean-christophe manciot
Also, what does "Is usable alongside become keywords" mean?

-- 
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/fcfab4a8-2e7d-4e18-ac5e-871351b8ec7dn%40googlegroups.com.


[ansible-project] Re: How to use include_vars with a file needing elevated privileges

2022-03-17 Thread jean-christophe manciot
Are you implying that it is not possible to include a vars file on the 
controller that require higher privileges than the user's permissions 
running the playbook?

-- 
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/4cbe4748-1cbd-4d26-a71d-ac42ca1540bdn%40googlegroups.com.


[ansible-project] Re: How to use include_vars with a file needing elevated privileges

2022-03-17 Thread jean-christophe manciot
It seems to be a tricky one.
I am using ansible core 2.12.3.

Does @Brian Coca have any suggestion?

-- 
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/326139ed-35a0-43df-9b28-06f178aa2367n%40googlegroups.com.


[ansible-project] How to use include_vars with a file needing elevated privileges

2022-03-17 Thread jean-christophe manciot
Let's suppose I run a playbook as a non-root user and one task needs to 
include a vars file with only root permissions.
The ansible.builtin.include_vars official doc 

 
states that:the 'become' attribute is not supported but "Is usable 
alongside become keywords", which seems to contradict the fact that it is 
unsupported.
I tried to use the become vars but that does not work either:
- name: Including vars issue
  hosts: all
  gather_facts: false
  tasks:
- name: Creating a file with root-only permissions 
  vars:
ansible_become: yes
ansible_become_method: sudo
ansible_become_user: root
  file:
group: 'root'
mode: '0640'
owner: 'root'
path: "../files/restricted_file"
state: touch

- name: Including vars with root-only permissions
  vars:
ansible_become: yes
ansible_become_method: sudo
ansible_become_user: root
  include_vars: "../files/restricted_file"
  ignore_errors: true

- name: Including vars with non-root user permissions
  vars:
ansible_become: yes
ansible_become_method: sudo
ansible_become_user: admin
  include_vars: "../files/capabilities.json"
leads to:
 ___
< TASK [Creating a file with root-only permissions] >
 ---
\   ^__^
 \  (oo)\___
(__)\   )\/\
||w |
|| ||

changed: [localhost] => changed=true 
  dest: ../files/restricted_file
  gid: 0
  group: root
  mode: '0640'
  owner: root
  size: 0
  state: file
  uid: 0
 __
< TASK [Including vars with root-only permissions] >
 --
\   ^__^
 \  (oo)\___
(__)\   )\/\
||w |
|| ||

fatal: [localhost]: FAILED! => changed=false 
  ansible_facts: {}
  ansible_included_var_files: []
  message: 'an error occurred while trying to read the file 
''playbooks/issues/../files/restricted_file'': [Errno 13] Permission 
denied: b''playbooks/files/restricted_file''. [Errno 13] Permission denied: 
b''playbooks/files/restricted_file'''
...ignoring
 __
< TASK [Including vars with non-root user permissions] >
 --
\   ^__^
 \  (oo)\___
(__)\   )\/\
||w |
|| ||

ok: [localhost] => changed=false 
...

I'm probably missing something here; how can we work around this limitation?

-- 
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/a706f783-a0a3-4648-8858-c66894c62980n%40googlegroups.com.


Re: [ansible-project] How to discriminate between chunked and not chunked answers from uri

2022-03-11 Thread jean-christophe manciot
I have filed the issue here 


-- 
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/43a058b5-0832-46d5-8c5e-f13543cfe279n%40googlegroups.com.


Re: [ansible-project] How to discriminate between chunked and not chunked answers from uri

2022-03-11 Thread jean-christophe manciot
But how can the Content-Length be unknown at the time the response is sent 
back to the client for small unchunked answers (case n°1 of my first post)?

-- 
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/fedc8bcc-bf73-494c-b021-067cf2b22114n%40googlegroups.com.


Re: [ansible-project] How to discriminate between chunked and not chunked answers from uri

2022-03-11 Thread jean-christophe manciot
Thanks.
But I understand from your answer that really chunked answers should not 
happen under normal circumstances.
Since "Python 3 should be attempting to read all chunks", I tried to set 
the timeout to a high value (3600) to eliminate that potential explanation 
for the chunked answer.
However, it is still happening.
Should I report this a bug or can you suggest anything else?

Also, by default (if no other headers are set), is it possible to receive 
something else than 'transfer_encoding: chunked' from the server for 
body_format: 'json' or body_format: 'raw'?

>

-- 
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/c9fb96b2-124e-4d88-a65f-3e2f5dfdd2e4n%40googlegroups.com.


[ansible-project] How to discriminate between chunked and not chunked answers from uri

2022-03-11 Thread jean-christophe manciot
ansible core 2.12.3

I am unable to discriminate with uri between responses which have been 
chunked and those which haven't.
In both cases, I get the same:
- msg: OK (unknown bytes)
- transfer_encoding: chunked

This happens for instance when using uri over CSR 1kv 17.3.1a with:
- name: Sending GET datastores with uri
  ansible.builtin.uri:
body: ''
body_format: 'json'
force_basic_auth: yes
headers:
  Accept: application/yang-data+json
  Content-Type: application/yang-data+json
method: GET
return_content: yes
status_code: 
"100,101,102,200,201,202,203,204,205,206,207,208,226,300,301,302,303,304,305,307,308"
timeout: 120
url: 
"https://172.21.126.182/restconf/data/ietf-netconf-monitoring:netconf-state/datastores?content=nonconfig;
url_password: password
url_username: admin
validate_certs: false
  register: return_uri_restconf_json

leads to:
task path: main.yml:219
 ESTABLISH LOCAL CONNECTION FOR USER: root
 EXEC /bin/bash -c 'echo ~root && sleep 0'
 EXEC /bin/bash -c '( umask 77 && mkdir -p "` echo 
/root/.ansible/tmp `"&& mkdir "` echo 
/root/.ansible/tmp/ansible-tmp-1646997734.4119265-2691638-25534722658108 `" 
&& echo ansible-tmp-1646997734.4119265-2691638-25534722658108="` echo 
/root/.ansible/tmp/ansible-tmp-1646997734.4119265-2691638-25534722658108 `" 
) && sleep 0'
Using module file 
/usr/local/lib/python3.9/dist-packages/ansible/modules/uri.py
 PUT 
/root/.ansible/tmp/ansible-local-269044374_5i0rc/tmpyvffo2y1 TO 
/root/.ansible/tmp/ansible-tmp-1646997734.4119265-2691638-25534722658108/AnsiballZ_uri.py
 EXEC /bin/bash -c 'chmod u+x 
/root/.ansible/tmp/ansible-tmp-1646997734.4119265-2691638-25534722658108/ 
/root/.ansible/tmp/ansible-tmp-1646997734.4119265-2691638-25534722658108/AnsiballZ_uri.py
 
&& sleep 0'
 EXEC /bin/bash -c '/usr/bin/python3 
/root/.ansible/tmp/ansible-tmp-1646997734.4119265-2691638-25534722658108/AnsiballZ_uri.py
 
&& sleep 0'
 EXEC /bin/bash -c 'rm -f -r 
/root/.ansible/tmp/ansible-tmp-1646997734.4119265-2691638-25534722658108/ > 
/dev/null 2>&1 && sleep 0'
ok: [CSR1000v-17.3.1a -> localhost] => changed=false 
  cache_control: private, no-cache, must-revalidate, proxy-revalidate
  connection: close
  content: |-
{
  "ietf-netconf-monitoring:datastores": {
"datastore": [
  {
"name": "running",
"tailf-netconf-monitoring:transaction-id": "1646-983921-976163"
  },
  {
"name": "candidate"
  }
]
  }
}
  content_type: application/yang-data+json
  cookies: {}
  cookies_string: ''
  date: Fri, 11 Mar 2022 11:22:14 GMT
  elapsed: 0
  invocation:
module_args:
  attributes: null
  body: ''
  body_format: json
  ca_path: null
  client_cert: null
  client_key: null
  creates: null
  dest: null
  follow_redirects: safe
  force: false
  force_basic_auth: true
  group: null
  headers:
Accept: application/yang-data+json
Content-Type: application/yang-data+json
  http_agent: ansible-httpget
  method: GET
  mode: null
  owner: null
  remote_src: false
  removes: null
  return_content: true
  selevel: null
  serole: null
  setype: null
  seuser: null
  src: null
  status_code:
  - 100
  - 101
  - 102
  - 200
  - 201
  - 202
  - 203
  - 204
  - 205
  - 206
  - 207
  - 208
  - 226
  - 300
  - 301
  - 302
  - 303
  - 304
  - 305
  - 307
  - 308
  timeout: 120
  unix_socket: null
  unredirected_headers: []
  unsafe_writes: false
  url: 
https://172.21.126.182/restconf/data/ietf-netconf-monitoring:netconf-state/datastores?content=nonconfig
  url_password: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
  url_username: admin
  use_gssapi: false
  use_proxy: true
  validate_certs: false
  json:
ietf-netconf-monitoring:datastores:
  datastore:
  - name: running
tailf-netconf-monitoring:transaction-id: 1646-983921-976163
  - name: candidate
  msg: OK (unknown bytes)
  pragma: no-cache
  redirected: false
  server: openresty
  status: 200
  transfer_encoding: chunked
  url: 
https://172.21.126.182/restconf/data/ietf-netconf-monitoring:netconf-state/datastores?content=nonconfig

We can see that the response has NOT been chunked.

In another example, the response has been chunked with:
- name: Sending GET netconf-state with uri
  ansible.builtin.uri:
body: ''
body_format: 'json'
force_basic_auth: yes
headers:
  Accept: application/yang-data+json
  Content-Type: application/yang-data+json
method: GET
return_content: yes
status_code: 
"100,101,102,200,201,202,203,204,205,206,207,208,226,300,301,302,303,304,305,307,308"
timeout: 120
url: 

Re: [ansible-project] How to extract a list of values of a field within a list of dictionaries when a key matches another list of values

2022-02-07 Thread jean-christophe manciot
Thanks, your first answer is exactly what I was looking for. :-)

On Monday, February 7, 2022 at 2:32:38 AM UTC+1 uto...@gmail.com wrote:

> This produces the list you want:
>
> ---
> - name: Extracting a list of values of a field within a list of dictionaries 
> when a key matches another list of values
>   hosts:
> - localhost
>   strategy: debug
>   tasks:
> - name: Extract a list of values of a field within a list of dictionaries 
> when a key matches another list of values
>   set_fact:
> db_used_ports: |
> {% set ports=[] %}
> {% for dd0 in domain_definition %}
> {%   set db_servers_names=dd0.db_servers|default([]) %}
> {%   for dd1 in domain_definition %}
> {% if dd1['name'] in db_servers_names %}
> {%   set _ = ports.append(dd1.port) %}
> {% endif %}
> {%   endfor %}
> {% endfor %}{{ ports }}"
>   vars:
> domain_definition:
>   - name: server11
> cluster: cluster1
> db_servers:
>   - server12
>   - server21
> port: '8080'
>   - name: server12
> cluster: cluster1
> db_servers:
>   - server22
> port: '8090'
>   - name: server13
> cluster: cluster1
> port: '8091'
>   - name: server21
> cluster: cluster2
> db_servers:
>   - server12
>   - server22
> port: '9080'
>   - name: server22
> cluster: cluster2
>     port: '9090'
>
> - name: Show the extracted ports
>   debug:
> msg: "{{ db_used_ports }}"
>
> On 2/5/22 9:56 AM, jean-christophe manciot wrote:
>
> Let's assume the following fictional list of dictionaries:
> domain_definition:
> - name: server11
>   cluster: cluster1
>   db_servers:
> - server12
> - server21
>   port: '8080'
> - name: server12
>   cluster: cluster1
>   db_servers:
> - server22
>   port: '8090'
> - name: server13
>   cluster: cluster1
>   port: '8091'
> - name: server21
>   cluster: cluster2
>   db_servers:
> - server12
> - server22
>   port: '9080'
> - name: server22
>   cluster: cluster2
>   port: '9090'
> The goal is to list all ports of used  'db_servers', so the expected list 
> is:
> - '8090'
> - '9080'
> - '9090'
> - '8090'
> - '9090'
>
> The *constraint* is to use one (some operation is performed for each 
> item) and only one loop (the list is huge in reality).
>
> The following does not work because selectattr expects a value, not a list 
> of values:
> - name: Extracting a list of values of a field within a list of 
> dictionaries when a key matches another list of values
>   hosts:
> - localhost
>   strategy: debug
>   tasks:
> - name: Extracting a list of values of a field within a 
> list of dictionaries when a key matches another list of values
>   vars:
> domain_definition:
> - name: server11
>   cluster: cluster1
>   db_servers:
> - server12
> - server21
>   port: '8080'
> - name: server12
>   cluster: cluster1
>   db_servers:
> - server22
>   port: '8090'
> - name: server13
>   cluster: cluster1
>   port: '8091'
> - name: server21
>   cluster: cluster2
>   db_servers:

[ansible-project] How to extract a list of values of a field within a list of dictionaries when a key matches another list of values

2022-02-05 Thread jean-christophe manciot
Let's assume the following fictional list of dictionaries:
domain_definition:
- name: server11
  cluster: cluster1
  db_servers:
- server12
- server21
  port: '8080'
- name: server12
  cluster: cluster1
  db_servers:
- server22
  port: '8090'
- name: server13
  cluster: cluster1
  port: '8091'
- name: server21
  cluster: cluster2
  db_servers:
- server12
- server22
  port: '9080'
- name: server22
  cluster: cluster2
  port: '9090'
The goal is to list all ports of used  'db_servers', so the expected list 
is:
- '8090'
- '9080'
- '9090'
- '8090'
- '9090'

The *constraint* is to use one (some operation is performed for each item) 
and only one loop (the list is huge in reality).

The following does not work because selectattr expects a value, not a list 
of values:
- name: Extracting a list of values of a field within a list of 
dictionaries when a key matches another list of values
  hosts:
- localhost
  strategy: debug
  tasks:
- name: Extracting a list of values of a field within a 
list of dictionaries when a key matches another list of values
  vars:
domain_definition:
- name: server11
  cluster: cluster1
  db_servers:
- server12
- server21
  port: '8080'
- name: server12
  cluster: cluster1
  db_servers:
- server22
  port: '8090'
- name: server13
  cluster: cluster1
  port: '8091'
- name: server21
  cluster: cluster2
  db_servers:
- server12
- server22
  port: '9080'
- name: server22
  cluster: cluster2
  port: '9090'
db_servers_names: "{{ item.db_servers |
default([]) |
list }}"
db_servers_used_ports:  "{{ domain_definition |
selectattr('name', 
'eq', db_servers_names) |
map(attribute= 
'port') |
list }}"
  debug:
msg:
- "db_servers_names:{{ 
db_servers_names }}"
- "db_servers_used_ports:   {{ 
db_servers_used_ports }}"
  loop: "{{ domain_definition }}"

Any suggestion?

-- 
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/db813385-71be-4028-aa73-151e0b5453f2n%40googlegroups.com.


[ansible-project] Re: How to list a subelement from hostvars for all ansible_play_hosts in a single line

2021-05-07 Thread jean-christophe manciot
The solution seems to be easy actually:
when: ansible_play_hosts | map('extract', hostvars, 'return_task') | list | 
selectattr('failed') | list is any

-- 
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/797a89d1-c5c3-4375-868b-854e3f8461d0n%40googlegroups.com.


[ansible-project] How to list a subelement from hostvars for all ansible_play_hosts in a single line

2021-05-07 Thread jean-christophe manciot
The use case is to run a specific task once over all hosts only when at 
least one host failed the previous task.

It could be something like:

- name: How to list a subelement from hostvars for all 
ansible_play_hosts
  gather_facts: True
  hosts: all
  strategy: debug
  tasks:
- name: Run first task
  include_tasks: "task.yml"
  register: return_task
  ignore_errors: yes

- name: Run second task when first task has failed on at 
least one playing host
  debug:
msg: "first task has failed on at least one of: {{ 
ansible_play_hosts }}"
  when: ansible_play_hosts | map('extract', hostvars, 
'return_task.failed') | list is any

The conditional makes the last task fail with:
'ansible.vars.hostvars.HostVarsVars object' has no attribute 
'return_task.failed'

Any suggestion to correct the conditional?

-- 
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/7a643870-3cd5-49ad-b699-5ea83d1ee4ben%40googlegroups.com.


Re: [ansible-project] Re: Optimizing 'gather_facts' response time over networking devices

2021-04-13 Thread jean-christophe manciot
... with *gather_facts: yes*. (end of last sentence)

-- 
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/528f5148-e396-45c4-aff4-6aa545ac19b2n%40googlegroups.com.


Re: [ansible-project] Re: Optimizing 'gather_facts' response time over networking devices

2021-04-13 Thread jean-christophe manciot
@Brian Coca

I have already tried to set *gathering=smart* in ansible.cfg or *export 
ANSIBLE_GATHERING=smart*
Nothing happens during the second play run when the *fact_caching_timeout* 
expires after the first one.

Anyway, even if it worked, there would be a major drawback: its 
*unpredictability*.

For instance, let's assume:

   - *fact_caching_timeout* expires after the first play run
   - one of the *ansible_facts* is used at the beginning of the second run 
   to perform a *group_by* with *ansible_net_version* for instance


Even if the smart feature works and kicks in after the second run begins, 
there is a high probability that the playbook will fail due to 
*ansible_net_version 
*being undefined, depending on when exactly it does kick in and how long it 
takes to retrieve the data from the remote device. 

On top of that, there is no way to run a module to gather facts when that 
variable is undefined, because there is no *setup* equivalence in the 
networking ecosystem. 
IIUC, when *gather_facts: yes* is used, it launches the correct 
platform-dependent gathering facts module based on the value of predefined 
*ansible_network_os*. And there is not a single umbrella module to take 
care of that logic.

Hence my initial wrong belief that *gather_facts: yes* would first check if 
the *fact_caching_timeout *is about to expire (for instance halfway 
through the timeout) before deciding whether to gather facts from the remote 
device or not.  With that type of logic, we could count on the fact that 
after that call, the *ansible_facts *would be accessible for sure*.*

*As a summary:*

   - the smart feature does not work over networking devices
   - even if it did, it would be:
  -unpredictable
  -unusable in some use cases where a gather_facts should be 
  avoided unless absolutely necessary
   
*Workaround*:

   - *gathering = explicit*
   - run a background playbook every halfway through the* fact_caching_timeout 
   *for all remote devices

-- 
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/06eba760-6e31-4630-be9c-a1e83b5986d3n%40googlegroups.com.


[ansible-project] Re: Optimizing 'gather_facts' response time over networking devices

2021-04-09 Thread jean-christophe manciot
I retried the playbook and I rectify a previous assertion: with '*gather_facts: 
no*', the *facts are read from the cache* as expected.
However,  I confirm that with  '*gather_facts: yes*', the *facts are always 
gathered from the remote host, regardless of the cache  timeout value.*

I also tried the same playbook over a compute node (Ubuntu server) (without 
'ansible_connection: network_cli' of course) , and I got the same results: 
the facts are always gathered from the remote device at each run with  
'*gather_facts: 
yes*'

It seems that I'm misunderstanding the real meaning of gather_facts and the 
primary goal of the thread does not seem to be implemented by ansible and 
should be manually implemented somehow by the user.
I suppose that it also means that when the cache timeout expires, all the 
'ansible_facts' data disappear from the cache and that's it. If nothing is 
done by the user to gather them from the remote device, they are not 
accessible anymore.
If the timeout expiration is supposed to trigger some background facts 
gathering from the remote device, it must happen during some playbook run, 
otherwise it is lost.

- If I run the first playbook with '*gather_facts: yes*'*, *then run it 
before the cache timeout expires with '*gather_facts: no*', the 
'ansible_facts' continue to be accessible.
- however, if I run the first playbook with '*gather_facts: yes*'*, *wait 
for the cache timeout to expire and then run it with '*gather_facts: no*', 
the 'ansible_facts' are not accessible anymore. Nothing is triggered.

Is the last behavior expected?

-- 
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/5137df6b-df57-4be5-87dd-86c1a3511991n%40googlegroups.com.


[ansible-project] Optimizing 'gather_facts' response time over networking devices

2021-04-09 Thread jean-christophe manciot
With:
- ansible 3.2.0 (pip3)
- ansible-base 2.10.7 (pip3)

The goal is twofold:
- gather the facts *only from the local cache* when the cache is not empty 
and its timeout  has not expired
- contact the remote network device *only when* the cache is empty or the 
timeout has expired

I made some tests with the following settings in /etc/ansible/ansible.cfg:
fact_caching = redis
fact_caching_timeout = 3600
fact_caching_connection = localhost:6379:0:

Making sure that redis is running with *requirepass * in 
/etc/redis/redis.conf:
$ sudo systemctl status redis
● redis-server.service - Advanced key-value store
 Loaded: loaded (/lib/systemd/system/redis-server.service; 
enabled; vendor preset: enabled)
 Active: active (running) since Fri 2021-04-09 12:09:54 CEST; 
7h ago
   Docs: http://redis.io/documentation,
 man:redis-server(1)
   Main PID: 1610 (redis-server)
 Status: "Ready to accept connections"
  Tasks: 5 (limit: 18973)
 Memory: 5.7M
 CGroup: /system.slice/redis-server.service
 └─1610 /usr/bin/redis-server 127.0.0.1:6379

Apr 09 12:09:54 host systemd[1]: Starting Advanced key-value 
store...
Apr 09 12:09:54 host systemd[1]: Started Advanced key-value store.

Running the following simple play over an IOS device:
-  "Gathering Facts" took several seconds indicating that the remote device 
has been contacted
- "Pinging remote device to create/update facts cache" was almost 
instantaneous

- name: Gathering remote device facts preferably from the cache 
  hosts: all
  vars:
ansible_connection: network_cli
  gather_facts: yes
  strategy: debug
  tasks:
- name: Pinging remote device to create/update facts cache
  ping: 

leads to:

ansible-playbook 2.10.7
  config file = /etc/ansible/ansible.cfg
  ansible python module location = 
/usr/local/lib/python3.9/dist-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.9.4 (default, Apr  4 2021, 19:38:44) [GCC 
10.2.1 20210401]
Using /etc/ansible/ansible.cfg as config file
Parsed lab/hosts inventory source with ini plugin
redirecting (type: cache) ansible.builtin.redis to 
community.general.redis
Redis connection: 
Redis>>
redirecting (type: callback) ansible.builtin.yaml to 
community.general.yaml
redirecting (type: callback) ansible.builtin.yaml to 
community.general.yaml
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
 __
< PLAYBOOK: sdxlive_gather_facts_tests.yml >
 --
\   ^__^
 \  (oo)\___
(__)\   )\/\
||w |
|| ||

1 plays in sdxlive_gather_facts_tests.yml
 __
/ PLAY [Gathering remote device facts preferably from the \
\ cache]   /
 --
\   ^__^
 \  (oo)\___
(__)\   )\/\
||w |
|| ||

 
< TASK [Gathering Facts] >
 
\   ^__^
 \  (oo)\___
(__)\   )\/\
||w |
|| ||

task path: playbooks/sdxlive_gather_facts_tests.yml:1
redirecting (type: connection) ansible.builtin.network_cli to 
ansible.netcommon.network_cli
[WARNING]: Ignoring timeout(30) for cisco.ios.ios_facts
Using module file 
/opt/ansible_collections/cisco/ios/plugins/modules/ios_facts.py
Pipelining is enabled.
<172.21.16.79> ESTABLISH LOCAL CONNECTION FOR USER: admin
<172.21.16.79> EXEC /bin/bash -c '/usr/bin/python3 && sleep 0'
ok: [XEv_Spine_31]
META: ran handlers
 ___
< TASK [Pinging remote device to create/update facts cache] >
 ---
\   ^__^
 \  (oo)\___
(__)\   )\/\
||w |
|| ||

task path: playbooks/sdxlive_gather_facts_tests.yml:8
redirecting (type: connection) 

Re: [ansible-project] How to improve the readability of block scalars when used with jinja

2021-03-18 Thread jean-christophe manciot
Thanks @sivel.
This is possible with:

   - 
   - #jinja2: lstrip_blocks: "true", trim_blocks: "true" (as first line of 
   yml task file before ---)
   - all {% and %} replaced with {%- and -%} respectively
   - both double-quotes removed from my previous example (which is normal 
   jinja actually)
   - all \\n replaced by \n in all regex_replace (n being a number)
   - maybe there are other pitfalls

-- 
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/f5f96d7e-f790-4eb6-8bf3-e8e9c554b8d0n%40googlegroups.com.


[ansible-project] How to improve the readability of block scalars when used with jinja

2021-03-17 Thread jean-christophe manciot
Hello everyone.
Block scalars are described here .

The goal is to be improve the readability of the following block scalar 
example and obtain the intended value for the variable: 
```
- set_fact:
variable: "{% if condition_1 is true %}{% if condition_2 is true 
%}'value'{% else %}'another value'{% endif %}{% else %}{{ another_variable 
}}{% endif %}"
```
I unsuccessfully tried:
```
- set_fact:
variable: >-
"{% if condition_1 is true %}
{% if condition_2 is true %}
'value'
{% else %}
'another value'
{% endif %}
{% else %}
{{ another_variable }}
{% endif %}"
```
Unfortunately, some spaces are added in the variable set value and there 
does not seem to be a way to completely "prune" them.

Am I missing something?

-- 
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/1b2e9cf5-923a-4891-878f-7d364573e2aan%40googlegroups.com.


Re: [ansible-project] Escaping special characters in regex_replace with '\' fails with: unknown escape character

2021-02-04 Thread jean-christophe manciot
@Felix Fontein
I tried to replace the double-quotes with:
-  >-: "An unhandled exception occurred while templating"
- '>: "An unhandled exception occurred while templating"

@Vladimir Botka
Fantastic :-)
I chose the last version.

-- 
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/87b6ee62-f93d-42c6-8c09-35eb80d33579n%40googlegroups.com.


[ansible-project] Re: Escaping special characters in regex_replace with '\' fails with: unknown escape character

2021-02-03 Thread jean-christophe manciot
I meant kicking , but you got the idea.

-- 
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/f57daba4-3b75-471f-91f0-2ce66146237cn%40googlegroups.com.


[ansible-project] Escaping special characters in regex_replace with '\' fails with: unknown escape character

2021-02-03 Thread jean-christophe manciot
ansible: *2.10.5* (with pip3 install)
python version = 3.9.1+ (default, Jan 20 2021, 14:49:22) [GCC 10.2.1 
20210110]

Escaping special characters in the searched string such as the following 
should be simply done with '\':
- ' --> \'
- ( --> \(
- , --> \,
- ) --> \)

However, it seems to be impossible to perform this task with a single '\' 
or even a double '\\'; for instance, running the following playbook fails:
```
---
- name: Escaping special characters in regex_replace fails
  hosts:
- localhost
  strategy: debug
  vars:
searched_string: "('string_a', 'string_b'),('string_c', 'string_d')"
  tasks:
- name: Escaping with '\' - Result expected is 
"string_a@string_b,string_c@string_d"
  set_fact:
replaced_string: "{{ searched_string |
 regex_replace('\(\'([^\']+)\'\, 
\'([^\']+)\'\)', '\\1@\\2') }}"
...
```
leads to:
```
ERROR! We were unable to read either as JSON nor YAML, these are the errors 
we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  found unknown escape character

The error appears to be in 
'escape_special_characters_in_regex_replace.yml': line 16, column 53, but 
may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

replaced_string: "{{ searched_string |
 regex_replace('\(\'([^\']+)\'\, 
\'([^\']+)\'\)', '\\1@\\2') }}"
^ here
```

I thought about using the jinja2 'replace' filter to remove all special 
characters first, but that would just be throwing the can down the road, 
right? ;-)

What am I missing?


-- 
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/f43c3555-8554-45ee-ba87-c7807e15c8b5n%40googlegroups.com.


Re: [ansible-project] How to extract a value of a field within a list of deeply nested dictionaries when some keys match other values

2020-10-16 Thread jean-christophe manciot
There are 2 issues:. 

   1. The loop can become costly as the number of entries rise: I have 
   files with hundreds (potentially thousands) of servers/workstations.
   2. it also results in as many answers as the number of entries, although 
   only one is non empty. I don't know how to set a variable with the single 
   right answer.

```
- debug:
msg: "{{ item.workstations|
   selectattr('name', 'eq', os_name)|
   selectattr('revision', 'eq', os_revision)|
   map(attribute= 'url')|
   list }}"
  loop: "{{ domain_definition }}"
  when: item.workstations|length > 0
  vars:
os_name: ubuntu1
os_revision: '2017-08-16'
```
leads to:
```
ok: [localhost] => (item={'name': 'server01', 'cluster': 'cluster0', 
'port': '8070', 'workstations': [{'name': 'name_default', 'revision': 
'revision_default', 'url': 'url_default'}]}) => {
"msg": []
}
ok: [localhost] => (item={'name': 'server11', 'cluster': 'cluster1', 
'port': '8080', 'workstations': [{'name': 'debian1', 'revision': 
'2016-06-21', 'url': 'http://debian1.example.com:9911/2016-06-21'}, 
{'name': 'ubuntu1', 'revision': '2017-08-16', 'url': 
'http://ubuntu1.example.com:9912/2017-08-16'}]}) => {
"msg": [
"http://ubuntu1.example.com:9912/2017-08-16;
]
}
ok: [localhost] => (item={'name': 'server12', 'cluster': 'cluster2', 
'port': '8090', 'workstations': [{'name': 'debian2', 'revision': 
'2018-04-15', 'url': 'http://debian2.example.com:9921/2018-04-15'}, 
{'name': 'ubuntu2', 'revision': '2020-01-12', 'url': 
'http://ubuntu2.example.com:9922/2020-01-12'}]}) => {
"msg": []
}
```
Isn't there a loopless solution? 

-- 
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/d64886dc-1d71-45dd-8ce9-56b5a5b78c90n%40googlegroups.com.


Re: [ansible-project] How to extract a value of a field within a list of deeply nested dictionaries when some keys match other values

2020-10-16 Thread jean-christophe manciot
Regarding my last remark, I probably should rather define the empty 
workstations as:
```
empty_workstations:
workstations:
  - name:
revision:
url:
```
What are your thoughts?

-- 
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/75b1d343-3735-4262-9473-c220ccc6a18en%40googlegroups.com.


Re: [ansible-project] How to extract a value of a field within a list of deeply nested dictionaries when some keys match other values

2020-10-16 Thread jean-christophe manciot
Thanks for your answer Vladimir.
Now, there is a 'workstations' attribute all the time.
However, I still cannot get 'os_url'.
The following includes your few lines:
```
- name: Extracting a value of a field deep within a list of complex 
dictionaries when some keys match other values
  hosts:
- localhost
  strategy: debug
  tasks:
- include_vars:
file: domain_definition.yml
name: domain_definition_raw

- set_fact:
domain_definition: "{{ domain_definition|default([]) + 
[my_item] }}"
  loop: "{{ domain_definition_raw.domain_definition }}"
  vars:
empty_workstations:
workstations: []
my_item: "{{ (item.workstations is defined)|
  ternary(item, 
item|combine(empty_workstations)) }}"

- name: (1) Extracting the OS URL when  is known
  vars:
- os_name: ubuntu1
- os_revision: 2017-08-16
  debug:
msg: 
"os_url: {{ domain_definition |
selectattr('workstations') |
list |
selectattr('name', 'eq', os_name) |
list |
selectattr('revision', 'eq', 
os_revision) |
map(attribute= 'url') |
first }}"
  ignore_errors: yes
```
leads to:
"The task includes an option with an undefined variable. The error was: No 
first item, sequence was empty..."
If I try to list only the workstations dictionaries with:
```
- name: (0) Listing all workstations
  debug:
msg: 
"workstations: {{ domain_definition |
  selectattr('workstations') |
  list }}"
  ignore_errors: yes
```
I get:
ok: [localhost] => {
"msg": "workstations: [{'name': 'server11', 'cluster': 'cluster1', 
'port': '8080', 'workstations': [{'name': 'debian1', 'revision': 
'2016-06-21', 'url': 'http://debian1.example.com:9911/2016-06-21'}, 
{'name': 'ubuntu1', 'revision': '2017-08-16', 'url': 
'http://ubuntu1.example.com:9912/2017-08-16'}]}, {'name': 'server12', 
'cluster': 'cluster2', 'port': '8090', 'workstations': [{'name': 'debian2', 
'revision': '2018-04-15', 'url': 
'http://debian2.example.com:9921/2018-04-15'}, {'name': 'ubuntu2', 
'revision': '2020-01-12', 'url': 
'http://ubuntu2.example.com:9922/2020-01-12'}]}]"
}

On top of that, even if I were able to extract only the 'workstations' 
dictionaries instead of the whole structure, there would also probably be 
an issue with the empty one(s) since I would not be able to access their 
non-existent nested attributes (name, version and url).

-- 
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/27ad2cab-1823-4bf0-98d7-d00194e9c548n%40googlegroups.com.


[ansible-project] How to extract a value of a field within a list of deeply nested dictionaries when some keys match other values

2020-10-15 Thread jean-christophe manciot
Let's assume the following fictional dictionary with nested lists, 
dictionaries, and strings, of arbitrary depth.:
*domain_definition.yml*
```
---
domain_definition:
- name: server01
  cluster: cluster0
  port: '8070'
- name: server11
  cluster: cluster1
  port: '8080'
  workstations:
  - name: debian1
revision: '2016-06-21'
url: http://debian1.example.com:9911/2016-06-21
  - name: ubuntu1
revision: '2017-08-16'
url: http://ubuntu1.example.com:9912/2017-08-16
- name: server12
  cluster: cluster2
  port: '8090'
  workstations:
  - name: debian2
revision: '2018-04-15'
url: http://debian2.example.com:9921/2018-04-15
  - name: ubuntu2
revision: '2020-01-12'
url: http://ubuntu2.example.com:9922/2020-01-12
```
The goal is to extract as efficiently as possible (the original file can be 
quite large) a workstation  matching:

   - a workstation os  with its : for instance ubuntu1 
   <--> 2017-08-16
   - only a workstation os : for instance debian2

As you can see from example above, the  key is not always set.
A major constraint is to avoid "json_query" as it cannot deal properly with 
keys  containing a colon (as it may happen in my real context), last time I 
checked

I unsuccessfully tried different tasks, including the following:
```
- name: Extracting a value of a field deep within a list of complex 
dictionaries when some keys match other values
  hosts:
- localhost
  strategy: debug
  tasks:
- name: Including domain_definition
  include_vars: "domain_definition.yml"

- name: Extracting the OS URL when  is known
  vars:
- os_name: ubuntu1
- os_revision: 2017-08-16
  debug:
msg: 
"os_url: {{ domain_definition |
selectattr('workstations') |
list |
selectattr('name', 'eq', os_name) |
list |
selectattr('revision', 'eq', 
os_revision) |
map(attribute= 'url') |
first }}"
  ignore_errors: yes

- name: Extracting the OS URL when  is known
  vars:
- os_name: ubuntu1
- os_revision: 2017-08-16
  debug:
msg: 
"os_url: {{ domain_definition |
selectattr('workstations.name', 'eq', 
os_name) |
list |
selectattr('workstations.revision', 
'eq', os_revision) |
map(attribute= 'workstations.url') |
first }}"
  ignore_errors: yes

- name: Extracting the OS URL when  is unknown
  vars:
- os_name: debian2
  debug:
msg: 
"os_url: {{ domain_definition |
selectattr('workstations') |
list |
selectattr('name', 'eq', os_name) |
map(attribute= 'url') |
first }}"
  ignore_errors: yes

- name: Extracting the OS URL when  is unknown
  vars:
- os_name: debian2
  debug:
msg: 
"os_url: {{ domain_definition |
selectattr('workstations.name', 'eq', 
os_name) |
map(attribute= 'workstations.url') |
first }}"
  ignore_errors: yes
```
I always get the error msg: 'dict object' has no attribute 'workstations'.

Any suggestion?

-- 
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/de8ac1bb-011e-4e1b-8319-3a776d86d4ffn%40googlegroups.com.


Re: [ansible-project] How to extract one attribute from a list of dictionaries

2020-04-24 Thread jean-christophe manciot
Yes, of course: you can define the variable anywhere, including in the 
hosts file.
There are 2 differences though:

   - the precedence 
   

   .
   - the way the variables are defined: the inventory file is not a yaml 
   file (ini format). Refer to this post 
   

 
   for how to define your dictionary(ies) in the hosts file.

-- 
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/8a4beb04-ffdd-44fb-b3d8-f0149eff5a61%40googlegroups.com.


Re: [ansible-project] How to extract one attribute from a list of dictionaries

2020-04-21 Thread jean-christophe manciot
Thanks. :-)

-- 
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/dd8ef30c-6483-4a96-a065-a5810eba51b4%40googlegroups.com.


Re: [ansible-project] How to extract one attribute from a list of dictionaries

2020-04-21 Thread jean-christophe manciot
That's what I meant (just a typo in the structure).
 
 

>
>

-- 
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/718d857b-7a8f-435e-9b9f-97170cab0813%40googlegroups.com.


[ansible-project] Re: How to extract one attribute from a list of dictionaries

2020-04-21 Thread jean-christophe manciot
 I'm using:
- ansible 2.9.7
- python 3.8.2 

-- 
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/4ba2f72d-b222-46c9-a251-c69b79301809%40googlegroups.com.


[ansible-project] How to extract one attribute from a list of dictionaries

2020-04-21 Thread jean-christophe manciot
Let's suppose the following list of dictionaries:
- domain_definition:
- name: server11
  cluster: cluster1
  port: '8080'
- name: server12
  cluster: cluster2
  port: '8090'
- name: server21
  cluster: cluster3
  port: '9080'
- name: server22
  cluster: cluster4
  port: '9090'

The aim is to extract all ```name: 'value'``` from ```domain_definition``` 
into a list:
- name: server11
- name: server12
- name: server21
- name: server22

I tried for instance the following:
- set_fact:
domain_definition_names_list: "{{ ['name'] | 
 intersect(domain_definition.keys()|list) 
}}"
but it fails:
fatal: [localhost]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The 
error was: 'list object' has no attribute 'keys'...}

Any suggestion?



-- 
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/0d4429c3-3a17-4078-ade4-38cd1f48ed83%40googlegroups.com.


Re: [ansible-project] How to extract a value of a field within a list of dictionaries when a key matches another value

2020-03-26 Thread jean-christophe manciot
Same result with your second proposal.

-- 
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/7143cacb-46da-4263-a877-84cf9de1e559%40googlegroups.com.


Re: [ansible-project] How to extract a value of a field within a list of dictionaries when a key matches another value

2020-03-26 Thread jean-christophe manciot
 
I also tried that one but it also fails: 
 
- name: test with json_query 1/2
  vars:
- domain_definition:
- name: server11
  cluster: cluster1
  port: '8080'
- name: server12
  cluster: cluster2
  port: '8090'
- name: server21
  cluster: cluster3
  port: '9080'
- name: server22
  cluster: cluster4
  port: '9090'
  debug:
var: "{{ domain_definition|
 json_query('[?name==`server21`].port') }}"
  ignore_errors: yes

leads to: 
  
ok: [localhost] => {
"": "VARIABLE IS NOT DEFINED!: "
}

What version of ansible are you using?
I have:
- ansible 2.9.6 (from pypi)
- python3 version = 3.7.7 (default, Mar 12 2020, 02:00:38) [GCC 9.2.1 
20200306]

-- 
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/cca5c8b6-3634-40e7-b111-af7f08addf08%40googlegroups.com.


[ansible-project] How to extract a value of a field within a list of dictionaries when a key matches another value

2020-03-26 Thread jean-christophe manciot
Let’s suppose the following data structure:
- domain_definition:
- name: server11
  cluster: cluster1
  port: '8080'
- name: server12
  cluster: cluster2
  port: '8090'
- name: server21
  cluster: cluster3
  port: '9080'
- name: server22
  cluster: cluster4
  port: '9090'
The goal is for example to *extract the port number when 
domain_definition.name == 'server21'.*

As simple as it sounds, I tried the following tasks with 'json_query' and 
'selectattr': they all failed:
- name: test with json_query 1/2
  vars:
- domain_definition:
- name: server11
  cluster: cluster1
  port: '8080'
- name: server12
  cluster: cluster2
  port: '8090'
- name: server21
  cluster: cluster3
  port: '9080'
- name: server22
  cluster: cluster4
  port: '9090'
  debug:
var: "{{ domain_definition | 
json_query('domain_definition[?name==`server21`].port') }}"
  ignore_errors: yes

- name: test with json_query 2/2
  vars:
- domain_definition:
- name: server11
  cluster: cluster1
  port: '8080'
- name: server12
  cluster: cluster2
  port: '8090'
- name: server21
  cluster: cluster3
  port: '9080'
- name: server22
  cluster: cluster4
  port: '9090'
  debug:
var: "{{ . | 
json_query('domain_definition[?name==`server21`].port') }}"
  ignore_errors: yes

- name: test with selectattr 'match'
  vars:
- domain_definition:
- name: server11
  cluster: cluster1
  port: '8080'
- name: server12
  cluster: cluster2
  port: '8090'
- name: server21
  cluster: cluster3
  port: '9080'
- name: server22
  cluster: cluster4
  port: '9090'
  debug:
var: "{{ domain_definition | selectattr('name', 'match', 
'^server21$')).port }}"
  ignore_errors: yes

- name: test with selectattr 'match'
  vars:
- domain_definition:
- name: server11
  cluster: cluster1
  port: '8080'
- name: server12
  cluster: cluster2
  port: '8090'
- name: server21
  cluster: cluster3
  port: '9080'
- name: server22
  cluster: cluster4
  port: '9090'
  debug:
var: "{{ domain_definition | selectattr('name', 'equalto', 
'server21')).port }}"
  ignore_errors: yes

leads to:
ASK [yang : test with json_query 1/2] 
***
fatal: [XEv_Spine_31]: FAILED! => {
"msg": "template error while templating string: Expected an expression, 
got 'end of print statement'. String: {{}}"
}
...ignoring

TASK [yang : test with json_query 2/2] 
***
fatal: [XEv_Spine_31]: FAILED! => {
"msg": "template error while templating string: unexpected '.'. String: 
{{ . | json_query('domain_definition[?name==`server21`].port') }}"
}
...ignoring

TASK [yang : test with selectattr 'match'] 
***
fatal: [XEv_Spine_31]: FAILED! => {
"msg": "template error while templating string: unexpected ')'. String: 
{{ domain_definition | selectattr('name', 'match', '^server21$')).port }}"
}
...ignoring

TASK [yang : test with selectattr 'match'] 
***
fatal: [XEv_Spine_31]: FAILED! => {
"msg": "template error while templating string: unexpected ')'. String: 
{{ domain_definition | selectattr('name', 'equalto', 'server21')).port }}"
}
...ignoring

What am I doing wrong?
Maybe there is an easier way to achieve that.

-- 
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/3faa234a-244c-497b-9e29-6b197bc092fe%40googlegroups.com.


Re: [ansible-project] How to replace all occurrences of '_' by '-' in all keys of a YAML complex object

2020-03-02 Thread jean-christophe manciot
There is an issue: the values of key-1, key-2 and key--3 are modified. It 
seems that the translation does not stop at the first encountered key colon.
With ansible 2.9.5 from pip3, I get:
"ansible_facts": {
"my_list2": [
{
"key-1": "$.*?/|\\^(){}+@[]&_-",
"key-2": [
"$.*?/|\\^(){}+@[]&_-",
"{ }",
"value23"
],
"key-3": "value31",
"key-4": [
"value41",
"value42"
],
"key-5": "value51"
},
{
"_key-2": [
"value24",
"value25"
],
"key--3": "$.*?/|\\^(){}+@[]&_-",
"key-1-": "value12",
"key-5---": "value52"
}
]
},

 Also, in reality, the input data can be structured in other ways than a 
list, such as:
my_struct:
  config:
key_1: '$.*?/|\^(){}+@[]&_-'
key_2:
  - '$.*?/|\^(){}+@[]&_-'
  - "{\_}"
  - value23

We cannot assume that the passed data are a list or not.

-- 
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/23468d02-63a2-45bf-8448-89dcfef30718%40googlegroups.com.


[ansible-project] How to replace all occurrences of '_' by '-' in all keys of a YAML complex object

2020-02-27 Thread jean-christophe manciot


For instance, let's consider the following YAML data:
  - key_1: '$.*?/|\^(){}+@[]&_-'
key_2:
  - '$.*?/|\^(){}+@[]&_-'
  - "{\_}"
  - value23
key_3: value31
key_4:
  - value41
  - value42
key_5: value51
  - key_1_: value12
_key_2:
  - value24
  - value25
key__3: '$.*?/|\^(){}+@[]&_-'
key_5___: value52


The goal is:

- to replace all '_' from the keys into '-', except for the first character 
which may be a '_';

- all values must remain untouched

- the name and number of the keys are variable

- the depth of "recursiveness" is variable and unlimited


For instance here, the translation would result as:
  - key-1: '$.*?/|\^(){}+@[]&_-'
key-2:
  - '$.*?/|\^(){}+@[]&_-'
  - "{\_}"
  - value23
key-3: value31
key-4:
  - value41
  - value42
key-5: value51
  - key-1-: value12
_key-2:
  - value24
  - value25
key--3: '$.*?/|\^(){}+@[]&_-'
key-5---: value52

Any suggestion?

-- 
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/000d7d79-ddbe-45c9-89c4-2eef73979b2f%40googlegroups.com.


Re: [ansible-project] How to translate a complex sed expressions into regex_replace

2020-02-25 Thread jean-christophe manciot

{{ var }} is already a yaml object: it is the list of dictionaries.

-- 
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/0e5d0f81-815d-4e8f-bf44-f5750aae08aa%40googlegroups.com.


[ansible-project] How to translate a complex sed expressions into regex_replace

2020-02-25 Thread jean-christophe manciot
ansible 2.9.5 (pip3)

The following sed command:
- function is to replace all '_' by '-' in all keys only (and not values) 
within a list of dictionaries in memory
- uses character classes which are probably not supported by regex_replace:
  + [:blank:]: space and tab
  + [:space:]: tab, newline, vertical tab, form feed, carriage return, and 
space
- a label and loop

sed -E ':l; 
s/^([[:blank:]]*(-[[:blank:]]*)?[^[:space:]:_]*)_([^[:space:]:]*:)/\1-\3/; 
tl;'

I need to use the previous sed over a yaml list of dictionaries.
If I use a shell command (such as printf "%s\n" "{{ var }}" | sed ...), the 
resulting structure is not yaml anymore.

I could save the var into a file, perform the function over the file and 
reopen the resulting file into memory.
However, I'm looking for a more efficient way, that is perform only memory 
operation(s).

Is there a way to translate the previous sed expression into a 
regex_replace command?
Or maybe there is another more ansible way to perform the same function?

-- 
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/dfd15e15-8b8d-4601-8b11-b3a69a529fdb%40googlegroups.com.


Re: [ansible-project] How to split a json key containing a colon into 2 keys

2020-02-11 Thread jean-christophe manciot
I'm impressed. It works perfectly. Only the first key is split as expected..

On Friday, February 7, 2020 at 7:12:40 PM UTC+1, Vladimir Botka wrote:
>
> On Fri, 7 Feb 2020 08:14:56 -0800 (PST) 
> jean-christophe manciot > wrote: 
>
> > I receive some json data which *sometimes* start with the first key 
> within 
> > "json_data" variable containing a ':', such as: 
> > "json_data": { 
> > "key1:key2": { 
> > "key3": [ 
> > "value31", 
> > "value32", 
> > "..." 
> > ] 
> > } 
> > } 
> > 
> > The goal is to split "key1:key2" into 2 keys so that we end up with: 
> > "json_data": { 
> > "key1": { 
> > "key2": { 
> > "key3": [ 
> > "value31", 
> > "value32", 
> > "..." 
> > ] 
> > } 
> > } 
> > } 
>
>
> Try this 
>
>- set_fact: 
> json_data1: "{{ json_data1|default({})|combine(my_key) }}" 
>   vars: 
> my_keys: "{{ item.key.split(':') }}" 
> my_keys_length: "{{ my_keys|length == 1 }}" 
> my_key: "{{ my_keys_length| 
> ternary({my_keys[0]: item.value}, 
> {my_keys[0]: 
> {my_keys[1:]|join(':')|default('NA'): 
>  item.value}}) }}" 
>   loop: "{{ json_data|dict2items }}" 
>
> HTH, 
>
> -vlado 
>

-- 
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/1f82855d-3090-4502-87f1-d3a6aec28aa6%40googlegroups.com.


[ansible-project] How to split a json key containing a colon into 2 keys

2020-02-07 Thread jean-christophe manciot
I receive some json data which *sometimes* start with the first key within 
"json_data" variable containing a ':', such as:
"json_data": {
"key1:key2": {
"key3": [
"value31",
"value32",
"..."
]
}
}

The goal is to split "key1:key2" into 2 keys so that we end up with:
"json_data": {
"key1": {
"key2": {
"key3": [
"value31",
"value32",
"..."
]
}
}
}

The key names are completely variable so I also have no idea about how to 
implement the play condition so that this transformation is done only when 
the first key within "json_data" contains a single colon. 

I am aware that a colon should not be part of any key, but I have no 
control over that.

Any suggestion?

-- 
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/1ec78535-917f-4758-b35b-a598db163d9b%40googlegroups.com.


Re: [ansible-project] Filtering a complex list of dictionaries into another list of dictionaries in an efficient way

2020-01-23 Thread jean-christophe manciot
fantastic! :-)

My real use case is little more complex:

   1. there are other attributes like "key1" or similar to "list" (within 
   top-level "list") which must be taken into account
   2. the regex filter on "key3" is a little more complex (a list of 
   logical OR)

I tried to to implement the 2 points by expanding your solution, and it 
works *beautifully,* despite the fact that some new keys are themselves 
list of dictionaries, instead of simple lists or strings.
I used something like:
...
  vars:
keys: "{{ ['key1', 'key3', 'key4', 'key5', 'key6']|
  intersect(item.keys()|list) }}"

  loop: "{{ list|
selectattr('key3', 'regex', 'regex1|regex2|regex3')|
list }}"



Finally, is there an *online documentation* that you would recommend to 
learn all the necessary tools to be able to perform such great filters (the 
ansible documentation is very sparse on that subject)?
For instance, if I need to add another constraint like another attribute 
must match another regex alongside "key3" (as an logical AND), I have no 
clue.

-- 
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/2e96aff4-e9ed-4a9e-8034-3b904f75bd10%40googlegroups.com.


[ansible-project] Filtering a complex list of dictionaries into another list of dictionaries in an efficient way

2020-01-22 Thread jean-christophe manciot
*ansible 2.9.4*

Let's assume the following list of dictionaries:

list:
-   key1:
- 'abc'
- 'def'
key2: 'ghi'
key3: 'jkl'
-   key1:
- 'mno'
- 'pqr'
key3: 'stu'
key4: 'dfg'
-   key1:
- 'vwx'
- 'yza'
key3: 'okl'
key4: 'azel'



The goal is threefold:

   - to extract only the records which match a regex criteria over one of 
   the keys, for instance if "key3" contains a 'k'.
   - to keep only a selection of keys, for instance only "key1", "key3" and 
   "key4", which may not always be present
   - to be as efficient as possible with thousands of records

In this case, we expect the resulting list:
result_list:
-   key1:
- 'abc'
- 'def'
key3: 'jkl'
-   key1:
- 'vwx'
- 'yza'
key3: 'okl'
key4: 'azel'


The difficulty I'm experiencing concerns "key1" as a list. 

My solution works without considering "key1":

- name: Filtering a complex list of dictionaries into another list of 
dictionaries
  vars:
"list": [
{
"key1": [
"abc",
"def"
],
"key2": "ghi",
"key3": "jkl"
},
{
"key1": [
"mno",
"pqr"
],
"key3": "stu",
"key4": "dfg"
},
{
"key1": [
"vwx",
"yza"
],
"key3": "okl",
"key4": "azel"
}
]
  set_fact:
result_list: "{{ result_list|default([]) + [ {'key3': item.key3, 
'key4': item.key4|default('')} ] }}"
  loop: "{{ list }}"
  when: item.key3 | regex_search('(^.*k.*$)')


leads to:

TASK [yang : Filtering a complex list of dictionaries into another list of 
dictionaries] 
*
task path: test.yml:133
<172.16.136.116> attempting to start connection
<172.16.136.116> using connection plugin network_cli
<172.16.136.116> found existing local domain socket, using it!
<172.16.136.116> updating play_context for connection
<172.16.136.116> 
<172.16.136.116> local domain socket path is .ansible/pc/521e859c25
ok: [TEST] => (item={'key1': ['abc', 'def'], 'key2': 'ghi', 'key3': 'jkl'}) 
=> {
"ansible_facts": {
"result_list": [
{
"key3": "jkl",
"key4": ""
}
]
},
"ansible_loop_var": "item",
"changed": false,
"item": {
"key1": [
"abc",
"def"
],
"key2": "ghi",
"key3": "jkl"
}
}
skipping: [TEST] => (item={'key1': ['mno', 'pqr'], 'key3': 'stu', 'key4': 
'dfg'})  => {
"ansible_loop_var": "item",
"changed": false,
"item": {
"key1": [
"mno",
"pqr"
],
"key3": "stu",
"key4": "dfg"
},
"skip_reason": "Conditional result was False"
}
ok: [TEST] => (item={'key1': ['vwx', 'yza'], 'key3': 'okl', 'key4': 'azel'}) 
=> {
"ansible_facts": {
"result_list": [
{
"key3": "jkl",
"key4": ""
},
{
"key3": "okl",
"key4": "azel"
}
]
},
"ansible_loop_var": "item",
"changed": false,
"item": {
"key1": [
"vwx",
"yza"
],
"key3": "okl",
"key4": "azel"
}
}


How can we insert "*key1*" in the picture?

Also, when the list contains thousands of records, it may be less compute 
intensive to use ```*json_query*```, but I don't know how to use it in this 
context.

-- 
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/86948dc2-7abd-4c3c-8012-ea89e34a8969%40googlegroups.com.


[ansible-project] Re: How to work around PERSISTENT_COMMAND_TIMEOUT

2019-09-13 Thread jean-christophe manciot
I have just found a mini workaround with *ansible_command_timeout *which 
can be used per command (instead of modifying the global 
PERSISTENT_COMMAND_TIMEOUT for all commands):
  vars:
ansible_connection: network_cli
ansible_network_os: nxos
ansible_password: "{{ connections.ssh.password }}"
ansible_user: "{{ connections.ssh.username }}"
*ansible_command_timeout*: "{{ firmware.transfer_timeout }}"
  net_get:
...


However, if there is a real issue during the transfer so that the target 
does not answer any more, we have to wait until *ansible_command_timeout *times 
out before moving on, although the transfer issue may have happened at the 
beginning of the transfer. 

-- 
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/7f4d2dfe-db66-4946-bcb4-af50d0a4021f%40googlegroups.com.


[ansible-project] How to work around PERSISTENT_COMMAND_TIMEOUT

2019-09-13 Thread jean-christophe manciot
Hi there, 

Every ansible network command has a PERSISTENT_COMMAND_TIMEOUT which 
"controls the amount of time to wait for response from remote device before 
timing out persistent connection". 

Actually, when using the *net_get* module with a very large file, the 
command times out during the transfer, even though both sides are still 
exchanging SCP messages, meaning the transfer is still underway & active. 

Shouldn't it time out **only** when there is no more communication between 
both nodes for the PERSISTENT_COMMAND_TIMEOUT period of time?

- Either we are supposed to guess how long each transfer is going to last, 
depending on the size of the file, the network load, the CPU load on both 
sides & in between and so on and adjust  PERSISTENT_COMMAND_TIMEOUT 
accordingly
- or there is way to avoid the command to timeout during the transfer, 
while still timing out when the target does not answer anymore after some 
time

Any suggestion?

-- 
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/1e71120f-c928-4d7f-a469-ce3c63251050%40googlegroups.com.


[ansible-project] Regex pattern for selecting an inverted word on a list of strings

2019-08-06 Thread jean-christophe manciot
The goal is to* select all the lines of a list which do NOT contain a 
specific word*.

For instance, "{{ files }}" contains a list of filenames. We need to select 
all the filenames which do not include the word 'all'.

So with:
files:
- aaa
- bbb
- 
- d-all-ddd

The expected result would be:
files_filtered::
- aaa
- bbb
- 

I unsuccessfully tried:
  set_fact:
files_filtered: "{{ files | select('match', '^(.*^((?!all).)*.+)$') 
| list }}"

The result is empty.

Any suggestion?

-- 
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/c700b4dd-1e1c-47f8-98c9-78f64b1adc63%40googlegroups.com.


[ansible-project] Re: Converting a multi-lines string into a list

2019-07-29 Thread jean-christophe manciot
Using a python string method on an ansible variable: brilliant!


-- 
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/ea5300dd-afaf-4bf5-9901-9c774230a7d6%40googlegroups.com.


[ansible-project] Converting a multi-lines string into a list

2019-07-28 Thread jean-christophe manciot
A call to ansible uri module returns a *multi-line string*:

- set_fact:
fact: "{{ return_uri_json.body }}"

ok: [target] => {
"ansible_facts": {
"fact": "line_1\nline_2\nline_3\nline_4\n"
}, 
"changed": false
}


The goal is to convert "fact" into:
- "line_1"
- "line_2"
- "line_3"
- "line_4"

I couldn't find any jinja2 filter for that task. Maybe I've missed 
something.
Any suggestion?

-- 
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/1317e235-6f96-4d9b-9c5a-d27ff974663c%40googlegroups.com.


[ansible-project] Re: How to check when at least one element of a list is not null

2019-06-27 Thread jean-christophe manciot


[image: thumb-up.jpeg]

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/7e7377d2-9693-4f5a-a544-336c4e47c3e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] How to check when at least one element of a list is not null

2019-06-26 Thread jean-christophe manciot
The real goal here is to perform an action only when at least one of 
*inventory_hostname* groups from *group_names* matches one group from 
another list of groups (in this example: ```git|local|ntopng```) .

This is defined within a *role*, so I cannot use the list ```  - hosts:```.

For instance, to print all hostname variables only when inventory_hostname 
belong to at least one of git,local or ntopng groups,I tried this:
- name: Print all variables for "{{ inventory_hostname }}" system device 
(except any role parameters)
  debug: 
var: hostvars[inventory_hostname]
  when: group_names | map('regex_search', "^(git|local|ntopng)$") | list | 
length > 0

It does not work as expected, because the length always returns the number 
of groups within group_names. It seems that length considers a null element 
as defined and counts it.
"is defined" does not work either for the same reason.

I cannot find a way to test if *at least one element of the search is not 
null*.

Any suggestion?
 

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/cf7f8b7e-72e4-4988-b362-57bde4101a76%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: Extracting a substring with regex_replace fails

2018-02-16 Thread jean-christophe manciot
You made it, bravo!
However,  this solution is not better than the regex search, because the 
whole point of using the xml module was to make it resilient to change. As 
soon as the URI {http://docs.oasis-open.org/ns/xri/xrd-1.0 
}
 
changes, the call to access both variables will fail. 

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/6aafa5d2-a54d-41af-96fa-80b80245bc38%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: Extracting a substring with regex_replace fails

2018-02-15 Thread jean-christophe manciot
@Kai Stian Olstad
Thanks for your answer: yes, it works.

@Felix Fontain
I have no XML skills. I nevertheless took a look at the ansible xml module, 
and it's obscure to me.
I think you're right though: it'd be better to read XML appropriately with 
that module rather than use some regex filter which can be easily defeated 
if the input changes in the future.
So I tried something, but of course it does not work (api_root_filename is 
the file containing the xml multi-line string):

- name: Reading RESTconf release 
  xml:
attribute: rel
content: attribute
path: "{{ api_root_filename }}"
xpath: /XRD/Link
  register: return_restconf_release

- name: Showing attribute value
  debug:
var: return_restconf_release.matches[0].Link.rel

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/4dddbcb7-5adb-4cbc-8455-2f5c43418b4b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: Extracting a substring with regex_replace fails

2018-02-14 Thread jean-christophe manciot

>
>
>
I  have the same difficulty with:
ansible 2.4.3.0 (detached HEAD 8a7f9beab7) last updated 2018/02/14 16:11:16 
(GMT +200)
 

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/fb5f8d45-ea18-42ab-bd6e-ddc19fbec3b3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Extracting a substring with regex_replace fails

2018-02-14 Thread jean-christophe manciot
ansible 2.6.0 (devel da5cf72236) last updated 2018/02/14 14:29:49 (GMT +200)

The task seems obvious at first glance but it appears to be difficult to 
implement in this context.
The string is:
host_meta: "\n
\n"

I need to extract the values of rel and href into api_release and api_root.
I unsuccessfully tried the following:
- set_fact: api_release="{{ host_meta | regex_replace('^.* rel=(.*) .*$', 
'\\1') }}"
  when: host_meta is defined

- set_fact: api_root="{{ host_meta | regex_replace('^.* href=(.*)/>.*$', 
'\\1') }}"
  when: host_meta is defined

Both variables contain the whole string instead of the corresponding 
substring, which should be:
api_release: 'restconf'
api_root: /restconf

I have already successfully used this filter in other contexts. 
What am I missing here? Is the filter confused by the string?

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/5aeaac3f-277e-4df8-af4b-95f419979447%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: Passing json data without losing the double quotes

2017-11-01 Thread jean-christophe manciot
it seems to be a bash behaviour which removes the double quotes before 
passing the parameter.

The solution is to *pass the parameter in simple-quoted double-quotes* like 
that:
- name: Checking for duplicate keys in json answer
  local_action: script 
/home/actionmystique/Bash/Scripts/json-check-duplicate-keys.sh '"{{ 
uri_return.content }}"'
  register: json_check_duplicate_keys
  failed_when: json_check_duplicate_keys.stderr 
...
"Checking /tmp/data.json...", 
"", 
"Duplicate keys found in /tmp/data.json:", 
"* subinterface", 
"* subinterface", 
"* subinterface", 
"* subinterface", 
"* subinterface", 
"* subinterface", 
"* subinterface", 
"* subinterface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface", 
"* interface"


-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/43ca6d23-6ce3-41c0-94df-2325a4faf924%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Passing json data without losing the double quotes

2017-11-01 Thread jean-christophe manciot
ansible 2.5.0 (devel 1068aa3ce7) last updated 2017/11/01 11:34:22 (GMT +200)
  config file = /etc/ansible/ansible.cfg
  configured module search path = 
[u'/home/actionmystique/Ansible/git-yang-networkop/ansible-101/library']
  ansible python module location = 
/home/actionmystique/src/Ansible/git-ansible/lib/ansible
  executable location = 
/home/actionmystique/src/Ansible/git-ansible/bin/ansible
  python version = 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 
20170118]

I wrote a bash script to check the json dictionary returned by the uri 
module for duplicate keys.
For instance, Sending "GET" to 
"https://NX_OSv_9k_Spine_31/restconf/data/openconfig-interfaces:interfaces?content=config;
 
returns a json string with multiple duplicate keys:
"content": 

[ansible-project] Re: Combining 2 lists of strings with a separator

2017-09-29 Thread jean-christophe manciot
The true solution is tricky:

- name: Building partial name of installed MTX packages
  set_fact:
installed_mtx_packages_partial_name_item: "{{ item.0 }}-{{ item.1 
}}"
  with_together: 
- "{{ installed_mtx_packages_name }}"
- "{{ installed_mtx_packages_version }}"
  register: installed_mtx_packages_partial_name_result

- name: Building partial list of name of installed MTX packages
  set_fact: 
installed_mtx_packages_partial_name: "{{ 
installed_mtx_packages_partial_name_result.results | 
map(attribute='ansible_facts.installed_mtx_packages_partial_name_item') | 
list }}"

leads to the true list:
"installed_mtx_packages_partial_name": [
"mtx-infra-1.0.0-r1705191346", 
"mtx-device-1.0.0-r1705191346", 
"mtx-grpc-agent-1.0.1-r1705191346", 
...
]

There might be a more elegant solution.

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/48556517-9473-4611-9f5a-8797fd851edd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: Combining 2 lists of strings with a separator

2017-09-29 Thread jean-christophe manciot
@branko 

Sorry for the false positive, *actually your solution does not work*: it 
loops over all versions as expected, but it puts each item at the end of 
each name for each iteration, so the final list is (as shown in previous 
post):
"installed_mtx_packages_partial_name": [
"mtx-device-1.0.1-r1705191346", 
"mtx-grpc-agent-1.0.1-r1705191346", 
"mtx-infra-1.0.1-r1705191346", 
...
]
As you can see (and I missed it the first time), all the elements have the 
same version, instead of its own.

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/e299f8ba-ade2-442b-8790-3379e95c982d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Combining 2 lists of strings with a separator

2017-09-28 Thread jean-christophe manciot
@branko 

- you're right about the typo.
- your "'\\1-' + item" solution works beautifully :-)

The solution hence is:
- name: Building partial name of installed MTX packages
  set_fact:
installed_mtx_packages_partial_name: "{{ 
installed_mtx_packages_name | map('regex_replace', '(.*)', '\\1-' + item) | 
list }}"
  with_items: "{{ installed_mtx_packages_version }}"

The result is:
"installed_mtx_packages_partial_name": [
"mtx-device-1.0.1-r1705191346", 
"mtx-grpc-agent-1.0.1-r1705191346", 
"mtx-infra-1.0.1-r1705191346", 
...
]

Thanks!

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/7bc53f5a-4416-495c-9cd9-dece990d26cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Combining 2 lists of strings with a separator

2017-09-27 Thread jean-christophe manciot
hello guys,
I need to build filenames based on 3 parts: basename, version and trailing 
'.x86_64.rpm'.
Let's focus on the first 2 parts:
The first one is:
"installed_mtx_packages_name": [
"mtx-device", 
"mtx-grpc-agent", 
"mtx-infra", 
...
]

The second one is:
"installed_mtx_packages_version": [
"1.0.0-r1705191346", 
"1.0.1-r1705191346", 
"1.0.0-r1705170158", 
...
]

the goal is to obtain this:
 "installed_mtx_packages_partialname": [
"mtx-device-1.0.0-r1705191346", 
"mtx-grpc-agent-1.0.0-r1705191346", 
"mtx-infra-1.0.0-r1705170158", 
...
]

I've tried several strategies, including:
- name: Building partial name of installed MTX packages
  set_fact:
installed_mtx_packages_partialname: "{{ installed_mtx_packages_name 
| map('regex_replace', '(.*)', '\\1-item') | list }}"
  with_items: installed_mtx_packages_version

But of course the item is considered as a string here instead of a variable.

If I move the ' just before item, I get the error:
"template error while templating string: expected token ',', got 'string' "

Any suggestion?

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/38fa96c7-a7c1-46a6-914c-f345cc094654%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: ansible 2.0.1.0 run show version with Cisco ASA-55x0

2016-11-09 Thread jean-christophe manciot

>
>
> The issue has been solved; for instance in a role with:
- name: Fetching IPv4_Routing_Table from the remote node
  asa_command:
provider: "{{ connections.ssh }}"
commands:
  - "show route summary"
  - "show route"
  register: table

we get the requested information.

I'm using *ansible 2.3.0 commit **20161109.65f019f*, but stable 2.2.0 
should work as fine as the current unstable one in this situation.

 

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d1624cf7-ca5c-42ce-90de-9b0784b0269e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: How to save stdout_lines to a file without losing the newlines

2016-11-08 Thread jean-christophe manciot
@Peter Sprygada: I'm now facing a more complicated case where I have 
trouble accessing stdout[0]:

- name: Fetching PACL_Table on all L2 interfaces from the remote node
  ios_command:
provider: "{{ connections.ssh }}"
commands:
  - "show run interface {{ net_item.key }} | include 
^interface|access-group"
  with_dict: "{{ ansible_net_interfaces }}"
  when: net_item.value.ipv4.address is not defined
  loop_control:
loop_var: net_item
  register: pacl_tables

- name: Printing pacl_tables.results
  debug: var=pacl_tables.results

...leads to:
ok: [IOSv_L2_10] => {
"pacl_tables.results": [
{
"_ansible_item_label": {
"key": "GigabitEthernet1/2", 
"value": {
"bandwidth": 100, 
"description": "Connected to 
[u'IOSv_Leaf_16.actionmystique.net'] on its port [u'Gi0/0']", 
"duplex": "Full", 
"ipv4": null, 
"lineprotocol": "up (connected) ", 
"macaddress": "0036.2586.7e06", 
"mediatype": "unknown media type", 
"mtu": 1500, 
"operstatus": "up", 
"type": "iGbE"
}
}, 
"_ansible_item_result": true, 
"_ansible_no_log": false, 
"_ansible_parsed": true, 
"changed": false, 
"invocation": {
"module_args": {
"auth_pass": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
"authorize": true, 
"commands": [
"show run interface GigabitEthernet1/2 | include 
^interface|access-group"
], 
"host": "172.21.100.210", 
"interval": 1, 
"match": "all", 
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
"port": 22, 
"provider": {
"auth_pass": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
"authorize": true, 
"host": "172.21.100.210", 
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
"port": 22, 
"ssh_keyfile": "~/.ssh/id_rsa", 
"timeout": 10, 
"transport": "cli", 
"username": "admin", 
"version": 2
}, 
"retries": 10, 
"ssh_keyfile": "/root/.ssh/id_rsa", 
"timeout": 10, 
"transport": "cli", 
"use_ssl": true, 
"username": "admin", 
"validate_certs": true, 
"wait_for": null
}, 
"module_name": "ios_command"
}, 
"net_item": {
"key": "GigabitEthernet1/2", 
"value": {
"bandwidth": 100, 
"description": "Connected to 
[u'IOSv_Leaf_16.actionmystique.net'] on its port [u'Gi0/0']", 
"duplex": "Full", 
"ipv4": null, 
"lineprotocol": "up (connected) ", 
"macaddress": "0036.2586.7e06", 
"mediatype": "unknown media type", 
"mtu": 1500, 
"operstatus": "up", 
"type": "iGbE"
}
}, 
"stdout": [
"show run interface GigabitEthernet1/2 | include 
^interface|access-$terface GigabitEthernet1/2 | include ^interface|access-g 
oup\ninterface GigabitEthernet1/2"
], 
"stdout_lines": [
[
"show run interface GigabitEthernet1/2 | include 
^interface|access-$terface GigabitEthernet1/2 | include ^interface|access-g 
oup", 
"interface GigabitEthernet1/2"
]
], 
"warnings": []
}, 
...repeated n times, for all found L2 interfaces.

However, I cannot access stdout[0] for all these interfaces with:
- name: Printing pacl_tables.results.stdout
  debug: var=print_item.stdout[0]
  with_dict: "{{ pacl_tables.results }}"
  loop_control:
loop_var: print_item
... which leads to:
fatal: [IOSv_L2_10]: FAILED! => {"failed": true, "msg": "with_dict expects 
a dict"}

How can we correctly access a table of results? (I'm not mentioning the 
issue with the strange result itself.)

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on 

[ansible-project] Re: How to save stdout_lines to a file without losing the newlines

2016-11-07 Thread jean-christophe manciot
Thanks, it's even simpler and it seems to function correctly in *all cases*.

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/76e3f82c-848c-4df0-b405-259c0959a340%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: How to save stdout_lines to a file without losing the newlines

2016-11-07 Thread jean-christophe manciot
I suppose it depends on how the data are returned by the target device, 
which is not a Linux server here, but a Cisco IOS networking device, which 
is a very specific beast.

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/9cdbfee1-8335-45fe-8c57-95656c3c30f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: How to save stdout_lines to a file without losing the newlines

2016-11-07 Thread jean-christophe manciot
@Kai Stian Olstad: stdout adds literal '\n' to each line
@Jon: |join('\n') has no effect on stdout_lines

However, if I combine both proposals into *table.stdout|join('\n')*, it 
works! Thanks guys ;)

For instance:
  Mac Address Table
---

VlanMac Address   TypePorts
---   -
   1.ab03.c600DYNAMIC Gi0/1
   1.ab41.1f00DYNAMIC Gi1/1
   1.ab55.1900DYNAMIC Gi1/0
   1.ab8d.1e00DYNAMIC Gi0/2
   1.abb9.4600DYNAMIC Gi2/2
   1.abc8.ec00DYNAMIC Gi1/2
   1.abe0.0600DYNAMIC Gi0/3
   12e05.f26f.bf83DYNAMIC Gi0/0
Total Mac Addresses for this criterion: 8

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/3ff408eb-bbc3-4baa-9f3e-ec6dc4a70cce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] How to save stdout_lines to a file without losing the newlines

2016-11-07 Thread jean-christophe manciot
Hi everyone,

Considering a variable {{ table.stdout_lines ]] containing the following 
when printing it out on the terminal:
TASK [save_table : Printing the returned table] 
*
ok: [IOSv_L2_10] => {
"table.stdout_lines": [
[
"Extended IP access list authorized-snmp-nms", 
"10 permit ip host 172.21.100.1 host 172.21.100.210 log", 
"Extended IP access list preauth_ipv4_acl (per-user)", 
"10 permit udp any any eq domain", 
"20 permit tcp any any eq domain", 
"30 permit udp any eq bootps any", 
"40 permit udp any any eq bootpc", 
"50 permit udp any eq bootpc any", 
"60 deny ip any any", 
"IPv6 access list preauth_ipv6_acl (per-user)", 
"permit udp any any eq domain sequence 10", 
"permit tcp any any eq domain sequence 20", 
"permit icmp any any nd-ns sequence 30", 
"permit icmp any any nd-na sequence 40", 
"permit icmp any any router-solicitation sequence 50", 
"permit icmp any any router-advertisement sequence 60", 
"permit icmp any any redirect sequence 70", 
"permit udp any eq 547 any eq 546 sequence 80", 
"permit udp any eq 546 any eq 547 sequence 90", 
"deny ipv6 any any sequence 100"
]
]
}

I need to save those lines into a local file, easy right?
- name: Saving "{{ item }}" into local file
  local_action: copy content="{{ table.stdout_lines }}" dest="{{ dest_file 
}}"

Unfortunately, all newlines are lost rendering the file difficult to read:
[["Extended IP access list authorized-snmp-nms", "10 permit ip host 
172.21.100.1 host 172.21.100.210 log", "Extended IP access list 
preauth_ipv4_acl (per-user)", "10 permit udp any any eq domain", "   
 20 permit tcp any any eq domain", "30 permit udp any eq bootps any", " 
   40 permit udp any any eq bootpc", "50 permit udp any eq bootpc any", 
"60 deny ip any any", "IPv6 access list preauth_ipv6_acl (per-user)", " 
   permit udp any any eq domain sequence 10", "permit tcp any any eq 
domain sequence 20", "permit icmp any any nd-ns sequence 30", "   
 permit icmp any any nd-na sequence 40", "permit icmp any any 
router-solicitation sequence 50", "permit icmp any any 
router-advertisement sequence 60", "permit icmp any any redirect 
sequence 70", "permit udp any eq 547 any eq 546 sequence 80", "   
 permit udp any eq 546 any eq 547 sequence 90", "deny ipv6 any any 
sequence 100"]]

Anyone has a brilliant simple idea to save {{ table.stdout_lines }} with 
all newlines in the file?

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/3f859fe7-55d3-47c6-86bc-38e1819fbb51%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] How to loop over hashes having an undefined number of sub-elements?

2016-10-21 Thread jean-christophe manciot
Hi everyone,

This is a tricky question which I have been unable to solve so far despîte 
multiple attempts, maybe it is not feasible with Ansible.
I pull some facts from a networking device, some of them end up in {{ 
facts.ansible_facts.ansible_net_neighbors }}:
"ansible_net_neighbors": {
"Gi0/0": [
{
"host": "IOSv_L2_10.actionmystique.net",
"port": "Gi0/1"
}
],
"Gi0/3": [
{
"host": "IOSv_Leaf_13.actionmystique.net",
"port": "Gi0/1.10"
},
{
"host": "IOSv_Leaf_13.actionmystique.net",
"port": "Gi0/1.20"
},
{
"host": "IOSv_Leaf_13.actionmystique.net",
"port": "Gi0/1"
}
],
"Gi0/4": [
{
"host": "IOSv_Leaf_14.actionmystique.net",
"port": "Gi0/1"
},
{
"host": "IOSv_Leaf_14.actionmystique.net",
"port": "Gi0/1.20"
},
{
"host": "IOSv_Leaf_14.actionmystique.net",
"port": "Gi0/1.10"
}
],
...
The number & names of ansible_net_neighbors sub-elements "Gin/p" (which 
stand for GigabitEthernet n/p) are dynamic: we have no clue in advance what 
they'll be.
Same characteristic for the number of host/port duo(s) inside each "Gin/p"

So, how can I loop over all those sub-elements? The tricky part for me is 
the fact that there is no name for the field "Gin/p".
I have unsuccessfully tried the following for instance:
- name: Updating the description of all interfaces
  ios_config:
provider: "{{ connections.ssh }}"
parents: 
  - "interface {{ item }}"
lines:
  - "description Connected to {{ item.host }} on its port {{ 
item.port }}"
  with_dict: "{{ facts.ansible_facts.ansible_net_neighbors }}"
  register: result

Any ingenious trick in mind?

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/bb8cd3ca-632c-4731-9716-df0e45c4e9ed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Re: ansible 2.0.1.0 run show version with Cisco ASA-55x0

2016-08-12 Thread jean-christophe manciot
My environment is the following:
- ubuntu 16.04 4.4.0-34
- ansible 2.1.1.0 *stable* from official PPA (not from sources)
- latest commit of extra modules pointed by "library"

On Fri, Aug 12, 2016 at 1:26 AM, Peter Sprygada <pspryg...@ansible.com>
wrote:

> Thats really strange... I just rebuilt an Ansible env using the latest
> from source and everything works fine.
>
> https://gist.github.com/privateip/88b68576d7c0dd8d8e566ce3ec75e4a8
>
> Will try to look at exactly which commit the asa_* modules showed up in
> the submodule a bit later but it definitely is working with the latest.
>
>
>
> On Thu, Aug 11, 2016 at 2:01 PM, jean-christophe manciot <
> actionmysti...@gmail.com> wrote:
>
>>
>> @Tony Reveal
>> @ Peter Sprygada
>> Tony is right, I've just tried the asa_command and experienced the same
>> issue:
>>
>> TASK [asa_pull_config : Fetching config from the remote node]
>> **
>> fatal: [172.21.100.252]: FAILED! => {"failed": true, "msg": "Could not
>> find imported module support code for asa_command.  Looked for either * or
>> asa"}
>>
>> despite having the "library" variable from /etc/ansible/ansible.cfg
>> pointing at the right folder containing the cloned
>> https://github.com/ansible/ansible-modules-extras.
>> The issue is the same whatever the asa_* call is: acl, config, template
>> or command.
>>
>> --
>> 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 post to this group, send email to ansible-project@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/ms
>> gid/ansible-project/7e7d5823-8087-4789-8771-0b85381f9c74%
>> 40googlegroups.com
>> <https://groups.google.com/d/msgid/ansible-project/7e7d5823-8087-4789-8771-0b85381f9c74%40googlegroups.com?utm_medium=email_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/ansible-project/QWMQJCUavV0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> ansible-project+unsubscr...@googlegroups.com.
> To post to this group, send email to ansible-project@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/ansible-project/CAEkdrMg02s42KAw4DZw0LdhiVMb%
> 3DjByc49g0ByX%2Bdy4YyerWwg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAEkdrMg02s42KAw4DZw0LdhiVMb%3DjByc49g0ByX%2Bdy4YyerWwg%40mail.gmail.com?utm_medium=email_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Jean-Christophe

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAKcFC3Zqvw5tGYhgeYsY0xb9kEojZy8FN0oCJ0hWENGWXeOW2A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Re: ansible 2.0.1.0 run show version with Cisco ASA-55x0

2016-08-11 Thread jean-christophe manciot

@Tony Reveal 
@ Peter Sprygada
Tony is right, I've just tried the asa_command and experienced the same 
issue:

TASK [asa_pull_config : Fetching config from the remote node] 
**
fatal: [172.21.100.252]: FAILED! => {"failed": true, "msg": "Could not find 
imported module support code for asa_command.  Looked for either * or asa"}

despite having the "library" variable from /etc/ansible/ansible.cfg 
pointing at the right folder containing the 
cloned https://github.com/ansible/ansible-modules-extras.
The issue is the same whatever the asa_* call is: acl, config, template or 
command.

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/7e7d5823-8087-4789-8771-0b85381f9c74%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: ansible 2.0.1.0 run show version with Cisco ASA-55x0

2016-08-09 Thread jean-christophe manciot
If you want the very latest builds (stable & RC release candidates), you 
can use my own PPA (only for Ubuntu 16.04) which also distributes many 
other fresh packages.



>

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/a81aeb32-29b8-4e6c-82f2-596b5fc91ae2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: ansible 2.0.1.0 run show version with Cisco ASA-55x0

2016-08-09 Thread jean-christophe manciot

>
> To install from official PPA for 
> Ubuntu: 
> http://docs.ansible.com/ansible/intro_installation.html#latest-releases-via-apt-ubuntu
>
Another way is through pip: sudo pip install ansible 

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/e78db62c-0fc2-4837-895c-e37b0400a4a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: ansible 2.0.1.0 run show version with Cisco ASA-55x0

2016-08-07 Thread jean-christophe manciot

>
> The asa_* commands are not part of latest Ansible version (2.1.1.0) yet.
>
You need to clone the repository 
https://github.com/ansible/ansible-modules-extras and modify ansible's 
configuration to point to your local folder in /etc/ansible/ansible.cfg 
with "library= ..." 

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/c80fd3d5-634e-4bfe-ad4d-17d116f95a15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] How to get the first group defined in inventory for current inventory_hostname involved in multiple groups

2016-08-07 Thread jean-christophe manciot
Ansible 2.1.1.0

Several hierarchical groups can be defined for a particular device in 
inventory.
Since hostvars[inventory_hostname].group_names lists all device groups 
listed in alphabetical order, hostvars[inventory_hostname].group_names[0] 
does not necessarily point to the right answer.

Does someone have a suggestion?

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/22ea2ead-bf1d-4eff-88dc-96e8bb30510f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Re: module (copy) is missing interpreter line

2016-07-08 Thread jean-christophe manciot
I've just tried with 2.1.1.0 RC2 and it works. As a conclusion, and since 
there is a typo in my first post, here is one *solution*:

- name: Saving config into local file
  local_action:
copy content="{{ out.stdout }}" dest=files/configs/{{ 
inventory_hostname }}/{{ inventory_hostname }}_{{ config }}_{{ 
ansible_date_time.date }}_{{ ansible_date_time.time }}_{{ 
ansible_date_time.tz }}.txt

- name: Reformatting config file
  local_action:
script {{ role_path }}/../../files/scripts/format-config.sh 
files/configs/{{ inventory_hostname }}/{{ inventory_hostname }}_{{ config 
}}_{{ ansible_date_time.date }}_{{ ansible_date_time.time }}_{{ 
ansible_date_time.tz }}.txt

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/785a1b96-45f5-4c3d-85ef-b8a242c3f5bd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Re: module (copy) is missing interpreter line

2016-07-07 Thread jean-christophe manciot
I combined my posts #2 & #3, and it worked!
That is to say, downgrading to 2.1.0 & using "local_action: copy..." 
enables me to save the content of the variable into a local file.

However, the file is barely readable because all line feeds have been 
removed and each line is enclosed by "", but at least that's a start.
That means some formatting is necessary, unless another way exists.

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/6f8418d6-e4cd-4e2d-a892-3e881c8bc074%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: module (copy) is missing interpreter line

2016-07-07 Thread jean-christophe manciot
If you are referring to "ios_template" 
, the module is 
designed to push a running-config, not to pull it from the remote device.

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/166ab9dc-bb62-4b80-9b7e-9608831b485f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: module (copy) is missing interpreter line

2016-07-07 Thread jean-christophe manciot
No: the template module only deals with files, not with a variable.

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/01db972c-9987-46f3-8409-ae6daf87cfde%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: module (copy) is missing interpreter line

2016-07-06 Thread jean-christophe manciot
Also, downgrading from 2.1.1.0 (which is a RC) to stable 2.1.0 does not 
change anything as far as this issue is concerned.

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/6c5e44c2-51fa-48de-a4c5-0e629b98b54c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: module (copy) is missing interpreter line

2016-07-06 Thread jean-christophe manciot
Exactly same issue if I slightly modify the call in the role with:

- name: Saving config into local file
  local_action:
copy content="{{ out.stdout_lines }}" dest=host_vars/"{{ 
inventory_hostname }}"/"{{ inventory_hostname }}"_"{{ config }}".txt

Also, I haven't mentioned the fact that I do not experience that issue with 
other modules.

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/407ea5da-a195-4eb9-a44c-d3e5e079549b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: module (copy) is missing interpreter line

2016-07-06 Thread jean-christophe manciot
How do we know that Ansible uses its module and not python's own "copy.py" 
available in:
/usr/lib/python3.5/copy.py
/usr/lib/python2.7/copy.py

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/72361b1c-352d-4798-b96e-983bae5c81f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] module (copy) is missing interpreter line

2016-07-06 Thread jean-christophe manciot
Hello everyone, 

It seems quite difficult to perform a very simple task: save the content of 
a variable to a file. 
This variable content is not just a string, but several lines as shown by 
the debug module:
"out.stdout_lines": [
[
"Using 2011 out of 262144 bytes, uncompressed size = 3870 
bytes", 
"!", 
"! Last configuration change at 18:34:42 UTC Thu Jun 30 2016 by 
admin", 
"!", 
"version 15.2", 
...
"hostname IOSv_L2_1", 
...
"end", 
""
]
]

Trying to use the "copy" module leads to the previously mentioned issue:
- name: Copying config into local file
  copy:
content: "{{ out.stdout_lines }}" 
dest: host_vars/{{ inventory_hostname }}/{{ inventory_hostname 
}}_{{ config }}.txt

=>
TASK [ios_pull_config : Copying config into local file] 

task path: 
/home/actionmystique/Program-Files/Ubuntu/Ansible/git-Ansible/roles/ios_pull_config/tasks/main.yml:59
<192.168.137.242> ESTABLISH LOCAL CONNECTION FOR USER: root
<192.168.137.242> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo 
$HOME/.ansible/tmp/ansible-tmp-1467812828.82-260068933148720 `" && echo 
ansible-tmp-1467812828.82-260068933148720="` echo 
$HOME/.ansible/tmp/ansible-tmp-1467812828.82-260068933148720 `" ) && sleep 
0'
<192.168.137.242> PUT /tmp/tmpotKrzg TO 
/root/.ansible/tmp/ansible-tmp-1467812828.82-260068933148720/stat
<192.168.137.242> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 
LC_MESSAGES=en_US.UTF-8 /usr/bin/python 
/root/.ansible/tmp/ansible-tmp-1467812828.82-260068933148720/stat; rm -rf 
"/root/.ansible/tmp/ansible-tmp-1467812828.82-260068933148720/" > /dev/null 
2>&1 && sleep 0'
<192.168.137.242> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo 
$HOME/.ansible/tmp/ansible-tmp-1467812828.91-263479362702141 `" && echo 
ansible-tmp-1467812828.91-263479362702141="` echo 
$HOME/.ansible/tmp/ansible-tmp-1467812828.91-263479362702141 `" ) && sleep 
0'
<192.168.137.242> PUT /tmp/tmp5gr0LH TO 
/root/.ansible/tmp/ansible-tmp-1467812828.91-263479362702141/source
fatal: [192.168.137.242]: FAILED! => {"failed": true, "msg": "*module 
(copy) is missing interpreter line*"}

In /etc/ansible/ansible.cfg with ansible 2.1.1.0, the default library path 
is defined:
library= /usr/lib/python2.7/dist-packages/ansible

Also, there are 2 default "copy.py" in the downstream tree:
- ./modules/core/files/copy.py
- ./plugins/action/copy.py
Isn't this confusing for Ansible?

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/7ff7b3cf-63f6-4693-967b-e293a2c99872%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Checking the presence of some CA certificate on a remote Linux host

2016-05-20 Thread jean-christophe manciot
Hello guys,

I need to download & install the CA certificate *only if it is not present 
on the remote host*.
Does anyone know how to achieve the CA certificate presence check with 
Ansible?
That will be used to populate a variable used for something along those 
lines:

  - name: Downloading the CA certificate from the official site
get_url: url={{ ca_url }} dest={{ dest_cert_folder }} use_proxy=no
when: ca_cert == 'absent'

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/81b4e649-d138-4434-a003-d256a0575301%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Checking the presence of some CA certificate on a remote Linux host

2016-05-20 Thread jean-christophe manciot
Hello guys,

I need to download & install the CA certificate *only if it is not present 
on the remote host*.
Does anyone know how to achieve the CA certificate presence check with 
Ansible?
That will be used to populate a variable used for something along those 
lines:

  - name: Downloading the CA certificate from the official site
get_url: url={{ ca_url }} dest={{ dest_cert_folder }} use_proxy=no
when: ca_cert='absent'



-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/ea07b195-93a9-4a59-a595-68f79811bc7f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Conflicting Modules

2016-03-04 Thread jean-christophe manciot
Actually, there is no Ansible core modules in /usr/share/ansible: they are 
located in "/usr/lib/pymodules/python2.7/ansible/modules".
Since there is only one global "library" variable in 
/etc/ansible/ansible.cfg, and considering the fact that it is set to 
/usr/share/ansible by default, without any detailed documentation on that 
matter, my *guess* is that:

   - ansible core modules have the highest priority
   - it is not possible to establish some priority amongst different 
   libraries with the ansible config file
   - we should avoid same module names among different libraries since we 
   don't know how Ansible would behave

... unless someone has an official answer.

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/b02b7272-3eb1-4e24-b54c-d8785d6a3b22%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Conflicting Modules

2016-03-03 Thread jean-christophe manciot
Thanks for your answer.
I have a /usr/share/ansible/cisco_nxos/library folder which contains many 
modules. Calling them works fine.
However, some new Cisco network modules are currently being incorporated as 
core modules into ansible 
(http://docs.ansible.com/ansible/list_of_network_modules.html#nxos), so 
some overlap is about to happen.

There is a "library = /usr/share/ansible" within /etc/ansible/ansible.cfg. 
Are you suggesting that we can have multiple "library = ..." settings, 
which would allow me to move the external library into another user folder 
and set some priority between them that way?

On Saturday, February 20, 2016 at 8:53:23 PM UTC+1, Brian Coca wrote:
>
> First, /usr/share/ansible is normally where core modules are stored, so 
> not sure what you are asking, but let me explain how loading works.
>
> If you put a module in a library dir adjacent to a play or in a role with 
> the same name as a 'core' module, it will override it, if that is what you 
> are asking.
>
> also the module path (settable in ansible.cfg or by env var) can be used 
> to establish priority, first found will override others, so if you set a 
> custom path before the ones shipped with ansible, they will be loaded first 
> and override them.
>
>
>
> --
> Brian Coca
>

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/2906cfa1-1be5-4b88-84a1-efd254b2d949%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Conflicting Modules

2016-02-20 Thread jean-christophe manciot
Hello guys,

Let's suppose that 2 different modules with the same name exist on Ansible 
2.0.0.2:

   - one as a core module
   - one as an imported module into /usr/share/ansible

Which one gets precedence by default when a call is made and how can we 
specifically choose one over the other one?

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/e83e0c49-52f8-47e9-b21f-cd1b3bb6bafb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.