Ansible: Unable to store command in variable and use it in different hostRun command on the Ansible hostHow to get the host name of the current machine as defined in the Ansible hosts file?Email results of ansible playbook / shell command?Using facts from one host group to configure another host group with AnsibleAnsible 'Failed to connect to the host via ssh'Ansible playbook variable/facts not available later on in playbookAnsible shell module execute kubectl error connection to localhost refusedkubeadm token create with AnsibleAnsible Error related to Stat and Command modules debug statementsTypeError: call_api() got an unexpected keyword argument 'async'

Why in most German places is the church the tallest building?

What is the difference between "Grippe" and "Männergrippe"?

Round towards zero

Sci fi film similar to Village of the Damned

Justifying the use of directed energy weapons

What is the difference between Major and Minor Bug?

Which household object drew this pattern?

Dataset human solutions for OR problems

find the path of a data extension for a SQL Query

Is using a hyperlink to close a modal a poor design decision?

Handling Disruptive Student on the Autistic Spectrum

How to gently end involvement with an online community?

Result of pgmathsetmacro is lost in loop iterations

What is the history of the university asylum law?

Prove your innocence

Thank God it's Friday, tomorrow is THE weekend. Why the definite article?

If all stars rotate, why was there a theory developed that requires non-rotating stars?

Was it ever possible to target a zone?

What is a CirKle Word™?

Did the British navy fail to take into account the ballistics correction due to Coriolis force during WW1 Falkland Islands battle?

Architectural feasibility of a tiered circular stone keep

Is it possible to perform a regression where you have an unknown / unknowable feature variable?

How can I unambiguously ask for a new user's "Display Name"?

Can't stopover at Sapporo when going from Asahikawa to Chitose airport?



Ansible: Unable to store command in variable and use it in different host


Run command on the Ansible hostHow to get the host name of the current machine as defined in the Ansible hosts file?Email results of ansible playbook / shell command?Using facts from one host group to configure another host group with AnsibleAnsible 'Failed to connect to the host via ssh'Ansible playbook variable/facts not available later on in playbookAnsible shell module execute kubectl error connection to localhost refusedkubeadm token create with AnsibleAnsible Error related to Stat and Command modules debug statementsTypeError: call_api() got an unexpected keyword argument 'async'






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am trying to set up a kubernetes cluster using kubeadm



I want to fetch the join command (done on the master node) and run it on worker node.



The following approach does not work:



- name: kubernetes.yml --> Get the join command
shell: kubeadm token create --print-join-command
register: rv_join_command
when: inventory_hostname in (groups['masters'] | last)
become: yes


- name: kubernetes.yml --> Store the join command
set_fact:
join_command: " rv_join_command.stdout "
run_once: true
when: inventory_hostname in (groups['masters'] | last)


- name: DEBUG kubernetes.yml --> Print the join command
debug:
var: rv_join_command.stdout
when: inventory_hostname in (groups['masters'] | last)


- name: kubernetes.yml --> Run the join command on kubernetes nodes
shell: " join_command "
when: inventory_hostname in groups['nodes']


as it fails with:




'join_command' is undefined




But the approach below also fails:



- name: kubernetes.yml --> Run the join command on kubernetes nodes
shell: " rv_join_command.stdout "
when: inventory_hostname in groups['nodes']
become: yes



The error was: 'dict object' has no attribute 'stdout'




Any suggestions?










share|improve this question


























  • Have you used "-vvv" option to Ansible Playbook and verified that "join_command" variable is indeed set, and as per your expectations?

    – Kishan Parekh
    Mar 27 at 18:01






  • 1





    It's hard to tell without seeing your actual playbook, but I suspect the run_once: true is tripping you up. set_fact sets a fact on the current host; if there are multiple hosts in your play, this fact will only be available on one of them.

    – larsks
    Mar 27 at 18:23











  • I removed run_once but I get the same results;

    – pkaramol
    Mar 27 at 20:33

















0















I am trying to set up a kubernetes cluster using kubeadm



I want to fetch the join command (done on the master node) and run it on worker node.



The following approach does not work:



- name: kubernetes.yml --> Get the join command
shell: kubeadm token create --print-join-command
register: rv_join_command
when: inventory_hostname in (groups['masters'] | last)
become: yes


- name: kubernetes.yml --> Store the join command
set_fact:
join_command: " rv_join_command.stdout "
run_once: true
when: inventory_hostname in (groups['masters'] | last)


- name: DEBUG kubernetes.yml --> Print the join command
debug:
var: rv_join_command.stdout
when: inventory_hostname in (groups['masters'] | last)


- name: kubernetes.yml --> Run the join command on kubernetes nodes
shell: " join_command "
when: inventory_hostname in groups['nodes']


as it fails with:




'join_command' is undefined




But the approach below also fails:



- name: kubernetes.yml --> Run the join command on kubernetes nodes
shell: " rv_join_command.stdout "
when: inventory_hostname in groups['nodes']
become: yes



The error was: 'dict object' has no attribute 'stdout'




Any suggestions?










share|improve this question


























  • Have you used "-vvv" option to Ansible Playbook and verified that "join_command" variable is indeed set, and as per your expectations?

    – Kishan Parekh
    Mar 27 at 18:01






  • 1





    It's hard to tell without seeing your actual playbook, but I suspect the run_once: true is tripping you up. set_fact sets a fact on the current host; if there are multiple hosts in your play, this fact will only be available on one of them.

    – larsks
    Mar 27 at 18:23











  • I removed run_once but I get the same results;

    – pkaramol
    Mar 27 at 20:33













0












0








0








I am trying to set up a kubernetes cluster using kubeadm



I want to fetch the join command (done on the master node) and run it on worker node.



The following approach does not work:



- name: kubernetes.yml --> Get the join command
shell: kubeadm token create --print-join-command
register: rv_join_command
when: inventory_hostname in (groups['masters'] | last)
become: yes


- name: kubernetes.yml --> Store the join command
set_fact:
join_command: " rv_join_command.stdout "
run_once: true
when: inventory_hostname in (groups['masters'] | last)


- name: DEBUG kubernetes.yml --> Print the join command
debug:
var: rv_join_command.stdout
when: inventory_hostname in (groups['masters'] | last)


- name: kubernetes.yml --> Run the join command on kubernetes nodes
shell: " join_command "
when: inventory_hostname in groups['nodes']


as it fails with:




'join_command' is undefined




But the approach below also fails:



- name: kubernetes.yml --> Run the join command on kubernetes nodes
shell: " rv_join_command.stdout "
when: inventory_hostname in groups['nodes']
become: yes



The error was: 'dict object' has no attribute 'stdout'




Any suggestions?










share|improve this question
















I am trying to set up a kubernetes cluster using kubeadm



I want to fetch the join command (done on the master node) and run it on worker node.



The following approach does not work:



- name: kubernetes.yml --> Get the join command
shell: kubeadm token create --print-join-command
register: rv_join_command
when: inventory_hostname in (groups['masters'] | last)
become: yes


- name: kubernetes.yml --> Store the join command
set_fact:
join_command: " rv_join_command.stdout "
run_once: true
when: inventory_hostname in (groups['masters'] | last)


- name: DEBUG kubernetes.yml --> Print the join command
debug:
var: rv_join_command.stdout
when: inventory_hostname in (groups['masters'] | last)


- name: kubernetes.yml --> Run the join command on kubernetes nodes
shell: " join_command "
when: inventory_hostname in groups['nodes']


as it fails with:




'join_command' is undefined




But the approach below also fails:



- name: kubernetes.yml --> Run the join command on kubernetes nodes
shell: " rv_join_command.stdout "
when: inventory_hostname in groups['nodes']
become: yes



The error was: 'dict object' has no attribute 'stdout'




Any suggestions?







ansible






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 17:53









larsks

134k23 gold badges219 silver badges221 bronze badges




134k23 gold badges219 silver badges221 bronze badges










asked Mar 27 at 17:28









pkaramolpkaramol

2,7841 gold badge27 silver badges71 bronze badges




2,7841 gold badge27 silver badges71 bronze badges















  • Have you used "-vvv" option to Ansible Playbook and verified that "join_command" variable is indeed set, and as per your expectations?

    – Kishan Parekh
    Mar 27 at 18:01






  • 1





    It's hard to tell without seeing your actual playbook, but I suspect the run_once: true is tripping you up. set_fact sets a fact on the current host; if there are multiple hosts in your play, this fact will only be available on one of them.

    – larsks
    Mar 27 at 18:23











  • I removed run_once but I get the same results;

    – pkaramol
    Mar 27 at 20:33

















  • Have you used "-vvv" option to Ansible Playbook and verified that "join_command" variable is indeed set, and as per your expectations?

    – Kishan Parekh
    Mar 27 at 18:01






  • 1





    It's hard to tell without seeing your actual playbook, but I suspect the run_once: true is tripping you up. set_fact sets a fact on the current host; if there are multiple hosts in your play, this fact will only be available on one of them.

    – larsks
    Mar 27 at 18:23











  • I removed run_once but I get the same results;

    – pkaramol
    Mar 27 at 20:33
















Have you used "-vvv" option to Ansible Playbook and verified that "join_command" variable is indeed set, and as per your expectations?

– Kishan Parekh
Mar 27 at 18:01





Have you used "-vvv" option to Ansible Playbook and verified that "join_command" variable is indeed set, and as per your expectations?

– Kishan Parekh
Mar 27 at 18:01




1




1





It's hard to tell without seeing your actual playbook, but I suspect the run_once: true is tripping you up. set_fact sets a fact on the current host; if there are multiple hosts in your play, this fact will only be available on one of them.

– larsks
Mar 27 at 18:23





It's hard to tell without seeing your actual playbook, but I suspect the run_once: true is tripping you up. set_fact sets a fact on the current host; if there are multiple hosts in your play, this fact will only be available on one of them.

– larsks
Mar 27 at 18:23













I removed run_once but I get the same results;

– pkaramol
Mar 27 at 20:33





I removed run_once but I get the same results;

– pkaramol
Mar 27 at 20:33












1 Answer
1






active

oldest

votes


















0















As pointed out in larsks comment above, you are storing the join_command var only for a single host (groups['masters'] | last) and it is only available there.



Meanwhile, it is possible to access vars from other hosts from anywhere.



You basically have two options:



  1. Modify the set_fact to store the var on each node from the result on your master

  2. Modify the join command run to use the stored var on master on every node

Option 1



- name: kubernetes.yml --> Store the join command
set_fact:
join_command: " hostvars[groups['masters'] "
when: inventory_hostname in groups['nodes']
# Now you can use join_command on each node


Option 2



# Leave set_fact as it was and then...
- name: kubernetes.yml --> Run the join command on kubernetes nodes
shell: " last].join_command "
when: inventory_hostname in groups['nodes']





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',
    autoActivateHeartbeat: false,
    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%2f55383269%2fansible-unable-to-store-command-in-variable-and-use-it-in-different-host%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









    0















    As pointed out in larsks comment above, you are storing the join_command var only for a single host (groups['masters'] | last) and it is only available there.



    Meanwhile, it is possible to access vars from other hosts from anywhere.



    You basically have two options:



    1. Modify the set_fact to store the var on each node from the result on your master

    2. Modify the join command run to use the stored var on master on every node

    Option 1



    - name: kubernetes.yml --> Store the join command
    set_fact:
    join_command: " hostvars[groups['masters'] "
    when: inventory_hostname in groups['nodes']
    # Now you can use join_command on each node


    Option 2



    # Leave set_fact as it was and then...
    - name: kubernetes.yml --> Run the join command on kubernetes nodes
    shell: " last].join_command "
    when: inventory_hostname in groups['nodes']





    share|improve this answer































      0















      As pointed out in larsks comment above, you are storing the join_command var only for a single host (groups['masters'] | last) and it is only available there.



      Meanwhile, it is possible to access vars from other hosts from anywhere.



      You basically have two options:



      1. Modify the set_fact to store the var on each node from the result on your master

      2. Modify the join command run to use the stored var on master on every node

      Option 1



      - name: kubernetes.yml --> Store the join command
      set_fact:
      join_command: " hostvars[groups['masters'] "
      when: inventory_hostname in groups['nodes']
      # Now you can use join_command on each node


      Option 2



      # Leave set_fact as it was and then...
      - name: kubernetes.yml --> Run the join command on kubernetes nodes
      shell: " last].join_command "
      when: inventory_hostname in groups['nodes']





      share|improve this answer





























        0














        0










        0









        As pointed out in larsks comment above, you are storing the join_command var only for a single host (groups['masters'] | last) and it is only available there.



        Meanwhile, it is possible to access vars from other hosts from anywhere.



        You basically have two options:



        1. Modify the set_fact to store the var on each node from the result on your master

        2. Modify the join command run to use the stored var on master on every node

        Option 1



        - name: kubernetes.yml --> Store the join command
        set_fact:
        join_command: " hostvars[groups['masters'] "
        when: inventory_hostname in groups['nodes']
        # Now you can use join_command on each node


        Option 2



        # Leave set_fact as it was and then...
        - name: kubernetes.yml --> Run the join command on kubernetes nodes
        shell: " last].join_command "
        when: inventory_hostname in groups['nodes']





        share|improve this answer















        As pointed out in larsks comment above, you are storing the join_command var only for a single host (groups['masters'] | last) and it is only available there.



        Meanwhile, it is possible to access vars from other hosts from anywhere.



        You basically have two options:



        1. Modify the set_fact to store the var on each node from the result on your master

        2. Modify the join command run to use the stored var on master on every node

        Option 1



        - name: kubernetes.yml --> Store the join command
        set_fact:
        join_command: " hostvars[groups['masters'] "
        when: inventory_hostname in groups['nodes']
        # Now you can use join_command on each node


        Option 2



        # Leave set_fact as it was and then...
        - name: kubernetes.yml --> Run the join command on kubernetes nodes
        shell: " last].join_command "
        when: inventory_hostname in groups['nodes']






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 27 at 19:41

























        answered Mar 27 at 19:35









        ZeitounatorZeitounator

        2,4331 gold badge4 silver badges19 bronze badges




        2,4331 gold badge4 silver badges19 bronze badges





















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55383269%2fansible-unable-to-store-command-in-variable-and-use-it-in-different-host%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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현