E
E
emashev2020-07-24 13:01:38
Ansible
emashev, 2020-07-24 13:01:38

How to pass the output of a command to a variable in ansible?

There is this playbook:

- name: Get the kubeadm join command from the Kubernetes master.
  command: kubeadm token create --print-join-command
  changed_when: false
  when: kubernetes_role == 'master'
  register: kubernetes_join_command_result

- name: Set the kubeadm join command globally.
  set_fact:
    kubernetes_join_HA_command: >
      {{ kubernetes_join_command_result.stdout }}
  when: kubernetes_join_command_result.stdout is defined
  delegate_to: "{{ item }}"
  delegate_facts: true
  with_items: "{{ groups['all'] }}"

- name: Join other masters to Kubernetes
  shell: >
    {{ kubernetes_join_HA_command }} --control-plane --certificate-key {{ kubernetes_crt_key }}


When deploying, this error occurs. Swears at the \n character and then the script does not see the arguments.

non-zero return code
fatal: [k8s-n4]: FAILED! => {
    "changed": true, 
    "cmd": "kubeadm join k8s:6443 --token j9maoq.giu2btxk4anc3h68     --discovery-token-ca-cert-hash sha256:3c6d6949e00b61381a230aef131196f82679f2abbaaa736b9e0347afd57bc268 \n --control-plane --certificate-key 328d137c398789dee3d2416976e5b8a3bb1f3217d634034cb1c963c1864c3e9c\n", 
    "delta": "0:00:09.025774", 
    "end": "2020-07-24 12:12:42.134381", 
    "rc": 127, 
    "start": "2020-07-24 12:12:33.108607"
}

....

STDERR:

W0724 12:58:58.581651   30901 join.go:346] [preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set.
/bin/sh: 2: --control-plane: not found


How can I get rid of \n in arguments?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
emashev, 2020-07-24
@emashev

Understood. https://github.com/lxhunter/ansible-filter-plugins...

shell: >
    {{ kubernetes_join_HA_command | trim }} --control-plane --certificate-key {{ kubernetes_crt_key }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question