I pass a multiline variable dest_host
from Jenkins to Ansible as below
ansible-playbook -i allmwhosts.hosts action.yml -e '{ dest_host: myhost1
myhost2 }' --tags validate
In ansible i wish to count the number of lines present in dest_host
which in this case is 2.
I can think of command: "cat {{ dest_host }} | wc -l"
register the output and then print as a solution. However, is these a better way to get this in Ansible rather than going for a unix command ?
That is what the | length
filter is for
- debug:
msg: '{{ dest_host | length }}'
vars:
dest_host: "alpha\nbeta\n"
although be forewarned that your -e
does not do what you think it does (about the lines) because of yaml's scalar folding
ansible -e '{ bob:
alpha
beta
}' -m debug -a var=bob -c local -i localhost, localhost
emits
"bob": "alpha beta"
but the | length
can still help you by using | split | length