How can I reference a registered variable from one ansible play in another?
up vote
0
down vote
favorite
I need to check if VMs exist or not. The check is registered in one play, 'control' and referenced in another, 'production'. What is the correct format for this?
create_vm.yml:
---
- hosts: control
tasks:
- name: Check VM
virt:
command: "list_vms"
register: vms
- hosts: production
tasks:
- name: Create VM
STUFF
when: inventory_hostname not in groups['control']['vms']
I've tried different 'when' formats but they've all failed.
I have various errors depending on the format of when condition used; the following is for the specified condition.
fatal: [prod-operator]: FAILED! => {"failed": true, "msg": "The conditional check 'inventory_hostname not in groups['control']['vms']' failed. The error was: error while evaluating conditional (inventory_hostname not in groups['control']['vms']): Unable to look up a name or access an attribute in template string ({% if inventory_hostname not in groups['control']['vms'] %} True {% else %} False {% endif %}).nMake sure your variable name does not contain invalid characters like '-': argument of type 'StrictUndefined' is not iterablennThe error appears to have been in '/root/micks-ci-setup/production/virtual_machine/create-vm.yml': line 29, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn - name: Create Production VMn ^ heren"}
ansible
|
show 3 more comments
up vote
0
down vote
favorite
I need to check if VMs exist or not. The check is registered in one play, 'control' and referenced in another, 'production'. What is the correct format for this?
create_vm.yml:
---
- hosts: control
tasks:
- name: Check VM
virt:
command: "list_vms"
register: vms
- hosts: production
tasks:
- name: Create VM
STUFF
when: inventory_hostname not in groups['control']['vms']
I've tried different 'when' formats but they've all failed.
I have various errors depending on the format of when condition used; the following is for the specified condition.
fatal: [prod-operator]: FAILED! => {"failed": true, "msg": "The conditional check 'inventory_hostname not in groups['control']['vms']' failed. The error was: error while evaluating conditional (inventory_hostname not in groups['control']['vms']): Unable to look up a name or access an attribute in template string ({% if inventory_hostname not in groups['control']['vms'] %} True {% else %} False {% endif %}).nMake sure your variable name does not contain invalid characters like '-': argument of type 'StrictUndefined' is not iterablennThe error appears to have been in '/root/micks-ci-setup/production/virtual_machine/create-vm.yml': line 29, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn - name: Create Production VMn ^ heren"}
ansible
1
Possible duplicate of How do I set register a variable to persist between plays in ansible?
– Steve E.
Nov 7 at 9:23
I checked that and tried various adaptations but did not work for me. I may of course be overlooking something simple.
– mickt
Nov 7 at 10:09
could you please update the question with the actual error you're getting?
– Bruce Becker
Nov 7 at 11:38
Please see error above.
– mickt
Nov 7 at 13:44
Thanks! this error shows which task the problem is --- name: Create Production VM
. No surprises there, an argument is undefined and therefore not iterable. It looks like you're just referencing the variable incorrectly. It should beinventory_hostname not in vms
sincevms
is the name of the variable you're registering. If you're putting a host into a group, you should use add_host
– Bruce Becker
Nov 7 at 15:26
|
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to check if VMs exist or not. The check is registered in one play, 'control' and referenced in another, 'production'. What is the correct format for this?
create_vm.yml:
---
- hosts: control
tasks:
- name: Check VM
virt:
command: "list_vms"
register: vms
- hosts: production
tasks:
- name: Create VM
STUFF
when: inventory_hostname not in groups['control']['vms']
I've tried different 'when' formats but they've all failed.
I have various errors depending on the format of when condition used; the following is for the specified condition.
fatal: [prod-operator]: FAILED! => {"failed": true, "msg": "The conditional check 'inventory_hostname not in groups['control']['vms']' failed. The error was: error while evaluating conditional (inventory_hostname not in groups['control']['vms']): Unable to look up a name or access an attribute in template string ({% if inventory_hostname not in groups['control']['vms'] %} True {% else %} False {% endif %}).nMake sure your variable name does not contain invalid characters like '-': argument of type 'StrictUndefined' is not iterablennThe error appears to have been in '/root/micks-ci-setup/production/virtual_machine/create-vm.yml': line 29, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn - name: Create Production VMn ^ heren"}
ansible
I need to check if VMs exist or not. The check is registered in one play, 'control' and referenced in another, 'production'. What is the correct format for this?
create_vm.yml:
---
- hosts: control
tasks:
- name: Check VM
virt:
command: "list_vms"
register: vms
- hosts: production
tasks:
- name: Create VM
STUFF
when: inventory_hostname not in groups['control']['vms']
I've tried different 'when' formats but they've all failed.
I have various errors depending on the format of when condition used; the following is for the specified condition.
fatal: [prod-operator]: FAILED! => {"failed": true, "msg": "The conditional check 'inventory_hostname not in groups['control']['vms']' failed. The error was: error while evaluating conditional (inventory_hostname not in groups['control']['vms']): Unable to look up a name or access an attribute in template string ({% if inventory_hostname not in groups['control']['vms'] %} True {% else %} False {% endif %}).nMake sure your variable name does not contain invalid characters like '-': argument of type 'StrictUndefined' is not iterablennThe error appears to have been in '/root/micks-ci-setup/production/virtual_machine/create-vm.yml': line 29, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn - name: Create Production VMn ^ heren"}
ansible
ansible
edited Nov 7 at 15:24
asked Nov 7 at 7:00
mickt
125
125
1
Possible duplicate of How do I set register a variable to persist between plays in ansible?
– Steve E.
Nov 7 at 9:23
I checked that and tried various adaptations but did not work for me. I may of course be overlooking something simple.
– mickt
Nov 7 at 10:09
could you please update the question with the actual error you're getting?
– Bruce Becker
Nov 7 at 11:38
Please see error above.
– mickt
Nov 7 at 13:44
Thanks! this error shows which task the problem is --- name: Create Production VM
. No surprises there, an argument is undefined and therefore not iterable. It looks like you're just referencing the variable incorrectly. It should beinventory_hostname not in vms
sincevms
is the name of the variable you're registering. If you're putting a host into a group, you should use add_host
– Bruce Becker
Nov 7 at 15:26
|
show 3 more comments
1
Possible duplicate of How do I set register a variable to persist between plays in ansible?
– Steve E.
Nov 7 at 9:23
I checked that and tried various adaptations but did not work for me. I may of course be overlooking something simple.
– mickt
Nov 7 at 10:09
could you please update the question with the actual error you're getting?
– Bruce Becker
Nov 7 at 11:38
Please see error above.
– mickt
Nov 7 at 13:44
Thanks! this error shows which task the problem is --- name: Create Production VM
. No surprises there, an argument is undefined and therefore not iterable. It looks like you're just referencing the variable incorrectly. It should beinventory_hostname not in vms
sincevms
is the name of the variable you're registering. If you're putting a host into a group, you should use add_host
– Bruce Becker
Nov 7 at 15:26
1
1
Possible duplicate of How do I set register a variable to persist between plays in ansible?
– Steve E.
Nov 7 at 9:23
Possible duplicate of How do I set register a variable to persist between plays in ansible?
– Steve E.
Nov 7 at 9:23
I checked that and tried various adaptations but did not work for me. I may of course be overlooking something simple.
– mickt
Nov 7 at 10:09
I checked that and tried various adaptations but did not work for me. I may of course be overlooking something simple.
– mickt
Nov 7 at 10:09
could you please update the question with the actual error you're getting?
– Bruce Becker
Nov 7 at 11:38
could you please update the question with the actual error you're getting?
– Bruce Becker
Nov 7 at 11:38
Please see error above.
– mickt
Nov 7 at 13:44
Please see error above.
– mickt
Nov 7 at 13:44
Thanks! this error shows which task the problem is --
- name: Create Production VM
. No surprises there, an argument is undefined and therefore not iterable. It looks like you're just referencing the variable incorrectly. It should be inventory_hostname not in vms
since vms
is the name of the variable you're registering. If you're putting a host into a group, you should use add_host– Bruce Becker
Nov 7 at 15:26
Thanks! this error shows which task the problem is --
- name: Create Production VM
. No surprises there, an argument is undefined and therefore not iterable. It looks like you're just referencing the variable incorrectly. It should be inventory_hostname not in vms
since vms
is the name of the variable you're registering. If you're putting a host into a group, you should use add_host– Bruce Becker
Nov 7 at 15:26
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
From what I've seen I should be able to reference the registered value form another task using a reference to host but it just won't work for me. I'll move on with a workaround of performing another check in the hosts: production play as follows.
(I'm doing a round-robin deployment of VMs hence the delegate_to complexity)
---
- hosts: control
tasks:
- name: Check VM
virt:
command: "list_vms"
register: vms
- hosts: production
tasks:
- name: Check VMs
virt:
command: "list_vms"
register: vms
delegate_to: '{{ groups["control"][play_hosts.index(inventory_hostname) % groups["control"]|length] }}'
And when condition as follows:
when: inventory_hostname not in vms.list_vms
If anyone can provide the "correct" method or a reason why it's not working for me then please feel free to post another answer.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
From what I've seen I should be able to reference the registered value form another task using a reference to host but it just won't work for me. I'll move on with a workaround of performing another check in the hosts: production play as follows.
(I'm doing a round-robin deployment of VMs hence the delegate_to complexity)
---
- hosts: control
tasks:
- name: Check VM
virt:
command: "list_vms"
register: vms
- hosts: production
tasks:
- name: Check VMs
virt:
command: "list_vms"
register: vms
delegate_to: '{{ groups["control"][play_hosts.index(inventory_hostname) % groups["control"]|length] }}'
And when condition as follows:
when: inventory_hostname not in vms.list_vms
If anyone can provide the "correct" method or a reason why it's not working for me then please feel free to post another answer.
add a comment |
up vote
0
down vote
From what I've seen I should be able to reference the registered value form another task using a reference to host but it just won't work for me. I'll move on with a workaround of performing another check in the hosts: production play as follows.
(I'm doing a round-robin deployment of VMs hence the delegate_to complexity)
---
- hosts: control
tasks:
- name: Check VM
virt:
command: "list_vms"
register: vms
- hosts: production
tasks:
- name: Check VMs
virt:
command: "list_vms"
register: vms
delegate_to: '{{ groups["control"][play_hosts.index(inventory_hostname) % groups["control"]|length] }}'
And when condition as follows:
when: inventory_hostname not in vms.list_vms
If anyone can provide the "correct" method or a reason why it's not working for me then please feel free to post another answer.
add a comment |
up vote
0
down vote
up vote
0
down vote
From what I've seen I should be able to reference the registered value form another task using a reference to host but it just won't work for me. I'll move on with a workaround of performing another check in the hosts: production play as follows.
(I'm doing a round-robin deployment of VMs hence the delegate_to complexity)
---
- hosts: control
tasks:
- name: Check VM
virt:
command: "list_vms"
register: vms
- hosts: production
tasks:
- name: Check VMs
virt:
command: "list_vms"
register: vms
delegate_to: '{{ groups["control"][play_hosts.index(inventory_hostname) % groups["control"]|length] }}'
And when condition as follows:
when: inventory_hostname not in vms.list_vms
If anyone can provide the "correct" method or a reason why it's not working for me then please feel free to post another answer.
From what I've seen I should be able to reference the registered value form another task using a reference to host but it just won't work for me. I'll move on with a workaround of performing another check in the hosts: production play as follows.
(I'm doing a round-robin deployment of VMs hence the delegate_to complexity)
---
- hosts: control
tasks:
- name: Check VM
virt:
command: "list_vms"
register: vms
- hosts: production
tasks:
- name: Check VMs
virt:
command: "list_vms"
register: vms
delegate_to: '{{ groups["control"][play_hosts.index(inventory_hostname) % groups["control"]|length] }}'
And when condition as follows:
when: inventory_hostname not in vms.list_vms
If anyone can provide the "correct" method or a reason why it's not working for me then please feel free to post another answer.
edited Nov 8 at 7:36
answered Nov 7 at 20:14
mickt
125
125
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53184826%2fhow-can-i-reference-a-registered-variable-from-one-ansible-play-in-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Possible duplicate of How do I set register a variable to persist between plays in ansible?
– Steve E.
Nov 7 at 9:23
I checked that and tried various adaptations but did not work for me. I may of course be overlooking something simple.
– mickt
Nov 7 at 10:09
could you please update the question with the actual error you're getting?
– Bruce Becker
Nov 7 at 11:38
Please see error above.
– mickt
Nov 7 at 13:44
Thanks! this error shows which task the problem is --
- name: Create Production VM
. No surprises there, an argument is undefined and therefore not iterable. It looks like you're just referencing the variable incorrectly. It should beinventory_hostname not in vms
sincevms
is the name of the variable you're registering. If you're putting a host into a group, you should use add_host– Bruce Becker
Nov 7 at 15:26