Skip to main content
deleted 19 characters in body; edited title
Source Link
Pierre.Vriens
  • 7.2k
  • 14
  • 37
  • 84

Ansible - using How to use facts from one host to perform actions on another host?

In Ansible I'm trying to generate a variable from a task on one host and then use the variable to do something to another host. Specifically, I'm trying to create a Kubernetes cluster - On the control plane node I can save the join command into a variable, but this appears to be scoped to the source host only - I can't work out how to access that variable from another host.

- name: read join command from the controlPlane
  shell: kubeadm token create --print-join-command
  when: inventory_hostname in groups['controlPlane']
  register: joinCmd

- name: join worker nodes to cluster
  command: "{{ joinCmd.stdout }}"
  when: inventory_hostname in groups['workerNodes']

The above fails on the second task (The task includes an option with an undefined variable. the error was: 'dict object' has no attribute 'stdout'). The standard approach seems to be save the line to a file locally, copy the file to the worker nodes and execute it locally which seems cumbersome (and leaves the join token lying around). 

How can I achieve this with variables only?

thanks,

Jim

Ansible - using facts from one host to perform actions on another host

In Ansible I'm trying to generate a variable from a task on one host and then use the variable to do something to another host. Specifically, I'm trying to create a Kubernetes cluster - On the control plane node I can save the join command into a variable, but this appears to be scoped to the source host only - I can't work out how to access that variable from another host.

- name: read join command from the controlPlane
  shell: kubeadm token create --print-join-command
  when: inventory_hostname in groups['controlPlane']
  register: joinCmd

- name: join worker nodes to cluster
  command: "{{ joinCmd.stdout }}"
  when: inventory_hostname in groups['workerNodes']

The above fails on the second task (The task includes an option with an undefined variable. the error was: 'dict object' has no attribute 'stdout'). The standard approach seems to be save the line to a file locally, copy the file to the worker nodes and execute it locally which seems cumbersome (and leaves the join token lying around). How can I achieve this with variables only?

thanks,

Jim

How to use facts from one host to perform actions on another host?

In Ansible I'm trying to generate a variable from a task on one host and then use the variable to do something to another host. Specifically, I'm trying to create a Kubernetes cluster - On the control plane node I can save the join command into a variable, but this appears to be scoped to the source host only - I can't work out how to access that variable from another host.

- name: read join command from the controlPlane
  shell: kubeadm token create --print-join-command
  when: inventory_hostname in groups['controlPlane']
  register: joinCmd

- name: join worker nodes to cluster
  command: "{{ joinCmd.stdout }}"
  when: inventory_hostname in groups['workerNodes']

The above fails on the second task (The task includes an option with an undefined variable. the error was: 'dict object' has no attribute 'stdout'). The standard approach seems to be save the line to a file locally, copy the file to the worker nodes and execute it locally which seems cumbersome (and leaves the join token lying around). 

How can I achieve this with variables only?

make title more relevant
Link

Ansible - declaring global variablesusing facts from task registerone host to perform actions on another host

Source Link

Ansible - declaring global variables from task register

In Ansible I'm trying to generate a variable from a task on one host and then use the variable to do something to another host. Specifically, I'm trying to create a Kubernetes cluster - On the control plane node I can save the join command into a variable, but this appears to be scoped to the source host only - I can't work out how to access that variable from another host.

- name: read join command from the controlPlane
  shell: kubeadm token create --print-join-command
  when: inventory_hostname in groups['controlPlane']
  register: joinCmd

- name: join worker nodes to cluster
  command: "{{ joinCmd.stdout }}"
  when: inventory_hostname in groups['workerNodes']

The above fails on the second task (The task includes an option with an undefined variable. the error was: 'dict object' has no attribute 'stdout'). The standard approach seems to be save the line to a file locally, copy the file to the worker nodes and execute it locally which seems cumbersome (and leaves the join token lying around). How can I achieve this with variables only?

thanks,

Jim