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

Running ansible-playbook using Python API

Deprecation Notice: This post doesn’t work as of ansible 2. The API was changed. This covered in the Ansible documentation under “Python API.” For example, ansible -i hosts dbservers -m setup is implemented via: import ansible.runner runner = ansible.runner.Runner( module_name=”setup”, module_args=””, pattern=’dbservers’, ) dbservers_get_facts = runner.run() There are a bunch of non-documented parameters in the … Read more

Write variable to a file in Ansible

An important comment from tmoschou: As of Ansible 2.10, The documentation for ansible.builtin.copy says: If you need variable interpolation in copied files, use the ansible.builtin.template module. Using a variable in the content field will result in unpredictable output. For more details see this and an explanation Original answer: You could use the copy module, with … Read more