Using register with a loop in Ansible - linux

i want to code a playbook which IF a user exists changes the pw of it.
The playbook should be able to take n User's and change the pw of those Users.
At the moment im having the issue that the when is empty due to the loop, i tried using with_items: {{ user_exists.results }} but this is somehow not working.
(http://docs.ansible.com/ansible/playbooks_loops.html#using-register-with-a-loop)
Am i doing something wrong ?
Br,
Numblesix
---
-
become: true
become_method: sudo
hosts: xetest
name: "Updates the password of given User if exists"
tasks:
-
ignore_errors: true
name: "Check if User exists"
register: user_exists
shell: "grep -q {{ item.key }} /etc/passwd &>/dev/null"
with_dict: "{{ users }}"
-
debug:
var: user_exists
-
debug:
msg: "User name is {{ item.key }} and hash is {{ item.value.passwd}} and return code is: "
with_dict: "{{ users }}"
-
debug:
var: user_exists
with_items: "{{user_exists.results }}"
-
name: "updating password for given User"
user: "name={{ item.key }} update_password=always password={{ item.value.passwd}} createhome=no"
when: user_exists.rc == 0
with_dict: "{{ users }}"
with_items: "{{ user_exists.results }}"
vars:
users:
foo:
passwd: $6$random_salt$12A.ar9eNDsgmds3leKoCDZPmq7OHLvhBtQg/Q3K2G/3yeEa/r8Ou4DxJpN6vzccewugvZt7IkfCbHFF2i.QU.
RESULTS IN ERROR!
duplicate loop in task: items
WITHOUT with_items: "{{ user_exists.results }}" im getting this error
"failed": true, "msg": "The conditional check 'user_exists.rc == 0' failed.
The error was: error while evaluating conditional (user_exists.rc == 0):
'dict object' has no attribute 'rc'

For my testing, I'm using ansible 2.1.4.0.
When running the script, you can see in the debug for user_exists.results that it contains the input value passed in along with the return code:
"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "grep -q foo /etc/passwd",
"delta": "0:00:00.009034",
"end": "2017-05-02 17:42:57.835871",
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "grep -q foo /etc/passwd",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": {
"key": "foo",
"value": {
"passwd": "foobar"
}
},
"rc": 1,
"start": "2017-05-02 17:42:57.826837",
"stderr": "",
"stdout": "",
"stdout_lines": [],
"warnings": []
},
So instead doing two loops (which would have been done with with_nested and two lists), you can do everything with a single loop:
- name: "updating password for given User"
debug:
msg: "name={{ item.item.key }} update_password=always password={{ item.item.value.passwd}} createhome=no"
when: item.rc == 0
with_items: "{{ user_exists.results }}"
Note: In my testing shell: "grep -q {{ item.key }} /etc/passwd &>/dev/null" was always returning a 0 return code. I had to remove the "&>/dev/null" part to get the proper return code.

Related

How to pass data from text file to ansible playbook?

I want to have text file which contains name and password
name: "Peter", "Joe", "Mark"
password: "smith", "biden", "garyy"
And I have playbook like this
---
- hosts: myhosts
become: yes
remote_user: root1
become_user: root
vars_files:
- vars.yml
vars:
ansible_ssh_private_key_file: "{{key}}"
tasks:
- name: Create users
user: name="{{item.name}}" shell=/bin/bash home="/srv/{{item.name}}" groups=root generate_ssh_key=yes ssh_key_bits=2048
loop: "{{ lookup('file', 'userspasswd.txt', wantList=True)| list }}"
- name: Set password to users
shell: echo "{{item.name}}:{{item.password}}" | sudo chpasswd
no_log: True
loop: "{{ lookup('file', 'userspasswd.txt', wantList=True)| list }}"
I am getting error like this
fatal: [xxx.xxx.xxx.xxx]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'name'\n\nThe error appears to be in '/home/root1/Documents/ansiblekernel/main.yml': line 12, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Create users\n ^ here\n"}
Is there any correct way of doing this? Cause I am new to this?
Given the file
shell> cat userspasswd.txt
name: "Peter", "Joe", "Mark"
password: "smith", "biden", "garyy"
Neither wantList=True nor the filter list will help you to parse the file because it's not a valid YAML. If you can't change the structure of the file you'll have to parse it on your own.
Declare the variables
userspasswd_lines: "{{ lookup('file', 'userspasswd.txt').splitlines() }}"
userspasswd_values: "{{ userspasswd_lines|
map('split', ':')|
map('last')|
map('regex_replace', '\"', '')|
map('split', ',')|
map('map', 'trim')|
list }}"
userspasswd_dict: "{{ dict(userspasswd_values.0|
zip(userspasswd_values.1)) }}"
give
userspasswd_lines:
- 'name: "Peter", "Joe", "Mark"'
- 'password: "smith", "biden", "garyy"'
userspasswd_values:
- - Peter
- Joe
- Mark
- - smith
- biden
- garyy
userspasswd_dict:
Joe: biden
Mark: garyy
Peter: smith
Iterate the dictionary. Test it
- name: Create users
debug:
msg: |
name: {{ item }}
shell: /bin/bash
home: /srv/{{ item }}
groups: root
generate_ssh_key: yes
ssh_key_bits: 2048
loop: "{{ userspasswd_dict.keys()|list }}"
- name: Set password to users
debug:
msg: 'echo "{{ item.key }}:{{ item.value}}" | sudo chpasswd'
loop: "{{ userspasswd_dict|dict2items }}"
gives
TASK [Create users] **************************************************************************
ok: [test_11] => (item=Peter) =>
msg: |-
name: Peter
shell: /bin/bash
home: /srv/Peter
groups: root
generate_ssh_key: yes
ssh_key_bits: 2048
ok: [test_11] => (item=Joe) =>
msg: |-
name: Joe
shell: /bin/bash
home: /srv/Joe
groups: root
generate_ssh_key: yes
ssh_key_bits: 2048
ok: [test_11] => (item=Mark) =>
msg: |-
name: Mark
shell: /bin/bash
home: /srv/Mark
groups: root
generate_ssh_key: yes
ssh_key_bits: 2048
TASK [Set password to users] *****************************************************************
ok: [test_11] => (item={'key': 'Peter', 'value': 'smith'}) =>
msg: echo "Peter:smith" | sudo chpasswd
ok: [test_11] => (item={'key': 'Joe', 'value': 'biden'}) =>
msg: echo "Joe:biden" | sudo chpasswd
ok: [test_11] => (item={'key': 'Mark', 'value': 'garyy'}) =>
msg: echo "Mark:garyy" | sudo chpasswd
Example of a complete playbook for testing
- hosts: myhosts
vars:
userspasswd_lines: "{{ lookup('file', 'userspasswd.txt').splitlines() }}"
userspasswd_values: "{{ userspasswd_lines|
map('split', ':')|
map('last')|
map('regex_replace', '\"', '')|
map('split', ',')|
map('map', 'trim')|
list }}"
userspasswd_dict: "{{ dict(userspasswd_values.0|
zip(userspasswd_values.1)) }}"
tasks:
- block:
- debug:
var: userspasswd_lines
- debug:
var: userspasswd_values
- debug:
var: userspasswd_dict
run_once: true
- name: Create users
debug:
msg: |
name: {{ item }}
shell: /bin/bash
home: /srv/{{ item }}
groups: root
generate_ssh_key: yes
ssh_key_bits: 2048
loop: "{{ userspasswd_dict.keys()|list }}"
- name: Set password to users
debug:
msg: 'echo "{{ item.key }}:{{ item.value}}" | sudo chpasswd'
loop: "{{ userspasswd_dict|dict2items }}"- hosts: myhosts
vars:
userspasswd_lines: "{{ lookup('file', 'userspasswd.txt').splitlines() }}"
userspasswd_values: "{{ userspasswd_lines|
map('split', ':')|
map('last')|
map('regex_replace', '\"', '')|
map('split', ',')|
map('map', 'trim')|
list }}"
userspasswd_dict: "{{ dict(userspasswd_values.0|
zip(userspasswd_values.1)) }}"
tasks:
- block:
- debug:
var: userspasswd_lines
- debug:
var: userspasswd_values
- debug:
var: userspasswd_dict
run_once: true
- name: Create users
debug:
msg: |
name: {{ item }}
shell: /bin/bash
home: /srv/{{ item }}
groups: root
generate_ssh_key: yes
ssh_key_bits: 2048
loop: "{{ userspasswd_dict.keys()|list }}"
- name: Set password to users
debug:
msg: 'echo "{{ item.key }}:{{ item.value}}" | sudo chpasswd'
loop: "{{ userspasswd_dict|dict2items }}"

Running the following 2 bash commands in Ansible [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
shell module: Bash <(process substitution) with ansible
(3 answers)
Closed 6 months ago.
I'm trying to create a playbook that incorporates the following 2 commands and due to their special characters, i cant get it to work. Is there a list of special characters i need to account for in ansible using a bash command?
- name: If file exists compare new users.txt
shell: >
diff -q users.txt <(getent passwd | awk -F: '{ print $1 }') 1>/dev/null; error="$?"
getent passwd | awk -F: '{ print $1 }' > users.txt
The error is
fatal: [localhost]: FAILED! => {
"changed": true,
"cmd": "diff -q users.txt <(getent passwd | awk -F: '{ print $1}') 1>/dev/null; error=\"$?\"\n",
"delta": "0:00:00.002893",
"end": "2022-08-19 23:43:07.324939",
"invocation": {
"module_args": {
"_raw_params": "diff -q users.txt <(getent passwd | awk -F: '{ print $1}') 1>/dev/null; error=\"$?\"\n",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"msg": "non-zero return code",
"rc": 1,
"start": "2022-08-19 23:43:07.322046",
"stderr": "/bin/sh: -c: line 0: syntax error near unexpected token `('\n/bin/sh: -c: line 0: `diff -q users.txt <(getent passwd | awk -F: '{ print $1}') 1>/dev/null; error=\"$?\"'",
"stderr_lines": [
"/bin/sh: -c: line 0: syntax error near unexpected token `('",
"/bin/sh: -c: line 0: `diff -q users.txt <(getent passwd | awk -F: '{ print $1}') 1>/dev/null; error=\"$?\"'"
],
"stdout": "",
"stdout_lines": []
}
Q: "Update the list of users in /tmp/users.txt if the list differs from those in /etc/passwd."
A: Use the module getent to read /etc/passwd. For example,
- name: Create dictionary ansible_facts.getent_passwd
ansible.builtin.getent:
database: passed
Next, read the file. Set changed_when: false to keep the play idempotent
- name: Create list users.stdout_lines from /tmp/users.txt
command: cat /tmp/users.txt
register: users
changed_when: false
Compare the lists and update the file if needed
- copy:
dest: /tmp/users.txt
content: |
{% for user in ansible_facts.getent_passwd.keys() %}
{{ user }}
{% endfor %}
when: diff|length > 0
vars:
diff: ansible_facts.getent_passwd.keys()|list|
difference(users.stdout_lines)
Example of a complete playbook for testing
- hosts: localhost
tasks:
- name: Create dictionary ansible_facts.getent_passwd
ansible.builtin.getent:
database: passwd
- ansible.builtin.debug:
var: ansible_facts.getent_passwd.keys()|list
when: debug|d(false)|bool
- name: Create list users.stdout_lines from /tmp/users.txt
command: cat /tmp/users.txt
register: users
changed_when: false
- ansible.builtin.debug:
var: users.stdout_lines
when: debug|d(false)|bool
- name: Update /tmp/users.txt
copy:
dest: /tmp/users.txt
content: |
{% for user in ansible_facts.getent_passwd.keys() %}
{{ user }}
{% endfor %}
when: new_users|length > 0
vars:
new_users: "{{ ansible_facts.getent_passwd.keys()|
difference(users.stdout_lines) }}"
Example of the update
If I remove the users admin, user1, and user2 from the file, the play gives (abridged) in --diff mode
shell> ansible-playbook pb.yml -CD
TASK [Update /tmp/users.txt] ************************************************
--- before: /tmp/users.txt
+++ after: /home/tester/.ansible/tmp/ansible-local-903563m7yp2ywc/tmpvm5fd7j2
## -50,4 +50,7 ##
libvirt-qemu
libvirt-dnsmasq
asadmin
+admin
+user1
+user2
_chrony
changed: [localhost]
Q: "Detect new users that were added."
A: Report new users in the block. For example,
- hosts: localhost
gather_facts: false
tasks:
- name: Create dictionary ansible_facts.getent_passwd
ansible.builtin.getent:
database: passwd
- ansible.builtin.debug:
var: ansible_facts.getent_passwd.keys()|list
when: debug|d(false)|bool
- name: Create list users.stdout_lines from /tmp/users.txt
command: cat /tmp/users.txt
register: users
changed_when: false
- ansible.builtin.debug:
var: users.stdout_lines
when: debug|d(false)|bool
- name: Report new users and update file
block:
- name: Report new users
debug:
msg: "New users: {{ new_users }}"
- name: Update /tmp/users.txt
copy:
dest: /tmp/users.txt
content: |
{% for user in ansible_facts.getent_passwd.keys() %}
{{ user }}
{% endfor %}
when: new_users|length > 0
vars:
new_users: "{{ ansible_facts.getent_passwd.keys()|
difference(users.stdout_lines) }}"

How to filter out a particular line from ansible output

I am facing an issue while running my ansible-playbook to filter out the IP address from the playbook output. Here is my code
- hosts: myhost
tasks:
- name: Gather all VMs from a specific folder
community.vmware.vmware_vm_info:
hostname: myhost.com
username: local
password: mypass
folder: "/VMFStorage/vm/"
validate_certs: False
delegate_to: localhost
register: vm_info
- debug:
var: vm_info
The output of the code
ok: [myhost] => {
"vm_info": {
"changed": false,
"failed": false,
"virtual_machines": [
{
"attributes": {},
"cluster": "Storage",
"datacenter": "VMFStorage",
"esxi_hostname": "161.92.211.24",
"folder": "/VMFStorage/vm/",
"guest_fullname": "Red Hat Enterprise Linux 8 (64-bit)",
"guest_name": "RHEL Machine",
"ip_address": "192.168.1.47",
"mac_address": [
"00:50:56:a3:b1:e9"
],
"moid": "vm-22949",
"power_state": "poweredOn",
"tags": [],
"uuid": "4223f3fa-eeae-6a96-6af5-d68dc94199f3",
"vm_network": {
"00:50:56:a3:b1:e9": {
"ipv4": [
"192.168.1.47"
],
"ipv6": [
"fe80::250:56ff:fea3:b1e9"
]
}
}
},
{
"attributes": {},
"cluster": "Storage",
"datacenter": "VMFStorage",
"esxi_hostname": "161.92.211.22",
"folder": "/VMFStorage/vm/",
"guest_fullname": "Red Hat Enterprise Linux 8 (64-bit)",
"guest_name": "Machine2",
"ip_address": "192.168.1.29",
"mac_address": [
"00:50:56:a3:b3:6d"
],
"moid": "vm-21360",
"power_state": "poweredOff",
"tags": [],
"uuid": "42239cd1-69ec-ff5d-a557-85d0bc8c9edc",
"vm_network": {
"00:50:56:a3:b3:6d": {
"ipv4": [],
"ipv6": []
}
}
},
The required output is to catch the IP address of Machine1 guest name and print that output
192.168.1.29
I tried many possibilities but couldn't find out the proper solution
Any help would be appreciated
UPDATE
- hosts: myhost
tasks:
- name: Gather all VMs from a specific folder
community.vmware.vmware_vm_info:
hostname: myhost.com
username: local
password: mypass
folder: "/VMFStorage/vm/"
validate_certs: False
delegate_to: localhost
register: vm_info
- set_fact:
ip: "{{ vm_info.virtual_machines|
selectattr('guest_name', 'eq', 'AlmaLinuxBUILD-Machine')|
map(attribute='ip_address')|first }}"
- debug:
var: ip
There are many options. For example,
Select the dictionary from the list first and extract the attribute
- debug:
var: ip
vars:
ip: "{{ vm_info.virtual_machines|
selectattr('guest_name', 'eq', 'Machine2')|
map(attribute='ip_address')|first }}"
gives
ip: 192.168.1.29
If you prefer json_query the task below gives the same result
- debug:
var: ip
vars:
ip: "{{ vm_info.virtual_machines|
json_query('[?guest_name==`Machine2`].ip_address')|first }}"
Create a dictionary first if there are many items on the list that should be referenced, e.g.
- debug:
var: name_ip
vars:
name_ip: "{{ vm_info.virtual_machines|
items2dict(key_name='guest_name',
value_name='ip_address') }}"
gives
name_ip:
Machine2: 192.168.1.29
RHEL Machine: 192.168.1.47
Then, use this dictionary to get the IP of a guest. The task below also gives the same result
- debug:
var: ip
vars:
ip: "{{ name_ip['Machine2'] }}"
Q: "How can I use the ip variable in other places in the same playbook?"
A: There are many options. For example, put it into the set_facts (precedence 19)
- set_facts:
ip: "{{ name_ip['Machine2'] }}"
- debug:
var: ip
try this.. if you only want machine1 ip_address.
- debug:
msg: "{{ vm_info.virtual_machines[1].ip_address }}"
try this.. if you only want all VMs ip_addresses.
- debug:
msg: "{{ vm_info.virtual_machines[*].ip_address }}"

filtering the ansible playbook output [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have output of an ansible playbook for which i need to apply fileters in such a way to proceed with next tasks. Please find the below output.
ok: [localhost] => {
"results": [
{
"actions": {
"namespaces": {},
"state": "present",
"xpath": "/Storage/SSL/KeyStorePath"
},
"ansible_loop_var": "item",
"changed": false,
"pod": 1,
"failed": false,
"invocation": {
"module_args": {
"mount": true,
"input_type": "yaml",
}
},
"item": "100.108.22.102",
"msg": "found 1 nodes"
},
{
"actions": {
"namespaces": {},
"state": "present",
"xpath": "/Storage/SSL/KeyStorePath"
},
"ansible_loop_var": "item",
"changed": false,
"pod": 0,
"failed": false,
"invocation": {
"module_args": {
"mount": true,
"input_type": "yaml",
}
},
"item": "100.108.22.103",
"msg": "found 0 nodes"
}
]
}
Here, i want the next task to be executed when the node respective pod value is 1 if the pod value of the node is 0 then the next task should not run on the respective node ip.
Please assist ..
Q: "Execute next task when the node's respective pod value is 1"
A: There are many ways how to proceed with the next task. For example, given the variable results
Use delegate_to and loop selected items
- hosts: localhost
tasks:
- debug:
msg: 'Task is running on {{ item.item }}'
loop: "{{ results|selectattr('pod', 'eq', 1)|list }}"
loop_control:
label: "{{ item.item }}"
delegate_to: "{{ item.item }}"
gives (abridged)
TASK [debug] ****
ok: [localhost -> 100.108.22.102] => (item=100.108.22.102) =>
msg: Task is running on 100.108.22.102
Create a list of selected hosts and test the host is in the list
- hosts: 100.108.22.102,100.108.22.103
tasks:
- debug:
msg: "Task is running on {{ inventory_hostname }}"
when: inventory_hostname in pods
vars:
pods: "{{ results|
selectattr('pod', 'eq', 1)|
map(attribute='item')|
list }}"
gives (abridged)
TASK [debug] ****
ok: [100.108.22.102] =>
msg: Task is running on 100.108.22.102
skipping: [100.108.22.103]
Use add_host to create an inventory group in the first play and use it in the second one
- hosts: localhost
tasks:
- add_host:
name: '{{ item }}'
groups: pods_enabled
loop: "{{ results|
selectattr('pod', 'eq', 1)|
map(attribute='item')|
list }}"
- hosts: pods_enabled
tasks:
- debug:
msg: "Task is running on {{ inventory_hostname }}"
gives (abridged)
PLAY [localhost] ****
TASK [add_host] ****
changed: [localhost] => (item=100.108.22.102)
PLAY [pods_enabled] ****
TASK [debug] ****
ok: [100.108.22.102] =>
msg: Task is running on 100.108.22.102
PLAY RECAP ****
100.108.22.102: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost: ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
You could use with_items and when in your next task as below
- name: validate pod value
debug:
msg: "The pod value is 1"
with_items: "{{ your_var.results }}"
when: item.pod == "1"

Ansible changes variable values when set

I have a series of variables set. Call them Hosts and Inthosts. Each has an appropriate value set, as seen in the debug output. When I try to assign the value of inthosts to hosts, it does not actually make them the same, it bolloxes it up changing the double quotes to single quotes and putting a "u" in front of each "item". It there a way to force Ansible to actually do a literal equals in this case without parsing the text? The text should just be treated as a string. In this case the "modified" value is being output to a file, and the change breaks things.
The plan was to use the default hosts, and override it with inthosts if the server in question should be using a different set of servers.
Default Variables Set
filebeat_kafka_hosts: '["x.compute-1.amazonaws.com:9093", "y.compute-1.amazonaws.com:9093"]'
filebeat_kafka_inthosts: '["x.compute-1.amazonaws.com:9093", "y.compute-1.amazonaws.com:9093", "z.compute-1.amazonaws.com:9093"]'
Ansible Code
- debug:
msg: "Hosts {{ filebeat_kafka_hosts }} "
- debug:
msg: "IntHosts {{ filebeat_kafka_inthosts }} "
- set_fact:
filebeat_kafka_hosts="{{ filebeat_kafka_inthosts }}"
- debug:
msg: "Inthosts -> hosts {{ filebeat_kafka_hosts }} "
Output (edited)
"msg": "Hosts [\"x.compute-1.amazonaws.com:9093\", \"y.compute-1.amazonaws.com:9093\"] " |
"msg": "IntHosts [\"x.compute-1.amazonaws.com:9093\", \"y.compute-1.amazonaws.com:9093\", \"z.compute-1.amazonaws.com:9093\"] "
set {"ansible_facts": {"filebeat_kafka_hosts": ["x.compute-1.amazonaws.com:9093", "y.compute-1.amazonaws.com:9093", "z.compute-1.amazonaws.com:9093"]}, "changed": false}
"msg": "Inthosts -> hosts [u'x.compute-1.amazonaws.com:9093', u'y.compute-1.amazonaws.com:9093', u'z.compute-1.amazonaws.com:9093'] "
Ansible is interpreting filebeat_kafka_inthosts and filebeat_kafka_hosts as lists. That gives you the 'u' characters before each item in your debug. The tasks below
- debug:
msg: "{{ item }}"
with_items: "{{ filebeat_kafka_hosts }}"
- debug:
msg: "{{ item }}"
with_items: "{{ filebeat_kafka_inthosts }}"
Would give you
TASK [debug] *******************************************************************
ok: [127.0.0.1] => (item=y.compute-1.amazonaws.com:9093) => {
"item": "y.compute-1.amazonaws.com:9093",
"msg": "y.compute-1.amazonaws.com:9093"
}
ok: [127.0.0.1] => (item=x.compute-1.amazonaws.com:9093) => {
"item": "x.compute-1.amazonaws.com:9093",
"msg": "x.compute-1.amazonaws.com:9093"
}
TASK [debug] *******************************************************************
ok: [127.0.0.1] => (item=x.compute-1.amazonaws.com:9093) => {
"item": "x.compute-1.amazonaws.com:9093",
"msg": "x.compute-1.amazonaws.com:9093"
}
ok: [127.0.0.1] => (item=y.compute-1.amazonaws.com:9093) => {
"item": "y.compute-1.amazonaws.com:9093",
"msg": "y.compute-1.amazonaws.com:9093"
}
ok: [127.0.0.1] => (item=z.compute-1.amazonaws.com:9093) => {
"item": "z.compute-1.amazonaws.com:9093",
"msg": "z.compute-1.amazonaws.com:9093"
}
Since your writing this line to a file, you shouldn't have to worry about it too much. The 'u' character is a side effect of the debug module. Writing the variable to a file would give the same result (although with single quotes instead of double).
- lineinfile:
path: some_file
line: "{{ filebeat_kafka_hosts }}"
some_file
['x.compute-1.amazonaws.com:9093', 'y.compute-1.amazonaws.com:9093', 'z.compute-1.amazonaws.com:9093']
If you really need double quotes, you can use the to_json filter
- lineinfile:
path: some_file
line: "{{ filebeat_kafka_hosts | to_json }}"
some_file
["x.compute-1.amazonaws.com:9093", "y.compute-1.amazonaws.com:9093", "z.compute-1.amazonaws.com:9093"]
Seems that casting the variable as a string also works. Thanks for the advice!

Resources