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"}










share|improve this question




















  • 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 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















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"}










share|improve this question




















  • 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 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













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"}










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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 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








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












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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer



























      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.






      share|improve this answer

























        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.






        share|improve this 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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 7:36

























        answered Nov 7 at 20:14









        mickt

        125




        125






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini