Re: [ansible-project] Unusal behaviour with set_fact and add_host

2024-04-25 Thread 'deewon' via Ansible Project
ok: [node-1] => {
"is_leader": true
}
ok: [node-2] => {
"is_leader": false
}

TASK [Debug is_leader value] 
**
ok: [node-1] => {}

MSG:

is_leader value for node-1 is True
ok: [node-2] => {}

MSG:

is_leader value for node-2 is False

TASK [Re-confirm is_leader before adding to group] 

skipping: [node-1] => {}
ok: [node-2] => {}

MSG:

Preparing to add node-2 to stby group based on is_leader = False

TASK [Add standby proxysql node] 
***
skipping: [node-1] => {
"changed": false,
"skip_reason": "Conditional result was False"
}

TASK [Re-confirm is_leader before adding to group] 

ok: [node-1] => {}

MSG:

Preparing to add node-1 to prim group based on is_leader = True
skipping: [node-2] => {}

TASK [Add primary proxysql node] 
***
changed: [node-1] => {
"add_host": {
"groups": [
"nodes_prim"
],
"host_name": "node-1",
"host_vars": {}
},
"changed": true
}

PLAY RECAP 
*
localhost  : ok=1changed=0unreachable=0failed=0 
   skipped=0rescued=0ignored=0
node-1 : ok=8changed=1unreachable=0failed=0skipped=2   
 rescued=0ignored=0
node-2 : ok=7changed=0unreachable=0failed=0skipped=1   
 rescued=0ignored=0


A lot of those debug steps were added in by me to figure out why the 
"is_leader" condition was being skipped when it was false.  
Also the plan was to add all the hosts to a single group but, in an order 
that serial tasks could be executed on in a subsequent playbook.

Thanks

On Thursday, April 25, 2024 at 1:38:38 PM UTC+1 deewon wrote:

> Testing if my email is still deleted without any code
>
> On Tuesday, April 23, 2024 at 11:28:59 PM UTC+1 Todd Lewis wrote:
>
>> All that said, I don't see why this one task wouldn't work for your 
>> purposes:
>>
>> - name: Add hosts to groups
>>   ansible.builtin.add_host:
>> name: "{{ inventory_hostname }}"
>> groups:
>>   - nodes
>>   - "{{ 'nodes_prim' if is_active | d(false) else 'nodes_stby' }}"
>>
>>
>> On 4/23/24 5:40 PM, Todd Lewis wrote:
>>
>> Hard to say. The job log you show doesn't match the tasks in the playbook 
>> you posted. They seem to be the same down to the "Debug is_leader value" 
>> task, but after that the task names in the log don't match those in the 
>> playbook.
>>
>> In the playbook you posted, task "Add standby host" an task "Add primary 
>> host" both add the hosts to the same group. That may be a source of your 
>> problem. But as we don't see the output for those tasks …, hmm.
>> —
>> Todd
>>
>> On 4/23/24 4:03 PM, 'deewon' via Ansible Project wrote:
>>
>> ansible version: 2:11.2 
>> jinja version: 3.0.3
>> python version: 3.6.8
>>
>> Hello all
>>
>> I have a peculiar problem with a relatively simple playbook I have 
>> written. It basically checks 2 servers to determine which one has more than 
>> 1 IP and uses that to set a simp;e fact called "is_leader" for each node. 
>>
>> This fact is then to be used  to build an in-memory inventory using 
>> module add_host to iterate through the servers based on the value of that 
>> fact. See below:
>>
>> - name: Get Server Recycling Order
>>   hosts: "{{ my_servers | default ('localhost') }}"
>>   become: yes
>>   become_method: sudo
>>   become_user: root
>>
>>   tasks:
>>
>> - name: Set Patching Sequence
>>   tags: always
>>   block:
>>
>>  - name: Get IP addresses of eth0 using nmcli command
>>command: nmcli -g ip4.address conn show &

Re: [ansible-project] Unusal behaviour with set_fact and add_host

2024-04-25 Thread 'deewon' via Ansible Project
"nmcli",
"-g",
"ip4.address",
"conn",
"show",
"System eth0"
],
"delta": "0:00:00.026130",
"end": "2024-04-23 15:46:30.828281",
"failed": false,
"msg": "",
"rc": 0,
"start": "2024-04-23 15:46:30.802151",
"stderr": "",
"stderr_lines": [],
"stdout": "192.168.10.11/24",
"stdout_lines": [
"192.168.10.11/24"
]
}
}

TASK [Set is_leader variable based on number of IPs] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.079)   0:00:16.815 
*
ok: [node-1] => {
"ansible_facts": {
"is_leader": true
},
"changed": false
}
ok: [node-2] => {
"ansible_facts": {
"is_leader": false
},
"changed": false
}

TASK [Display if server is ACTIVE (1) or PASSIVE (0)] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.083)   0:00:16.898 
*
ok: [node-1] => {
"is_leader": true
}
ok: [node-2] => {
"is_leader": false
}

TASK [Debug is_leader value] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.072)   0:00:16.971 
*
ok: [node-1] => {}

MSG:

is_leader value for node-1 is True
ok: [node-2] => {}

MSG:

is_leader value for node-2 is False

TASK [Re-confirm is_leader before adding to group] 

Tuesday 23 April 2024  19:46:31 + (0:00:00.074)   0:00:17.045 
*
skipping: [node-1] => {}
ok: [node-2] => {}

MSG:

Preparing to add node-2 to stby group based on is_leader = False

TASK [Add standby proxysql node] 
***
Tuesday 23 April 2024  19:46:31 + (0:00:00.088)   0:00:17.134 
*
skipping: [node-1] => {
"changed": false,
"skip_reason": "Conditional result was False"
}

TASK [Re-confirm is_leader before adding to group] 

Tuesday 23 April 2024  19:46:31 + (0:00:00.036)   0:00:17.170 
*
ok: [node-1] => {}

MSG:

Preparing to add node-1 to prim group based on is_leader = True
skipping: [node-2] => {}

TASK [Add primary proxysql node] 
***
Tuesday 23 April 2024  19:46:31 + (0:00:00.059)   0:00:17.230 
*
changed: [node-1] => {
"add_host": {
"groups": [
"nodes_prim"
],
"host_name": "node-1",
"host_vars": {}
},
"changed": true
}

PLAY RECAP 
*
localhost  : ok=1changed=0unreachable=0failed=0 
   skipped=0rescued=0ignored=0
node-1 : ok=8changed=1unreachable=0failed=0skipped=2   
 rescued=0ignored=0
node-2 : ok=7changed=0unreachable=0failed=0skipped=1   
 rescued=0ignored=0


A lot of those debug steps were added in by me to figure out why the 
"is_leader" condition was being skipped when it was false.  Also the plan 
was also to add all the hosts to a single group but in an order that serial 
tasks could be executed in a subsequent playbook. Hopefully this response 
gets through

Thanks

On Thursday, April 25, 2024 at 1:38:38 PM UTC+1 deewon wrote:

> Testing if my email is still deleted without any code
>
> On Tuesday, April 23, 2024 at 11:28:59 PM UTC+1 Todd Lewis wrote:
>
>> All that said, I don't see why this one task wouldn't work for your 
>> purposes:
>>
>> - name: Add hosts to groups
>>   ansible.builtin.add_host:
>> name: "{{ inventory_hostname }}"
>> groups:
>>   - nodes
>>

Re: [ansible-project] Unusal behaviour with set_fact and add_host

2024-04-25 Thread 'deewon' via Ansible Project
"nmcli",
"-g",
"ip4.address",
"conn",
"show",
"System eth0"
],
"delta": "0:00:00.026130",
"end": "2024-04-23 15:46:30.828281",
"failed": false,
"msg": "",
"rc": 0,
"start": "2024-04-23 15:46:30.802151",
"stderr": "",
"stderr_lines": [],
"stdout": "192.168.10.11/24",
"stdout_lines": [
"192.168.10.11/24"
]
}
}

TASK [Set is_leader variable based on number of IPs] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.079)   0:00:16.815 
*
ok: [node-1] => {
"ansible_facts": {
"is_leader": true
},
"changed": false
}
ok: [node-2] => {
"ansible_facts": {
"is_leader": false
},
"changed": false
}

TASK [Display if server is ACTIVE (1) or PASSIVE (0)] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.083)   0:00:16.898 
*
ok: [node-1] => {
"is_leader": true
}
ok: [node-2] => {
"is_leader": false
}

TASK [Debug is_leader value] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.072)   0:00:16.971 
*
ok: [node-1] => {}

MSG:

is_leader value for node-1 is True
ok: [node-2] => {}

MSG:

is_leader value for node-2 is False

TASK [Re-confirm is_leader before adding to group] 

Tuesday 23 April 2024  19:46:31 + (0:00:00.074)   0:00:17.045 
*
skipping: [node-1] => {}
ok: [node-2] => {}

MSG:

Preparing to add node-2 to stby group based on is_leader = False

TASK [Add standby proxysql node] 
***
Tuesday 23 April 2024  19:46:31 + (0:00:00.088)   0:00:17.134 
*
skipping: [node-1] => {
"changed": false,
"skip_reason": "Conditional result was False"
}

TASK [Re-confirm is_leader before adding to group] 

Tuesday 23 April 2024  19:46:31 + (0:00:00.036)   0:00:17.170 
*
ok: [node-1] => {}

MSG:

Preparing to add node-1 to prim group based on is_leader = True
skipping: [node-2] => {}

TASK [Add primary proxysql node] 
***
Tuesday 23 April 2024  19:46:31 + (0:00:00.059)   0:00:17.230 
*
changed: [node-1] => {
"add_host": {
"groups": [
"nodes_prim"
],
"host_name": "node-1",
"host_vars": {}
},
"changed": true
}

PLAY RECAP 
*
localhost  : ok=1changed=0unreachable=0failed=0 
   skipped=0rescued=0ignored=0
node-1 : ok=8changed=1unreachable=0failed=0skipped=2   
 rescued=0ignored=0
node-2 : ok=7changed=0unreachable=0failed=0skipped=1   
 rescued=0ignored=0


A lot of those debug steps were adding in by me to figure out why the 
"is_leader" condition was being skipped when it was false.  Also the plan 
was also to add all the hosts t a single group but in an order that serial 
tasks can be executed on in a subsequent playbook.

Thanks

On Tuesday, April 23, 2024 at 11:28:59 PM UTC+1 Todd Lewis wrote:

> All that said, I don't see why this one task wouldn't work for your 
> purposes:
>
> - name: Add hosts to groups
>   ansible.builtin.add_host:
> name: "{{ inventory_hostname }}"
> groups:
>   - nodes
>   - "{{ 'nodes_prim' if is_active | d(false) else 'nodes_stby' }}"
>
>
> On 4/23/24 5:40 PM, Todd Lewis wrote:
>
> Hard to say. The job log you show doesn't match the t

Re: [ansible-project] Unusal behaviour with set_fact and add_host

2024-04-25 Thread 'deewon' via Ansible Project
Testing if my email is still deleted without any code

On Tuesday, April 23, 2024 at 11:28:59 PM UTC+1 Todd Lewis wrote:

> All that said, I don't see why this one task wouldn't work for your 
> purposes:
>
> - name: Add hosts to groups
>   ansible.builtin.add_host:
> name: "{{ inventory_hostname }}"
> groups:
>   - nodes
>   - "{{ 'nodes_prim' if is_active | d(false) else 'nodes_stby' }}"
>
>
> On 4/23/24 5:40 PM, Todd Lewis wrote:
>
> Hard to say. The job log you show doesn't match the tasks in the playbook 
> you posted. They seem to be the same down to the "Debug is_leader value" 
> task, but after that the task names in the log don't match those in the 
> playbook.
>
> In the playbook you posted, task "Add standby host" an task "Add primary 
> host" both add the hosts to the same group. That may be a source of your 
> problem. But as we don't see the output for those tasks …, hmm.
> —
> Todd
>
> On 4/23/24 4:03 PM, 'deewon' via Ansible Project wrote:
>
> ansible version: 2:11.2 
> jinja version: 3.0.3
> python version: 3.6.8
>
> Hello all
>
> I have a peculiar problem with a relatively simple playbook I have 
> written. It basically checks 2 servers to determine which one has more than 
> 1 IP and uses that to set a simp;e fact called "is_leader" for each node. 
>
> This fact is then to be used  to build an in-memory inventory using module 
> add_host to iterate through the servers based on the value of that fact. 
> See below:
>
> - name: Get Server Recycling Order
>   hosts: "{{ my_servers | default ('localhost') }}"
>   become: yes
>   become_method: sudo
>   become_user: root
>
>   tasks:
>
> - name: Set Patching Sequence
>   tags: always
>   block:
>
>  - name: Get IP addresses of eth0 using nmcli command
>command: nmcli -g ip4.address conn show "System eth0"
>register: nmcli_output
>changed_when: false
>
>
>  - name: Get nmcli output
>debug:
>  var: nmcli_output
>
>
>  - name: Set is_leader variable based on number of IPs
>set_fact:
>  is_leader: "{{ (nmcli_output.stdout.split('|') | length) > 1 
> }}"
>
>
>  - name: Display if server is ACTIVE (1) or PASSIVE (0)
>debug:
>  var: is_leader
>
>
>  - name: Debug is_leader value
>debug:
>   msg: "is_leader value for {{ inventory_hostname }} is {{ 
> is_leader }}"
>
>
>  - name: Add standby host
>add_host:
>   name: "{{ inventory_hostname }}"
>   groups: nodes
>when: not is_leader 
>
>  - name: Add primary host
>add_host:
>   name: "{{ inventory_hostname }}"
>   groups: nodes
>when: is_leader
>
> #- name: Execute tasks on servers in a specified order
> #  hosts: nodes
> #  gather_facts: no
> #  serial: 1
> #  tasks:
> #- name: Run a command in a specified order
> #  command: echo "Running on {{ inventory_hostname }}"
> #  tags: always
>
> The problem is that when this runs, the  "is_leader" fact is  ignored for 
> when the condition is false and the standby node is  not added to the 
> inventory. The relevant extract from the execution is below:
>
>  TASK [Set is_leader variable based on number of IPs] 
> **
> Tuesday 23 April 2024  19:46:31 + (0:00:00.079)   0:00:16.815 
> *
> ok: [node-1] => {
> "ansible_facts": {
> "is_leader": true
> },
> "changed": false
> }
> ok: [node-2] => {
> "ansible_facts": {
> "is_leader": false
> },
> "changed": false
> }
>
> TASK [Display if server is ACTIVE (1) or PASSIVE (0)] 
> **
> Tuesday 23 April 2024  19:46:31 + (0:00:00.083)   0:00:16.898 
> *
> ok: [node-1] => {
> "is_leader": true
> }
> ok: [node-2] => {
> "is_leader": false
> }
>
> TASK [Debug is_leader value] 
> **
> Tuesday 

Re: [ansible-project] Unusal behaviour with set_fact and add_host

2024-04-25 Thread 'deewon' via Ansible Project
Hi Todd. I believe I responded to you but it seems my response was deleted. 
I can post the full playbook and output if required. A lot of those debug 
steps were just to understand why the condition was being ignored when 
"is_leader" is set to false

I'll incorporate the suggestion above to reduce the number of tasks 
required to do this. The goal was simply to add the tasks into an in-memory 
group and carry out some serial action in the order in which they were 
added. 

Thanks

On Tuesday, April 23, 2024 at 11:28:59 PM UTC+1 Todd Lewis wrote:

All that said, I don't see why this one task wouldn't work for your 
purposes:
- name: Add hosts to groups ansible.builtin.add_host: name: "{{ 
inventory_hostname }}" groups: - nodes - "{{ 'nodes_prim' if is_active | 
d(false) else 'nodes_stby' }}" 

On 4/23/24 5:40 PM, Todd Lewis wrote:

Hard to say. The job log you show doesn't match the tasks in the playbook 
you posted. They seem to be the same down to the "Debug is_leader value" 
task, but after that the task names in the log don't match those in the 
playbook.

In the playbook you posted, task "Add standby host" an task "Add primary 
host" both add the hosts to the same group. That may be a source of your 
problem. But as we don't see the output for those tasks …, hmm.
—
Todd

On 4/23/24 4:03 PM, 'deewon' via Ansible Project wrote:

ansible version: 2:11.2 
jinja version: 3.0.3
python version: 3.6.8

Hello all

I have a peculiar problem with a relatively simple playbook I have written. 
It basically checks 2 servers to determine which one has more than 1 IP and 
uses that to set a simp;e fact called "is_leader" for each node. 

This fact is then to be used  to build an in-memory inventory using module 
add_host to iterate through the servers based on the value of that fact. 
See below:

- name: Get Server Recycling Order
  hosts: "{{ my_servers | default ('localhost') }}"
  become: yes
  become_method: sudo
  become_user: root

  tasks:

- name: Set Patching Sequence
  tags: always
  block:

 - name: Get IP addresses of eth0 using nmcli command
   command: nmcli -g ip4.address conn show "System eth0"
   register: nmcli_output
   changed_when: false


 - name: Get nmcli output
   debug:
 var: nmcli_output


 - name: Set is_leader variable based on number of IPs
   set_fact:
 is_leader: "{{ (nmcli_output.stdout.split('|') | length) > 1 
}}"


 - name: Display if server is ACTIVE (1) or PASSIVE (0)
   debug:
 var: is_leader


 - name: Debug is_leader value
   debug:
  msg: "is_leader value for {{ inventory_hostname }} is {{ 
is_leader }}"


 - name: Add standby host
   add_host:
  name: "{{ inventory_hostname }}"
  groups: nodes
   when: not is_leader 

 - name: Add primary host
   add_host:
  name: "{{ inventory_hostname }}"
  groups: nodes
   when: is_leader

#- name: Execute tasks on servers in a specified order
#  hosts: nodes
#  gather_facts: no
#  serial: 1
#  tasks:
#- name: Run a command in a specified order
#  command: echo "Running on {{ inventory_hostname }}"
#  tags: always

The problem is that when this runs, the  "is_leader" fact is  ignored for 
when the condition is false and the standby node is  not added to the 
inventory. The relevant extract from the execution is below:

 TASK [Set is_leader variable based on number of IPs] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.079)   0:00:16.815 
*
ok: [node-1] => {
"ansible_facts": {
"is_leader": true
},
"changed": false
}
ok: [node-2] => {
"ansible_facts": {
"is_leader": false
},
"changed": false
}

TASK [Display if server is ACTIVE (1) or PASSIVE (0)] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.083)   0:00:16.898 
*
ok: [node-1] => {
"is_leader": true
}
ok: [node-2] => {
"is_leader": false
}

TASK [Debug is_leader value] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.072)   0:00:16.971 
*
ok: [node-1] => {}

MSG:

is_leader value for node-1 is True
ok: [node-2] => {}

MSG:

is_leader value for node-2 is False

TASK [Re-confirm is_leader before adding to

Re: [ansible-project] Unusal behaviour with set_fact and add_host

2024-04-25 Thread 'deewon' via Ansible Project
24 | 192.168.4.1/32",
"stdout_lines": [
"192.168.10.10/24 | 192.168.4.1/32"
]
}
}
ok: [node-2] => {
"nmcli_output": {
"changed": false,
"cmd": [
"nmcli",
"-g",
"ip4.address",
"conn",
"show",
"System eth0"
],
"delta": "0:00:00.026130",
"end": "2024-04-23 15:46:30.828281",
"failed": false,
"msg": "",
"rc": 0,
"start": "2024-04-23 15:46:30.802151",
"stderr": "",
"stderr_lines": [],
"stdout": "192.168.10.11/24",
"stdout_lines": [
"192.168.10.11/24"
]
}
}

TASK [Set is_leader variable based on number of IPs] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.079)   0:00:16.815 
*
ok: [node-1] => {
"ansible_facts": {
"is_leader": true
},
"changed": false
}
ok: [node-2] => {
"ansible_facts": {
"is_leader": false
},
"changed": false
}

TASK [Display if server is ACTIVE (1) or PASSIVE (0)] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.083)   0:00:16.898 
*
ok: [node-1] => {
"is_leader": true
}
ok: [node-2] => {
"is_leader": false
}

TASK [Debug is_leader value] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.072)   0:00:16.971 
*
ok: [node-1] => {}

MSG:

is_leader value for node-1 is True
ok: [node-2] => {}

MSG:

is_leader value for node-2 is False

TASK [Re-confirm is_leader before adding to group] 

Tuesday 23 April 2024  19:46:31 + (0:00:00.074)   0:00:17.045 
*
skipping: [node-1] => {}
ok: [node-2] => {}

MSG:

Preparing to add node-2 to stby group based on is_leader = False

TASK [Add standby proxysql node] 
***
Tuesday 23 April 2024  19:46:31 + (0:00:00.088)   0:00:17.134 
*
skipping: [node-1] => {
"changed": false,
"skip_reason": "Conditional result was False"
}

TASK [Re-confirm is_leader before adding to group] 

Tuesday 23 April 2024  19:46:31 + (0:00:00.036)   0:00:17.170 
*
ok: [node-1] => {}

MSG:

Preparing to add node-1 to prim group based on is_leader = True
skipping: [node-2] => {}

TASK [Add primary proxysql node] 
***
Tuesday 23 April 2024  19:46:31 + (0:00:00.059)   0:00:17.230 
*
changed: [node-1] => {
"add_host": {
"groups": [
"nodes_prim"
],
"host_name": "node-1",
"host_vars": {}
},
"changed": true
}

PLAY RECAP 
*
localhost  : ok=1changed=0unreachable=0failed=0 
   skipped=0rescued=0ignored=0
node-1 : ok=8changed=1unreachable=0failed=0skipped=2   
 rescued=0ignored=0
node-2 : ok=7changed=0unreachable=0failed=0skipped=1   
 rescued=0ignored=0


A lot of those debugging steps I added were simply to wrap my head around 
what could be causing the condition to be false in task "Add standby 
proxysql node".  

I will also  give the example you showed as a go to  reduce the number of 
tasks needed to achieve the same result

As you've probably seen, the goal was just to re-order the patching 
sequence on the machines starting with the server that has a single IP (the 
standby). I was eventually going to add both to a single group called 
"nodes" in the preferred order but that failed for the same reason as 

[ansible-project] Unusal behaviour with set_fact and add_host

2024-04-23 Thread 'deewon' via Ansible Project
ansible version: 2:11.2
jinja version: 3.0.3
python version: 3.6.8

Hello all

I have a peculiar problem with a relatively simple playbook I have written. 
It basically checks 2 servers to determine which one has more than 1 IP and 
uses that to set a simp;e fact called "is_leader" for each node. 

This fact is then to be used  to build an in-memory inventory using module 
add_host to iterate through the servers based on the value of that fact. 
See below:

- name: Get Server Recycling Order
  hosts: "{{ my_servers | default ('localhost') }}"
  become: yes
  become_method: sudo
  become_user: root

  tasks:

- name: Set Patching Sequence
  tags: always
  block:

 - name: Get IP addresses of eth0 using nmcli command
   command: nmcli -g ip4.address conn show "System eth0"
   register: nmcli_output
   changed_when: false


 - name: Get nmcli output
   debug:
 var: nmcli_output


 - name: Set is_leader variable based on number of IPs
   set_fact:
 is_leader: "{{ (nmcli_output.stdout.split('|') | length) > 1 
}}"


 - name: Display if server is ACTIVE (1) or PASSIVE (0)
   debug:
 var: is_leader


 - name: Debug is_leader value
   debug:
  msg: "is_leader value for {{ inventory_hostname }} is {{ 
is_leader }}"


 - name: Add standby host
   add_host:
  name: "{{ inventory_hostname }}"
  groups: nodes
   when: not is_leader 

 - name: Add primary host
   add_host:
  name: "{{ inventory_hostname }}"
  groups: nodes
   when: is_leader

#- name: Execute tasks on servers in a specified order
#  hosts: nodes
#  gather_facts: no
#  serial: 1
#  tasks:
#- name: Run a command in a specified order
#  command: echo "Running on {{ inventory_hostname }}"
#  tags: always

The problem is that when this runs, the  "is_leader" fact is  ignored for 
when the condition is false and the standby node is  not added to the 
inventory. The relevant extract from the execution is below:

 TASK [Set is_leader variable based on number of IPs] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.079)   0:00:16.815 
*
ok: [node-1] => {
"ansible_facts": {
"is_leader": true
},
"changed": false
}
ok: [node-2] => {
"ansible_facts": {
"is_leader": false
},
"changed": false
}

TASK [Display if server is ACTIVE (1) or PASSIVE (0)] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.083)   0:00:16.898 
*
ok: [node-1] => {
"is_leader": true
}
ok: [node-2] => {
"is_leader": false
}

TASK [Debug is_leader value] 
**
Tuesday 23 April 2024  19:46:31 + (0:00:00.072)   0:00:16.971 
*
ok: [node-1] => {}

MSG:

is_leader value for node-1 is True
ok: [node-2] => {}

MSG:

is_leader value for node-2 is False

TASK [Re-confirm is_leader before adding to group] 

Tuesday 23 April 2024  19:46:31 + (0:00:00.074)   0:00:17.045 
*
skipping: [node-1] => {}
ok: [node-2] => {}

MSG:

Preparing to add node-2 to stby group based on is_leader = False

TASK [Add standby proxysql node] 
***
Tuesday 23 April 2024  19:46:31 + (0:00:00.088)   0:00:17.134 
*
skipping: [node-1] => {
"changed": false,
"skip_reason": "Conditional result was False"
}

TASK [Re-confirm is_leader before adding to group] 

Tuesday 23 April 2024  19:46:31 + (0:00:00.036)   0:00:17.170 
*
ok: [node-1] => {}

MSG:

Preparing to add node-1 to prim group based on is_leader = True
skipping: [node-2] => {}

TASK [Add primary proxysql node] 
***
Tuesday 23 April 2024  19:46:31 + (0:00:00.059)   0:00:17.230 
*
changed: [node-1] => {
"add_host": {
"groups": [
"nodes_prim"
],
"host_name": "node-1",
"host_vars": {}
},
"changed": true
}

PLAY RECAP 

Re: [ansible-project] parted - azure managed disks - unrecognized disk label

2020-02-13 Thread 'deewon' via Ansible Project
I know right :) 

It was an isolated lab environment so no harm done. 

appreciated!

On Wednesday, February 12, 2020 at 5:09:57 PM UTC, Hugo Gonzalez wrote:
>
>
>
> On 2/11/20 6:23 AM, 'deewon' via Ansible Project wrote:
>
> I got to the bottom of this eventually :)
>
> The task was running on the controller which didn't have a /dev/sdc device 
> :)
>
>
> Whoa! careful there! :) 
>
> Hugo G. 
>

-- 
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/967b444e-df43-4546-9674-85d5654610d1%40googlegroups.com.


Re: [ansible-project] parted - azure managed disks - unrecognized disk label

2020-02-11 Thread 'deewon' via Ansible Project
I got to the bottom of this eventually :)

The task was running on the controller which didn't have a /dev/sdc device 
:)

Simply delegating the task to the remote node fixed the problem

Thanks!


On Tuesday, February 11, 2020 at 10:41:44 AM UTC, deewon wrote:
>
> Thanks Hugo!
>
> I gave this a go with the label explicitly specified and it returned the 
> same error i.e.
>
> - name: Partition Disk
>   parted:
> device: /dev/sdc
> number: 1
> state: present
> label: msdos
>
>   
>
> TASK [role_azure_disk : Partition Azure Disk] 
> ***
> task path: /etc/ansible/roles/role_azure_disk/tasks/main.yml:19
> fatal: [dbbox8]: FAILED! => {"changed": false, "err": "Error: Could not 
> stat device /dev/sdc - No such file or directory.\n", "msg": "Error while 
> getting device information with parted script: '/sbin/parted -s -m /dev/sdc 
> -- unit 'KiB' print'", "out": "", "rc": 1}
>
>
> I suppose the issue is more related to the disk profile as opposed to the 
> parted module in itself?  The disk itself was created as recommended by 
> azure (
> https://docs.microsoft.com/en-us/azure/virtual-machines/linux/add-disk) 
>
> Regards
>
> On Monday, February 10, 2020 at 7:01:17 PM UTC, Hugo Gonzalez wrote:
>>
>>
>> On 2/10/20 3:49 AM, 'deewon' via Ansible Project wrote:
>>
>> Hi all, 
>>
>> I'm pretty certain someone has figured this out so any hints will be 
>> appreciated
>>
>> I've attached an  azure managed disk to a linux vm  but noticed it 
>> doesn't have any disk label  i.e.
>>
>>
>> The disk label is the partition table, and will not be recognized for an 
>> unpartitioned disk. There is a parameter in the module called "label" with 
>> a default of "msdos" as the table format.
>>
>>
>> https://docs.ansible.com/ansible/latest/modules/parted_module.html#parameter-label
>>
>> So I guess you can pass it along your partition definitions. Just make 
>> sure it works idempotently, I haven't tested it.
>>
>> Hugo G.
>>
>>
>>
>>
>>
>> Consequently, the ansible parted module returns the below error when 
>> attempting to create a partition
>>
>> TASK [role_azure_disk : Partition Azure Disk] 
>> ***
>> task path: /etc/ansible/roles/role_azure_disk/tasks/main.yml:7
>> fatal: [labserver]: FAILED! => {"changed": false, "err": "Error: Could 
>> not stat device /dev/sdc - No such file or directory.\n", "msg": "Error 
>> while getting device information with parted script: '/sbin/parted -s -m 
>> /dev/sdc -- unit 'KiB' print'", "out": "", "rc": 1}
>>
>> To achieve idempotence when re-running playbooks , I'm trying to avoid 
>> using fdisk passed to the shell module  i.e. (echo n; echo p; echo 1; echo 
>> ; echo ; echo w) | sudo fdisk /dev/sdc
>>
>> Will appreciate any ideas on how to get around 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...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/49c9a80b-1302-4463-a6ee-0e1a2e049e8b%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/ansible-project/49c9a80b-1302-4463-a6ee-0e1a2e049e8b%40googlegroups.com?utm_medium=email_source=footer>
>> .
>>
>>

-- 
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/8b052201-0145-48cd-b48c-feb6a893300c%40googlegroups.com.


Re: [ansible-project] parted - azure managed disks - unrecognized disk label

2020-02-11 Thread 'deewon' via Ansible Project
Thanks Hugo!

I gave this a go with the label explicitly specified and it returned the 
same error i.e.

- name: Partition Disk
  parted:
device: /dev/sdc
number: 1
state: present
label: msdos

  

TASK [role_azure_disk : Partition Azure Disk] 
***
task path: /etc/ansible/roles/role_azure_disk/tasks/main.yml:19
fatal: [dbbox8]: FAILED! => {"changed": false, "err": "Error: Could not 
stat device /dev/sdc - No such file or directory.\n", "msg": "Error while 
getting device information with parted script: '/sbin/parted -s -m /dev/sdc 
-- unit 'KiB' print'", "out": "", "rc": 1}


I suppose the issue is more related to the disk profile as opposed to the 
parted module in itself?  The disk itself was created as recommended by 
azure (
https://docs.microsoft.com/en-us/azure/virtual-machines/linux/add-disk) 

Regards

On Monday, February 10, 2020 at 7:01:17 PM UTC, Hugo Gonzalez wrote:
>
>
> On 2/10/20 3:49 AM, 'deewon' via Ansible Project wrote:
>
> Hi all, 
>
> I'm pretty certain someone has figured this out so any hints will be 
> appreciated
>
> I've attached an  azure managed disk to a linux vm  but noticed it doesn't 
> have any disk label  i.e.
>
>
> The disk label is the partition table, and will not be recognized for an 
> unpartitioned disk. There is a parameter in the module called "label" with 
> a default of "msdos" as the table format.
>
>
> https://docs.ansible.com/ansible/latest/modules/parted_module.html#parameter-label
>
> So I guess you can pass it along your partition definitions. Just make 
> sure it works idempotently, I haven't tested it.
>
> Hugo G.
>
>
>
>
>
> Consequently, the ansible parted module returns the below error when 
> attempting to create a partition
>
> TASK [role_azure_disk : Partition Azure Disk] 
> ***
> task path: /etc/ansible/roles/role_azure_disk/tasks/main.yml:7
> fatal: [labserver]: FAILED! => {"changed": false, "err": "Error: Could not 
> stat device /dev/sdc - No such file or directory.\n", "msg": "Error while 
> getting device information with parted script: '/sbin/parted -s -m /dev/sdc 
> -- unit 'KiB' print'", "out": "", "rc": 1}
>
> To achieve idempotence when re-running playbooks , I'm trying to avoid 
> using fdisk passed to the shell module  i.e. (echo n; echo p; echo 1; echo 
> ; echo ; echo w) | sudo fdisk /dev/sdc
>
> Will appreciate any ideas on how to get around 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...@googlegroups.com .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/49c9a80b-1302-4463-a6ee-0e1a2e049e8b%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/ansible-project/49c9a80b-1302-4463-a6ee-0e1a2e049e8b%40googlegroups.com?utm_medium=email_source=footer>
> .
>
>

-- 
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/8f3c285c-3eb7-4737-b279-e8a166144ee7%40googlegroups.com.


[ansible-project] parted - azure managed disks - unrecognized disk label

2020-02-10 Thread 'deewon' via Ansible Project
Hi all,

I'm pretty certain someone has figured this out so any hints will be 
appreciated

I've attached an  azure managed disk to a linux vm  but noticed it doesn't 
have any disk label  i.e.


Error: /dev/sdc: unrecognised disk label
Model: Msft Virtual Disk (scsi)
Disk /dev/sdc: 18.3GB
Sector size (logical/physical): 512B/4096B
Partition Table: unknown
Disk Flags:


Consequently, the ansible parted module returns the below error when 
attempting to create a partition

TASK [role_azure_disk : Partition Azure Disk] 
***
task path: /etc/ansible/roles/role_azure_disk/tasks/main.yml:7
fatal: [labserver]: FAILED! => {"changed": false, "err": "Error: Could not 
stat device /dev/sdc - No such file or directory.\n", "msg": "Error while 
getting device information with parted script: '/sbin/parted -s -m /dev/sdc 
-- unit 'KiB' print'", "out": "", "rc": 1}

To achieve idempotence when re-running playbooks , I'm trying to avoid 
using fdisk passed to the shell module  i.e. (echo n; echo p; echo 1; echo 
; echo ; echo w) | sudo fdisk /dev/sdc

Will appreciate any ideas on how to get around 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 view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/49c9a80b-1302-4463-a6ee-0e1a2e049e8b%40googlegroups.com.


[ansible-project] vmware - add new guest to VM group

2019-05-08 Thread 'deewon' via Ansible Project
Hi,

Not sure if anyone has ever tried to do this: I'm trying to add a VM in 
ESXi to a specific VM group in a cluster. I stumbled on 
vmware_vm_vm_drs_rule_module.html 

 which 
seems to try to do the job but looks more like a sledgehammer! 

It deletes and recreates the VM group even if it exists! (when 
state=present) 

It seems there's no functionality to simply add a new VM to an existing VM 
group, without having to recreate the group and repopulate all the VMs

Will appreciate any ideas if anyone has been able 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/959f4db9-d4f8-4207-bbc0-2baa257efd2e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Re: access to register stdout_lines attributes

2018-05-18 Thread 'deewon' via Ansible Project
Got to the bottom of this eventually...

-debug: var= "{{ res.stdout_lines | last }}"

It's the last item in the list 

On Tuesday, May 15, 2018 at 2:15:57 PM UTC+1, deewon wrote:
>
> Hi,
>
> I have a task that executes a shell command and spews out a few lines. 
> Specifically, it probes a remote DB and and returns some information .  
> Please see below:
>
> - name: Test role delegation to apps
>   hosts: appboxes
>   tasks:
> - name: get cluster status from dbboxes
>   shell: mysqlsh --redirect-primary test123:test123@box1:3306 --cluster 
> -e "cluster.status().defaultReplicaSet.primary" --interactive --json
>   register: res
>   delegate_to: "{{ groups.mysqldbs | first }}"
>   run_once: True
>
>
> - name: check result
>   debug:
> var=res.stdout_lines
>   run_once: True
>
>
> #- name: check result
> #  debug:
> #var: "{{ item.value }}"
> # with_items: "{{ res.stdout_lines }}"
> #  run_one: True
>
>
>
>
>
> root@controller:/etc/ansible/playbooks# ansible-playbook test_delegate.yml
>
>
> PLAY [Test role delegation to apps] 
> *
>
>
> TASK [Gathering Facts] 
> **
> ok: [appbox1]
> ok: [appbox2]
>
>
> TASK [get cluster status from dbboxes] 
> **
> changed: [appbox1 -> dbbox1]
>
>
> TASK [check result] 
> *
> ok: [appbox1] => {
> "res.stdout_lines": [
> "{",
> "\"info\": \"Creating a session to 'test123@box1:3306'\"",
> "}",
> "{",
> "\"info\": \"Your MySQL connection id is 246 Server version: 
> 5.7.21-log MySQL Commercial Server (Advanced) No default schema selected; 
> type use  to set one.\"",
> "}",
> "{",
> "\"info\": \"Reconnecting to PRIMARY instance of the InnoDB 
> cluster (mysql://box1:3306)...\"",
> "}",
> "{",
> "\"info\": \"Creating a Classic session to 
> 'test123@box1:3306'\"",
> "}",
> "{",
> "\"info\": \"Closing old connection...\"",
> "}",
> "{",
> "\"info\": \"Your MySQL connection id is 239 Server version: 
> 5.7.21-log MySQL Commercial Server (Advanced) No default schema selected; 
> type use  to set one.\"",
> "}",
> "{",
> "\"value\": \"box1:3306\"",
> "}"
> ]
> }
>
>
> PLAY RECAP 
> **
> appbox1: ok=3changed=1unreachable=0failed=
> 0
> appbox2: ok=1changed=0unreachable=0failed=
> 0
>
>
> I've been  scratching my head trying to figure out how to return only the 
> "value" attribute and its corresponding result. 
>
> I tried using a loop (commented out in the playbook) but it just spews out 
> a load of errors saying the attribute doesn't exist i.e.
>
> TASK [check result] 
> *
> fatal: [appbox1]: FAILED! => {"msg": "The task includes an option with an 
> undefined variable. The error was: 
> 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 
> 'value'\n\nThe error appears to have been in 
> '/etc/ansible/playbooks/test_delegate.yml': line 15, column 7, but may\nbe 
> elsewhere in the file depending on the exact syntax problem.\n\nThe 
> offending line appears to be:\n\n\n- name: check result\n  ^ 
> here\n\nexception type:  'ansible.errors.AnsibleUndefinedVariable'>\nexception: 
> 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 
> 'value'"}
> fatal: [appbox2]: FAILED! => {"msg": "The task includes an option with an 
> undefined variable. The error was: 
> 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 
> 'value'\n\nThe error appears to have been in 
> '/etc/ansible/playbooks/test_delegate.yml': line 15, column 7, but may\nbe 
> 

Re: [ansible-project] delegate_to with --extra-vars usage

2018-05-17 Thread 'deewon' via Ansible Project
Thanks Brian.

This pretty much nailed it

Dayo

On Tuesday, May 15, 2018 at 6:28:25 PM UTC+1, Brian Coca wrote:
>
> quotes!: 
>
> delegate_to: "{{ groups[v_grp] | random }}" 
>
>
>
>
>
> -- 
> -- 
> 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/3d727962-1d2a-447a-8a05-95a10784fa79%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] delegate_to with --extra-vars usage

2018-05-17 Thread 'deewon' via Ansible Project
Thanks for the hint

On Tuesday, May 15, 2018 at 5:02:24 PM UTC+1, Brian Coca wrote:
>
> do --extra-vars="v_grp=testgrp" 
>
>
> -- 
> -- 
> 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/54aefec1-cf20-4450-bae6-74b22fd730e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] delegate_to with --extra-vars usage

2018-05-15 Thread 'deewon' via Ansible Project
Trying to do something that seems straightforward but can't work out why 
its failing.

I'm simply trying to pass a random server within a  group whose name is 
supplied at execution time to a task using "delegate_to" and  
"--extra-vars" but it doesn't like it i.e


  delegate_to: {{ groups.v_grp | random }}
>
>   

followed by:

  
ansible-playbook playbook.yml --extra-vars="{{ v_grp=testgrp }}" 



Execution throws up the below error. 

fatal: [machine1]: FAILED! => {"msg": "'dict object' has no attribute 
'v_grp'"}


The group does exist and the playbook actually executes when the group name 
testgrp is hard coded. Unfortunately, the idea is to be able to pass group 
names in dynamically at runtime

I've trawled through the web for any hints around getting this working but 
there's been no luck. The closes I found was a suggestion on another  
thread (dated 2014) but it didn't work either 
https://groups.google.com/forum/#!topic/ansible-project/wfsS1b8Qq64

I'm assuming this is definitely possible in ansible?


Thanks
Dayo

-- 
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/eba1c394-c757-4b1e-add8-11316d0bd62e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] access to register stdout_lines attributes

2018-05-15 Thread 'deewon' via Ansible Project
Hi,

I have a task that executes a shell command and spews out a few lines. 
Specifically, it probes a remote DB and and returns some information .  
Please see below:

- name: Test role delegation to apps
  hosts: appboxes
  tasks:
- name: get cluster status from dbboxes
  shell: mysqlsh --redirect-primary test123:test123@box1:3306 --cluster 
-e "cluster.status().defaultReplicaSet.primary" --interactive --json
  register: res
  delegate_to: "{{ groups.mysqldbs | first }}"
  run_once: True


- name: check result
  debug:
var=res.stdout_lines
  run_once: True


#- name: check result
#  debug:
#var: "{{ item.value }}"
# with_items: "{{ res.stdout_lines }}"
#  run_one: True





root@controller:/etc/ansible/playbooks# ansible-playbook test_delegate.yml


PLAY [Test role delegation to apps] 
*


TASK [Gathering Facts] 
**
ok: [appbox1]
ok: [appbox2]


TASK [get cluster status from dbboxes] 
**
changed: [appbox1 -> dbbox1]


TASK [check result] 
*
ok: [appbox1] => {
"res.stdout_lines": [
"{",
"\"info\": \"Creating a session to 'test123@box1:3306'\"",
"}",
"{",
"\"info\": \"Your MySQL connection id is 246 Server version: 
5.7.21-log MySQL Commercial Server (Advanced) No default schema selected; 
type use  to set one.\"",
"}",
"{",
"\"info\": \"Reconnecting to PRIMARY instance of the InnoDB 
cluster (mysql://box1:3306)...\"",
"}",
"{",
"\"info\": \"Creating a Classic session to 
'test123@box1:3306'\"",
"}",
"{",
"\"info\": \"Closing old connection...\"",
"}",
"{",
"\"info\": \"Your MySQL connection id is 239 Server version: 
5.7.21-log MySQL Commercial Server (Advanced) No default schema selected; 
type use  to set one.\"",
"}",
"{",
"\"value\": \"box1:3306\"",
"}"
]
}


PLAY RECAP 
**
appbox1: ok=3changed=1unreachable=0failed=0
appbox2: ok=1changed=0unreachable=0failed=0


I've been  scratching my head trying to figure out how to return only the 
"value" attribute and its corresponding result. 

I tried using a loop (commented out in the playbook) but it just spews out 
a load of errors saying the attribute doesn't exist i.e.

TASK [check result] 
*
fatal: [appbox1]: FAILED! => {"msg": "The task includes an option with an 
undefined variable. The error was: 
'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 
'value'\n\nThe error appears to have been in 
'/etc/ansible/playbooks/test_delegate.yml': line 15, column 7, but may\nbe 
elsewhere in the file depending on the exact syntax problem.\n\nThe 
offending line appears to be:\n\n\n- name: check result\n  ^ 
here\n\nexception type: \nexception: 
'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 
'value'"}
fatal: [appbox2]: FAILED! => {"msg": "The task includes an option with an 
undefined variable. The error was: 
'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 
'value'\n\nThe error appears to have been in 
'/etc/ansible/playbooks/test_delegate.yml': line 15, column 7, but may\nbe 
elsewhere in the file depending on the exact syntax problem.\n\nThe 
offending line appears to be:\n\n\n- name: check result\n  ^ 
here\n\nexception type: \nexception: 
'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 
'value'"}
to retry, use: --limit @/etc/ansible/playbooks/test_delegate.retry


PLAY RECAP 

Re: [ansible-project] merge dictionary list in jinja2

2018-02-21 Thread 'deewon' via Ansible Project
Thanks for the hint

On Tuesday, February 20, 2018 at 1:24:26 PM UTC, George Shuklin wrote:
>
> Try to use 'combine' filter:
>
> one: {foo: bar}
> two: {baz: bazbaz }
> three: '{{one|combine(two)}}'
>
> Result: three: {foo: bar, baz: bazbaz}
>
>
> On 02/20/2018 11:46 AM, 'deewon' via Ansible Project wrote:
>
> Hi all, 
>
> I'm relatively new to using ansible and hoping someone can assist with 
> some insight around this.
>
> I'm trying to create a jinja2 template that generates authentication 
> credentials for a database in json format 
>
> I've defined the variables using the format below (using dictionaries)
>
> vars:
> db_roles:
>  user1:
>- { db: "lab2", privs: "READ,DELETE" }
>  user2:
>- { db: "lab1", privs: "INSERT,DELETE" }
>- { db: "lab2", privs: "UPDATE" }
>
>
> The idea is to loop through the users (user1 and user2 in this example) 
> and assign the "privs" on the "db" for each user across multiple databases 
> as needed
>
>
>
> To do this, I created a jinja2 template that looks like the below:
>
> {% for item in db_roles %}
> {% for dict_item in db_roles[item] %}
>  ,
>   { "db" : "admin", "userName" : "{{ item }}",
>  "roles" : [
>{% for dbpriv in dict_item.privs.split(',') %}
>   {
>"db" : "{{ dict_item.db }}",
>"role" : "{{ dbpriv }}"
>   }
>{% if not loop.last %},{% endif %}
>{% endfor %}
>   ]
>   }
>  {% endfor %}
>  
>   {% endfor %}
>
>
> Using dict_items, I'm able to access the values in each dictionary and the 
> generated file looks this when executed:
>
> ,
>   { "db" : "admin", "userName" : "user2",
>  "roles" : [
>  {
>"db" : "lab1",
>"role" : "INSERT"
>   }
>, {
>"db" : "lab1",
>"role" : "DELETE"
>   }
> ]
>   }
>  ,
>   { "db" : "admin", "userName" : "user2",
>  "roles" : [
>  {
>"db" : "lab2",
>"role" : "UPDATE"
>   }
> ]
>   }
>   ,
>   { "db" : "admin", "userName" : "user1",
>  "roles" : [
>  {
>"db" : "lab2",
>"role" : "READ"
>   }
>, {
>"db" : "lab2",
>"role" : "DELETE"
>   }
> ]
>   }
>
>
>
> This is close to what I want with the exception of "user2"  whose 
> privileges seem to have been split into 2 separate documents. All the 
> privileges for each user should be defined within the roles array in the 
> same document similar to the below.
>
> { "db" : "admin", "userName" : "user2",
>  "roles" : [
>  {
>"db" : "lab1",
>"role" : "INSERT"
>   }
>   ,
>   {
>"db" : "lab1",
>"role" : "DELETE"
>   }
>   ,
>   {
>"db" : "lab2",
>"role" : "UPDATE"
>   }
>   ]
>}
>  ,
>  ,
>{ "db" : "admin", "roleName" : "user1",
>  "roles" : [
>  {
>"db" : "lab2",
>"role" : "READ"
>   }
>,
>{
>"db" : "lab2",
>"role" : "DELETE"
>}
>   ]
> }
>
>
>  I can't figure out how to merge the dictionary items for a single user 
> across different "db" values into a  sing

Re: [ansible-project] merge dictionary list in jinja2

2018-02-21 Thread 'deewon' via Ansible Project
Thanks Kai.

The example you gave came pretty close (see result below for user2) but  it 
kind of pointed me in the right direction and I think I've fixed it

{ "db" : "admin", "userName" : "user2",

  "roles" : [

   {
"db" : "lab1",
"role" : "INSERT"
   },

   {
"db" : "lab1",
"role" : "DELETE"
   }


  "roles" : [

   {
"db" : "lab2",
"role" : "UPDATE"
   }

   }
  

>From the above, I still get 2 separate roles array for a single user but in 
addition to the above, If I move the dictionary items lookup into the 
"roles: [ " section, I seem to achieve the desired result i.e.

{% for item in db_roles %}
  ,
   { "db" : "admin", "userName" : "{{ item }}",
  "roles" : [
{% for dict_item in db_roles[item] %}
{% for dbpriv in dict_item.privs.split(',') %}
   {
"db" : "{{ dict_item.db }}",
"role" : "{{ dbpriv }}"
   }
{%- if not loop.last %},{% endif %}
{% endfor %}
{%- if not loop.last %},{% endif %}
  {% endfor %}
   ]
   }
   {% endfor %}

Which gives me this:

 ,
   { "db" : "admin", "userName" : "user2",
  "roles" : [


   {
"db" : "lab1",
"role" : "INSERT"
   },

   {
"db" : "lab1",
"role" : "DELETE"
   }
,


   {
"db" : "lab2",
"role" : "UPDATE"
   }


   ]
   }

  ,
   { "db" : "admin", "userName" : "user1",
  "roles" : [


   {
"db" : "lab2",
"role" : "READ"
   },

   {
"db" : "lab2",
"role" : "DELETE"
   }


   ]
   }

Thank you :)

On Tuesday, February 20, 2018 at 11:34:14 AM UTC, Kai Stian Olstad wrote:
>
> On 20.02.2018 10:46, 'deewon' via Ansible Project wrote: 
> > vars: 
> > db_roles: 
> >  user1: 
> >- { db: "lab2", privs: "READ,DELETE" } 
> >  user2: 
> >- { db: "lab1", privs: "INSERT,DELETE" } 
> >- { db: "lab2", privs: "UPDATE" } 
> > 
> > 
> > To do this, I created a jinja2 template that looks like the below: 
> > 
> > {% for item in db_roles %} 
> > {% for dict_item in db_roles[item] %} 
> >  , 
> >   { "db" : "admin", "userName" : "{{ item }}", 
> >  "roles" : [ 
> >{% for dbpriv in dict_item.privs.split(',') %} 
> >   { 
> >"db" : "{{ dict_item.db }}", 
> >"role" : "{{ dbpriv }}" 
> >   } 
> >{% if not loop.last %},{% endif %} 
> >{% endfor %} 
> >   ] 
> >   } 
> >  {% endfor %} 
> > 
> >   {% endfor %} 
>
> You need to move your { "db" up one level. 
>
> {% for item in db_roles %} 
>   , 
>{ "db" : "admin", "userName" : "{{ item }}", 
>  {% for dict_item in db_roles[item] %} 
>   "roles" : [ 
> {% for dbpriv in dict_item.privs.split(',') %} 
>{ 
> "db" : "{{ dict_item.db }}", 
> "role" : "{{ dbpriv }}" 
>} 
> {%- if not loop.last %},{% endif %} 
> {% endfor %} 
>   {% endfor %} 
>] 
>} 
>{% endfor %} 
>
>
> I also added a "-" to the "if" so it comes after "}" and not on a 
> newline. 
>
>
> -- 
> Kai Stian Olstad 
>

-- 
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/af09c469-c551-4de2-b7ef-4cc567da39cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] merge dictionary list in jinja2

2018-02-20 Thread 'deewon' via Ansible Project
Hi all,

I'm relatively new to using ansible and hoping someone can assist with some 
insight around this.

I'm trying to create a jinja2 template that generates authentication 
credentials for a database in json format 

I've defined the variables using the format below (using dictionaries)

vars:
db_roles:
 user1:
   - { db: "lab2", privs: "READ,DELETE" }
 user2:
   - { db: "lab1", privs: "INSERT,DELETE" }
   - { db: "lab2", privs: "UPDATE" }


The idea is to loop through the users (user1 and user2 in this example) and 
assign the "privs" on the "db" for each user across multiple databases as 
needed



To do this, I created a jinja2 template that looks like the below:

{% for item in db_roles %}
{% for dict_item in db_roles[item] %}
 ,
  { "db" : "admin", "userName" : "{{ item }}",
 "roles" : [
   {% for dbpriv in dict_item.privs.split(',') %}
  {
   "db" : "{{ dict_item.db }}",
   "role" : "{{ dbpriv }}"
  }
   {% if not loop.last %},{% endif %}
   {% endfor %}
  ]
  }
 {% endfor %}
 
  {% endfor %}


Using dict_items, I'm able to access the values in each dictionary and the 
generated file looks this when executed:

,
  { "db" : "admin", "userName" : "user2",
 "roles" : [
 {
   "db" : "lab1",
   "role" : "INSERT"
  }
   , {
   "db" : "lab1",
   "role" : "DELETE"
  }
]
  }
 ,
  { "db" : "admin", "userName" : "user2",
 "roles" : [
 {
   "db" : "lab2",
   "role" : "UPDATE"
  }
]
  }
  ,
  { "db" : "admin", "userName" : "user1",
 "roles" : [
 {
   "db" : "lab2",
   "role" : "READ"
  }
   , {
   "db" : "lab2",
   "role" : "DELETE"
  }
]
  }



This is close to what I want with the exception of "user2"  whose 
privileges seem to have been split into 2 separate documents. All the 
privileges for each user should be defined within the roles array in the 
same document similar to the below.

{ "db" : "admin", "userName" : "user2",
 "roles" : [
 {
   "db" : "lab1",
   "role" : "INSERT"
  }
  ,
  {
   "db" : "lab1",
   "role" : "DELETE"
  }
  ,
  {
   "db" : "lab2",
   "role" : "UPDATE"
  }
  ]
   }
 ,
 ,
   { "db" : "admin", "roleName" : "user1",
 "roles" : [
 {
   "db" : "lab2",
   "role" : "READ"
  }
   ,
   {
   "db" : "lab2",
   "role" : "DELETE"
   }
  ]
}


 I can't figure out how to merge the dictionary items for a single user 
across different "db" values into a  single dictionary that  allows me 
build the json document to look like the above

Any insights or suggestions will be highly appreciated :)

Regards

-- 
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/83cd07c9-e67d-4186-9ac7-7433ba18fc35%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.