I need to create a workflow where I have a “host” device where it has a key value of “SN”
I need to use passed playbook “extra var” input , and using that set fact SN that I can use in later tasks. Seems simple just reuturn key / value from hosts.. but turns out it is not working. I have tried many ways.
command:
ansible-playbook -i inventories/production/hosts.yaml --limit triton tabletop-deploy.yaml --user ansible -e tabletop_number=“$tabletop_number”
tasks:
- name: Search ansible hosts and set fact based on SN
set_fact:
sn_value: "{{ hostvars[item].SN }}"
loop: "{{ groups['arduino_boards'] | list }}"
when: item == tabletop_number
register: sn_result
- name: Print SN value
debug:
msg: "SN value: {{ sn_result.results.ansible_facts.sn_value }}"
Example hosts.yaml
all:
children:
pi_servers:
hosts:
triton:
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
# ssh_pub_key: # ssh_key_pub in SCM
ssid: penguinpages
wifi_password: $PASSWORD # Set in SCM
baclava:
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
arduino_boards:
hosts:
t01:
SN: 24EC4A2365A4
ip_address: 172.16.100.121
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
t02:
SN: 48CA435CF294
ip_address: 172.16.100.122
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
t03:
SN: 1234567
ip_address: 172.16.100.123
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
I would expect output to be if input was “t02” then SN fact set would be SN=48CA435CF294
But I have tried many many ways and keep getting back bad results.
Error:
<triton> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="ansible"' -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o 'ControlPath="/root/.ansible/cp/0901649b71"' triton '/bin/sh -c '"'"'rm -f -r /home/ansible/.ansible/tmp/ansible-tmp-1745329422.0644264-70-103872123862905/ > /dev/null 2>&1 && sleep 0'"'"''
<triton> (0, b'', b"OpenSSH_9.9p2, OpenSSL 3.3.3 11 Feb 2025\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 22: include /etc/ssh/ssh_config.d/*.conf matched no files\r\ndebug3: expanded UserKnownHostsFile '~/.ssh/known_hosts' -> '/root/.ssh/known_hosts'\r\ndebug3: expanded UserKnownHostsFile '~/.ssh/known_hosts2' -> '/root/.ssh/known_hosts2'\r\ndebug1: auto-mux: Trying existing master at '/root/.ansible/cp/0901649b71'\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 58\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\ndebug3: mux_client_read_packet_timeout: read header failed: Broken pipe\r\ndebug2: Received exit status from master 0\r\n")
ok: [triton]
TASK [serial-scan : Set Ansible facts for shell variables] *********************
task path: /builds/penguinpages/infra/shuffleboard/roles/serial-scan/tasks/main.yaml:1
ok: [triton] => {
"ansible_facts": {
"tabletop_number": "t01"
},
"changed": false
}
TASK [serial-scan : Output Ansible facts] **************************************
task path: /builds/penguinpages/infra/shuffleboard/roles/serial-scan/tasks/main.yaml:9
ok: [triton] => {
"msg": [
"tabletop_number: t01"
]
}
TASK [serial-scan : Search ansible hosts and set fact based on SN] *************
task path: /builds/penguinpages/infra/shuffleboard/roles/serial-scan/tasks/main.yaml:18
ok: [triton] => (item=t01) => {
"ansible_facts": {
"sn_value": "24EC4A2365A4"
},
"ansible_loop_var": "item",
"changed": false,
"item": "t01"
}
skipping: [triton] => (item=t02) => {
"ansible_loop_var": "item",
"changed": false,
"false_condition": "item == tabletop_number",
"item": "t02",
"skip_reason": "Conditional result was False"
}
skipping: [triton] => (item=t03) => {
"ansible_loop_var": "item",
"changed": false,
"false_condition": "item == tabletop_number",
"item": "t03",
"skip_reason": "Conditional result was False"
}
<snip>
}
TASK [serial-scan : Print SN value] ********************************************
task path: /builds/penguinpages/infra/shuffleboard/roles/serial-scan/tasks/main.yaml:25
fatal: [triton]: FAILED! => {
"msg": "The task includes an option with an undefined variable.. 'list object' has no attribute 'ansible_facts'\n\nThe error appears to be in '/builds/penguinpages/infra/shuffleboard/roles/serial-scan/tasks/main.yaml': line 25, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Print SN value\n ^ here\n"
}
PLAY RECAP *********************************************************************
triton : ok=4 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Its like it loops through “hosts” and finds the value "“sn_value”: “24EC4A2365A4” but never can return it as a fact or usable. I have tried dozens of means to get this done and nothing can end in "input = “hostname” and return a sub fact under that host of field “SN” set as a fact.