Ansible - Write multiple line output to file

Viewed 25863

I am using ansible to gather information from remote nodes and will then use this information to update relevant RPMs.

The issue I am having is collection version number of various applications and writing them to a file.

Playbook:

---
  - name: Check Application Versions
    hosts: kubernetes
    tasks:
       - name: Check K8S version.
         shell: kubectl --version
         register: k8s_version

       - debug: msg="{{ k8s_version.stdout }}"

Inventory file:

[kubernetes]
172.29.219.102 
172.29.219.105 
172.29.219.104 
172.29.219.103 

Output:

TASK [debug] *******************************************************************
ok: [172.29.219.102] => {
    "msg": "Kubernetes v1.4.0"
}
ok: [172.29.219.103] => {
    "msg": "Kubernetes v1.4.0"
}
ok: [172.29.219.105] => {
    "msg": "Kubernetes v1.4.0"
}
ok: [172.29.219.104] => {
    "msg": "Kubernetes v1.4.0"
}

The above portion is simple and works. Now I want to write the output to file.

Now im trying to write this information to a file.I want something like:

Kubernetes v1.4.0 Kubernetes v1.4.0 Kubernetes v1.4.0 Kubernetes v1.4.0

So I added the below line:

- local_action: copy content={{ k8s_version.stdout_lines }} dest=/tmp/test

My /tmp/test looks like :

# cat /tmp/test 
["Kubernetes v1.4.0"]

There is only one value here.

I tried to do something different then.

- local_action: lineinfile dest=/tmp/foo line="{{ k8s_version.stdout }}" insertafter=EOF

This resulted in:

# cat  /tmp/foo 
Kubernetes v1.4.0

Im trying to figure out why I only see one value whereas I should see the versions of every node in my inventory file. What am I doing wrong?

2 Answers
Related