[ansible-project] Sorting dicts in lists - works different between my machine and others

2019-05-14 Thread Jon Spriggs
Hi,

I have a variable which is a list (sourced from registering the response
from a 3rd party module). In that list, each item is a dict, as follows:

[
  {'policyid': '1009', 'other': 'content'},
  {'policyid': '2019', 'other': 'content'},
  {'policyid': '1024', 'other': 'content'},
  {'policyid': '1000', 'other': 'content'},
  {'policyid': '8732', 'other': 'content'},
  {'policyid': '1012', 'other': 'content'}
]

I have some code that works locally on my machine:

- name: sort policy
  set_fact:
desired_policy: |
  [
{% for policy in fwpolicy.meta.results | sort(attribute='policyid')
%}
  {
'rule': '{{ policy.policyid }}',
'after': '{{ (loop.previtem | default({}) )['policyid'] |
default(0) }}',
'before': '{{ (loop.nextitem | default({}) )['policyid'] |
default(0) }}'
  },
{% endfor %}
  ]

When I run this on my machine, I get the following list:

[
  {'policyid': '1000', 'after': 0, 'before': 1009},
  {'policyid': '1009' , 'after': 1000, 'before': 1012},
  {'policyid': '1012' , 'after': 1009, 'before': 1024},
  {'policyid': '1024' , 'after': 1012, 'before': 2019},
  {'policyid': '2019' , 'after': 1024, 'before': 8732},
  {'policyid': '8732' , 'after': 2019, 'before': 0}
]

On my colleague's machine (and on AWX for that matter), I get:

[
  {'policyid': '1000', 'after': 0, 'before': 0},
  {'policyid': '1009' , 'after': 0, 'before': 0},
  {'policyid': '1012' , 'after': 0, 'before': 0},
  {'policyid': '1024' , 'after': 0, 'before': 0},
  {'policyid': '2019' , 'after': 0, 'before': 0},
  {'policyid': '8732' , 'after': 0, 'before': 0}
]

Am I doing something wrong, and should I be doing something differently?
--
Jon "The Nice Guy" Spriggs
@jontheniceguy everywhere...
https://jon.sprig.gs

-- 
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/CAOrCWesZBGwYfnXq17YRmZ9gAiUrGxW0omHxSN%2BhrhEc5DYP_g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Building variables like in Jekyll?

2019-03-21 Thread Jon Spriggs
Hi,

I've a complex data structure to build, which, while I can do it in
group/host_vars/ I'm worrying about the people who follow me on, and how
they'll cope with it.

I've some experience of Jekyll, which has a good data structure with yml
files in a directory, and I'd like, if possible to use that with Ansible.
In that system, there is a "data" directory, and each directory under that
path is loaded as a dict with the same name as the directory, and then each
file under that is loaded as either a variable or a dict again (depending
on the content).

So, for example, if I had this structure:

data / dir1 / file1.yml
data / dir2 / dir3 / file2.yml
data / dir2 / dir3 / file3.json
data / file4.yml

I would have these variables loaded:
{
  "data": {
"dir1": {
  "file1": {{ content_of_file1.yml }}
},
"dir2": {
  "dir3": {
"file2": {{ content_of_file2.yml }},
'file3': {{ content_of_file3.json }}
  }
},
'file4': {{ content_of_file4.yml" }}
  }
}

Short of writing a lookup or a playbook to load the data like this, are
there any modules or plugins I can use already, or import from another
project?

Thanks!
--
Jon "The Nice Guy" Spriggs
@jontheniceguy everywhere...
https://jon.sprig.gs

-- 
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/CAOrCWesYmfzktgkrwmS3gbjk5LLxnxiU0_Dj5E%2BBeCo4yTigsw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Re: setting variable in a playbook from a jinjia2 snippet

2019-02-26 Thread Jon Spriggs
Looks right to me :)

Like you, however, I don't have a way to test it right now!

---
Jon "The Nice Guy" Spriggs
@jontheniceguy everywhere...
https://jon.sprig.gs


On Mon, 25 Feb 2019, 21:05 fusillator,  wrote:

> Thanks for the tip Jon.
> So if the variable was necessary in subsequent tasks I could use set_fact
> module rather than defining a variable in the scope of the task or play
>
> do you mean something like...
>
>  - name: "list files in an archive"
> unarchive:
>   src: /root/mybundle.zip
>   dest: /opt
>   list_files: yes
>   remote_src: yes
> register: archive_content
> loop:
>   - /root/mybundle.zip
>   - /root/mybudle2.zip
>
>   - name: "adapts structure"
> set_fact:
>   path: "{% if output is undefined %}\
>{% set output = [] %}\
>{% endif %}\
>  {% for file in item.files %}\
>{% if file | regex_search('.rpm$') %}
>  {{ output.append(item.dest  ~ '/' ~ file) }}\
>{% endif %}
>  {% endfor %}\
>  {% output %}"
> loop: "{{ archive_content.results|list }}"
>   - debug:
>   var: path
>
> Unfortunately in this moment I can't test the playbook..
>
> Regards
>
> Luca
>
>
>
>
>
> Il giorno lunedì 25 febbraio 2019 13:02:17 UTC+1, Jon Spriggs ha scritto:
>>
>> Also, look at the set_fact module, I use this extensively!
>> --
>> Jon "The Nice Guy" Spriggs
>> @jontheniceguy everywhere...
>> https://jon.sprig.gs
>>
>>
>> On Fri, 22 Feb 2019 at 11:45, fusillator  wrote:
>>
>>> Sorry for the stupid question
>>> solved with
>>>
>>>   - name: "debug archive_content"
>>> debug:
>>>   var: path
>>> vars:
>>>   path: "{% set output = [] %}\
>>>  {% for file in item.files %}\
>>>{% if file | regex_search('.rpm$') %}
>>>  {{ output.append(item.dest  ~ '/' ~ file) }}\
>>>{% endif %}
>>>  {% endfor %}\
>>>  {{ output }}"
>>> loop: "{{ archive_content.results|list }}"
>>>
>>> Luca
>>>
>>> --
>>> 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 post to this group, send email to ansible...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/ansible-project/ad5b6d83-5a4e-46f8-a74c-cc9ef1cbae09%40googlegroups.com
>>> <https://groups.google.com/d/msgid/ansible-project/ad5b6d83-5a4e-46f8-a74c-cc9ef1cbae09%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> 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/3b57360c-cae9-4c35-8cf1-84e0b3018ab1%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/3b57360c-cae9-4c35-8cf1-84e0b3018ab1%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAOrCWetdz%3Dt5WQJ8UzTKQYJhyDqKjbtoE8av%3D2DQEKQpvErXBw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Re: setting variable in a playbook from a jinjia2 snippet

2019-02-25 Thread Jon Spriggs
Also, look at the set_fact module, I use this extensively!
--
Jon "The Nice Guy" Spriggs
@jontheniceguy everywhere...
https://jon.sprig.gs


On Fri, 22 Feb 2019 at 11:45, fusillator  wrote:

> Sorry for the stupid question
> solved with
>
>   - name: "debug archive_content"
> debug:
>   var: path
> vars:
>   path: "{% set output = [] %}\
>  {% for file in item.files %}\
>{% if file | regex_search('.rpm$') %}
>  {{ output.append(item.dest  ~ '/' ~ file) }}\
>{% endif %}
>  {% endfor %}\
>  {{ output }}"
> loop: "{{ archive_content.results|list }}"
>
> Luca
>
> --
> 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/ad5b6d83-5a4e-46f8-a74c-cc9ef1cbae09%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAOrCWeuHsWGDDiYEGsMtCFnNfpB2u-gFcOLq7Zy0k-9buC%3Dx4Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Dealing with non-fixed length lists of variables

2019-02-25 Thread Jon Spriggs
Why not structure your data like this:

---
- hosts: all
  vars:
   provider:
 password: Password
 server: IP
 user: username
 validate_certs: no
 server_port: port
  mapping: # This is new and might be stored elsewhere :)
server1.example.com: 192.0.2.1
server2.example.com: 192.0.2.2
server3.example.com: 192.0.2.3


  tasks:
   - name: Create a pool
 bigip_pool:
   provider: "{{ provider }}"
   lb_method: ratio-member
   name: "{{ service }}"
   slow_ramp_time: 120
 delegate_to: localhost

- name: Add members to pool
 bigip_pool_member:
   provider: "{{ provider }}"
   description: "webserver {{ item.key }}"
   host: "{{ item.value }}"
   name: "{{ item.key }}"
   pool: "{{ service }}"
   port: "{{ lbport }}"
 loop:"{{ mapping|flatten() }}"

Regards,
--
Jon "The Nice Guy" Spriggs
@jontheniceguy everywhere...
https://jon.sprig.gs


On Sat, 23 Feb 2019 at 14:29, Spencer Webb  wrote:

> Hi all,
>
> I've got a playbook that uses variables to create a load balanced pool and
> the required members on our test F5 BigIP.
>
> I've got the basics working without issue, however, the number of members
> of a pool for any given run is not a fixed number and could range from 1 up
> to say 10.
>
> I'm running Ansible Tower and invoking the template via tower-cli. I'm
> using extra-vars to provide realip and realserver and looped through. The
> default values are set to null so I was hoping to loop through the list
> until a null value for item.host is observed. I'm not sure if this is the
> best way to deal with this, it's the first time that I've tried to deal
> with variable lists that are not fixed in number.
>
> Here's my current code, any help would be greatly appreciated.
>
> Cheers
> Spence
>
> ---
>
> - name: Create a VIP, pool and pool members
>  hosts: all
>  connection: local
>
>   vars:
>provider:
>  password: Password
>  server: IP
>  user: username
>  validate_certs: no
>  server_port: port
>
>   tasks:
>- name: Create a pool
>  bigip_pool:
>provider: "{{ provider }}"
>lb_method: ratio-member
>name: "{{ service }}"
>slow_ramp_time: 120
>  delegate_to: localhost
>
> - name: Add members to pool
>  bigip_pool_member:
>provider: "{{ provider }}"
>description: "webserver {{ item.name }}"
>host: "{{ item.host }}"
>name: "{{ item.name }}"
>pool: "{{ service }}"
>port: "{{ lbport }}"
>  loop:
>- { host: "{{ realip1 }}",  name: "{{ realserver1 }}" }
>- { host: "{{ realip2 }}",  name: "{{ realserver2 }}" }
>- { host: "{{ realip3 }}",  name: "{{ realserver3 }}" }
>- { host: "{{ realip4 }}",  name: "{{ realserver4 }}" }
>- { host: "{{ realip5 }}",  name: "{{ realserver5 }}" }
>- { host: "{{ realip6 }}",  name: "{{ realserver6 }}" }
>- { host: "{{ realip7 }}",  name: "{{ realserver7 }}" }
>- { host: "{{ realip8 }}",  name: "{{ realserver8 }}" }
>- when: item.host != ""
>- delegate_to: localhost
>
>
> --
> 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/cf8e3969-eeaf-422b-9fbd-4ca36d19d660%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAOrCWesJFMjWxPga0XSp9zDgEHOyqga8vdqXt_EmZ2cUTOOD4w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Hosts with multiple users.

2019-02-12 Thread Jon Spriggs
Hi Eduardo,

Ansible tends to be relatively prescriptive - you need to specify the
username, or you'd need to specify multiple "host" entries with each of the
usernames.

You could perform a "simple" action (like "ping" or "setup") and let it
stop processing hosts that it doesn't get a response from? (Although, to be
fair, I don't entirely know how you'd do that).

Perhaps you'd be better off explaining what you're trying to do?
--
Jon "The Nice Guy" Spriggs
@jontheniceguy everywhere...
https://jon.sprig.gs


On Tue, 12 Feb 2019 at 19:14, Eduardo Nunes Pereira 
wrote:

> Someone could help me with this case where I have multiple hosts and for
> these hosts I do not know which exact user but I have a list with some
> possible possible users. inside the hosts file I create the group: vars but
> I can only put a single user.
>
> --
> 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/cdca0560-ebb0-4692-b6de-9c87fb9d3413%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAOrCWev%2B%3DkvEN%2BJTDdj_zwRUgb_rQZz0NGAwtWD2jChmPG6d0w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Re: PLAY RECAP -- Status

2019-02-11 Thread Jon Spriggs
If you spot, in the logs, it shows that it's breaking on the "ethernet0/1",
and, if I read it right that's in the "/" in that... So it's probably worth
wrapping any instance of "ethernet0/1" in quotes, like I did.

Give that a whirl, and try again :)

---
Jon "The Nice Guy" Spriggs
@jontheniceguy everywhere...
https://jon.sprig.gs

On Mon, 11 Feb 2019, 21:00 'Tcpip' via Ansible Project <
ansible-project@googlegroups.com wrote:

> This is the playbook Im using for test.
>
> ---
>
>
>
> - hosts: all
>   gather_facts: false
>   connection: local
>   vars:
>cli:
> #host: "{{ inventory_hostname}}"
> username: cisco
> password: cisco
>   tasks:
>
> - name: Shutdown Interface
>   ios_interface:
> name: Ethernet0/1
> enabled: False
> state: down
> provider: "{{ cli }}"
>   register: first
>
> - pause:
> seconds=3
>
> - name: Startup Interface
>   ios_interface:
> name: Ethernet0/1
> enabled: True
> state: up
> provider: "{{ cli }}"
>
> - name: my task
>   shell: ./print.py
>   when: failed  == '1'
>   register: task
>   tags: task
>
> - debug: var=task.stdout_lines[0]
>   tags: show_task
>
> the ./print.py is just showing the "hello" but is not working
>
> The full traceback is:
> Traceback (most recent call last):
>   File "/tmp/ansible_62pEWK/ansible_module_ios_interface.py", line 490, in
> 
> main()
>   File "/tmp/ansible_62pEWK/ansible_module_ios_interface.py", line 478, in
> main
> load_config(module, commands)
>   File
> "/tmp/ansible_62pEWK/ansible_modlib.zip/ansible/module_utils/network/ios/ios.py",
> line 168, in load_config
>   File
> "/tmp/ansible_62pEWK/ansible_modlib.zip/ansible/module_utils/connection.py",
> line 174, in __rpc__
> ansible.module_utils.connection.ConnectionError: interface Ethernet0/1
>^
> % Invalid input detected at '^' marker.
>
> CBR1(config)#
>
> fatal: [cbr1]: FAILED! => {
> "changed": false,
> "module_stderr": "Traceback (most recent call last):\n  File
> \"/tmp/ansible_62pEWK/ansible_module_ios_interface.py\", line 490, in
> \nmain()\n  File
> \"/tmp/ansible_62pEWK/ansible_module_ios_interface.py\", line 478, in
> main\nload_config(module, commands)\n  File
> \"/tmp/ansible_62pEWK/ansible_modlib.zip/ansible/module_utils/network/ios/ios.py\",
> line 168, in load_config\n  File
> \"/tmp/ansible_62pEWK/ansible_modlib.zip/ansible/module_utils/connection.py\",
> line 174, in __rpc__\nansible.module_utils.connection.ConnectionError:
> interface Ethernet0/1\r\n   ^\r\n% Invalid
> input detected at '^' marker.\r\n\r\nCBR1(config)#\n",
> "module_stdout": "",
> "msg": "MODULE FAILURE",
> "rc":
>
> Thanks.
>
> El lunes, 11 de febrero de 2019, 13:10:19 (UTC-3), Tcpip escribió:
>>
>> Hi all,
>>
>> I need to execute a script every time PLAY RECAP  failed=1, how can I
>> configure my Ansible Playbook to do this?
>>
>> 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/4aab0ed9-4cf5-420b-9b94-5bfc9d0f965d%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAOrCWeuC%3D1YVQQE3dYU_5K3kWi0_nrTDzQ3W6ML-syNbzZrwkw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] How can I pass extra variables to a shell script in my playbooks

2019-02-11 Thread Jon Spriggs
Hi Conor, sounds like you're probably doing this (untested pseudocode):

- shell: "/path/to/script {{ var }}"
  vars:
var:
  some_key: some_data

Instead, you need to do something like this (untested pseudocode):

- shell: "/path/to/script {% for item in var|dict2items %}{{ item.key }}={{
item.value }}{% endfor %}"
  vars:
var:
  some_key: some_data

If not, if you can post some example script, that might help...

All the best,
--
Jon "The Nice Guy" Spriggs
@jontheniceguy everywhere...
https://jon.sprig.gs


On Mon, 11 Feb 2019 at 16:24, Conor Proud  wrote:

> Hi,
> This looks like the right place for posting questions- but please point me
> in the right direction if not.
> I'm using the "extra variables" box on the awx web front-end to provide
> key-value pairs to a shell script in my playbook. The problem I'm having is
> how I can use those variables in my shell script, as they all appear as
> {u'key': u'value'} when I print them from within my shell script. This
> means that they don't conform to the standard JSON format (which uses
> double-quotes) and also as these are unicode strings I'm having other
> issues trying to pull out the information I want. What I would like is to
> easily put these key-value pairs into an associative array so I can access
> the values when they're required. Is there a good way to do this? At the
> moment I'm having to use sed to substitute out the bits I don't want (the
> single quotes have to be converted to double quotes and I have to remove
> the "u" character and then pass this to a package called jq to read the
> string as a JSON object). This feels like the wrong approach, so I'm look
> for some advice or examples of whether this can be done more elegantly?
>
> --
> 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/d04dc999-d042-46b2-af50-c543443d7190%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAOrCWevLxGJ3-fXycGRnBz%3DGeAwnezmTgm6CChf5Tzjd9hnPxQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.