I'm wrong.... it's not hostvars['master'], because master is not the name of your first host in group master

Le 20/01/2021 à 08:33, Jean-Yves LENHOF a écrit :
You're looking for something like this I think (not tested) :

    - name: add master node
      shell: "kubeadm join {{ groups['master']|first }}:6443 --token {{hostvars['master']['kubeadm_token'].stdout}} --discovery-token-ca-cert-hash sha256:{{hostvars['master']['kubeadm_hash'].stdout}} --control-plane"
      when: inventory_hostname != groups['master']|first

Please be careful using special hostvars variable, there's no control in this array if the variable exist, etc... so use with caution

Regards,

JYL

Le 20/01/2021 à 08:17, liyo...@126.com a écrit :
I know,but I want get the token in A host,use it in other hosts,how to do it ?

在2021年1月20日星期三 UTC+8 下午2:31:36<dick....@geant.org> 写道:

    Hii

    You're registering a variable for one host (47.108.222.84) but
    then try to use it for another (47.108.213.148).

    I'm have no experience with kubeadm but I think your logic wrt
    host selection should be improved, so that you can reliably pick
    the variable from a stable group name, instead of "the last item".


    On Wed, 20 Jan 2021 at 05:26, liyo...@126.com <liyo...@126.com>
    wrote:

        when I edit a playbook test.yml:

        ---
        - hosts: master
          gather_facts: no
          tasks:
            - name: register hash
              shell: "openssl x509 -pubkey -in
        /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der
        2>/dev/null | openssl dgst -sha256 -hex | sed 's/^ .* //'|cut
        -d' ' -f 2"
              register: kubeadm_hash
              when: inventory_hostname == groups['master']|first

            - name: display hash
              debug: msg="{{kubeadm_hash.stdout}}"
              when: inventory_hostname == groups['master']|first

            - name: register token
              shell: kubeadm token list |grep forever|awk '{print $1}'
              register: kubeadm_token
              when: inventory_hostname == groups['master']|first
            - name: display token
              debug: msg="{{kubeadm_token.stdout}}"
              when: inventory_hostname == groups['master']|first
            - name: add master node
              shell: "kubeadm join {{ groups['master'][0] }}:6443
        --token {{kubeadm_token.stdout}}
        --discovery-token-ca-cert-hash
        sha256:{{kubeadm_hash.stdout}}  --control-plane"
              when: inventory_hostname != groups['master']|first


        the results is below:
        PLAY [master]
        
***************************************************************************************************************************************************************

        TASK [register hash]
        
********************************************************************************************************************************************************
        skipping: [47.108.234.26]
        skipping: [47.108.213.148]
        changed: [47.108.222.84]

        TASK [display hash]
        
*********************************************************************************************************************************************************
        ok: [47.108.222.84] => {
            "msg":
        "af040f8e06e320e264a79f62b677b3267f4b681d869408658bdd121fa568216c"
        }
        skipping: [47.108.213.148]
        skipping: [47.108.234.26]

        TASK [register token]
        
*******************************************************************************************************************************************************
        skipping: [47.108.234.26]
        skipping: [47.108.213.148]
        changed: [47.108.222.84]

        TASK [display token]
        
********************************************************************************************************************************************************
        ok: [47.108.222.84] => {
            "msg": "otnb1h.cvagwwe7tgdvzbtx"
        }
        skipping: [47.108.234.26]
        skipping: [47.108.213.148]

        TASK [add master node]
        
******************************************************************************************************************************************************
        skipping: [47.108.222.84]
        fatal: [47.108.234.26]: FAILED! => {"msg": "The task includes
        an option with an undefined variable. The error was: 'dict
        object' has no attribute 'stdout'\n\nThe error appears to be
        in '/root/test.yml': line 23, 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: add master
        node\n      ^ here\n"}
        fatal: [47.108.213.148]: FAILED! => {"msg": "The task
        includes an option with an undefined variable. The error was:
        'dict object' has no attribute 'stdout'\n\nThe error appears
        to be in '/root/test.yml': line 23, 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: add master node\n      ^ here\n"}

        PLAY RECAP
        
******************************************************************************************************************************************************************
        47.108.213.148             : ok=0    changed=0  
        unreachable=0    failed=1    skipped=4 rescued=0    ignored=0
        47.108.222.84              : ok=4    changed=2  
        unreachable=0    failed=0    skipped=1 rescued=0    ignored=0
        47.108.234.26              : ok=0    changed=0  
        unreachable=0    failed=1    skipped=4 rescued=0    ignored=0

        how to use it ?

-- You received this message because you are subscribed to the
        Google Groups "Ansible Project" group.
        To unsubscribe from this group and stop receiving emails from
        it, send an email to ansible-proje...@googlegroups.com.
        To view this discussion on the web visit
        
https://groups.google.com/d/msgid/ansible-project/21ca354e-a0ed-4d10-9d01-8f48f170d22cn%40googlegroups.com
        
<https://groups.google.com/d/msgid/ansible-project/21ca354e-a0ed-4d10-9d01-8f48f170d22cn%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- Sent from a mobile device - please excuse the brevity, spelling
    and punctuation.

--
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 <mailto:ansible-project+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/647f4749-f97f-4522-a368-c95a25878790n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/647f4749-f97f-4522-a368-c95a25878790n%40googlegroups.com?utm_medium=email&utm_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 <mailto:ansible-project+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1ed8d8ac-3d8b-29cd-576b-a26fecb91ed7%40lenhof.eu.org <https://groups.google.com/d/msgid/ansible-project/1ed8d8ac-3d8b-29cd-576b-a26fecb91ed7%40lenhof.eu.org?utm_medium=email&utm_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/bee82960-1fb1-4b1d-07c1-ab27eae157f9%40lenhof.eu.org.

Reply via email to