How to set environmental variables using Ansible

Yes, there is a cleaner way. You can set environment variables per task: tasks: – shell: echo JAVA_HOME is $JAVA_HOME environment: JAVA_HOME: /usr/java/jre1.8.0_51 register: shellout – debug: var=shellout Output: TASK: [shell echo JAVA_HOME is $JAVA_HOME] ********************************** changed: [localhost] TASK: [debug var=shellout] **************************************************** ok: [localhost] => { “var”: { “shellout”: { “changed”: true, “cmd”: “echo JAVA_HOME … Read more

ansible run command on remote host in background

Simplified answer from link I mentioned in the comment: — – hosts: centos-target gather_facts: no tasks: – shell: “(cd /; python -mSimpleHTTPServer >/dev/null 2>&1 &)” async: 10 poll: 0 Note subshell parentheses. Update: actually, you should be fine without async, just don’t forget to redirect stdout: – name: start simple http server in background shell: … Read more

How to join a list of strings in Ansible?

Solution join filter works on lists, so apply it to your list: – name: Concatenate the public keys set_fact: my_joined_list: “{{ my_list | join(‘\n’) }}” Explanation While my_list in your example is a list, when you use with_items, in each iterationitem is a string. Strings are treated as lists of characters, thus join splits them. … Read more

Ansible stdout Formatting

Try this option. You’ll love it. There’s a new YAML callback plugin introduced with Ansible 2.5 — meaning any machine running Ansible 2.5.0 or later can automatically start using this format without installing custom plugins. To use it, edit your ansible.cfg file (either globally, in /etc/ansible/ansible.cfg, or locally in your playbook/project), and add the following … Read more