Force fact-gathering on all hosts

Ansible version 2 introduced a clean, official way to do this using delegated facts (see: http://docs.ansible.com/ansible/latest/playbooks_delegation.html#delegated-facts). when: hostvars[item][‘ansible_default_ipv4′] is not defined is a check to ensure you don’t check for facts in a host you already know the facts about — # This play will still work as intended if called with –limit “<host>” or … Read more

Rendering Ansible template into the fact variable

I think you might be just looking for the template lookup plugin: – set_fact: rendered_template: “{{ lookup(‘template’, ‘./template.j2’) }}” Usage example: template.j2 Hello {{ value_for_template }} playbook.yml — – hosts: localhost gather_facts: no connection: local vars: value_for_template: world tasks: – debug: var: rendered_template vars: rendered_template: “{{ lookup(‘template’, ‘./template.j2’) }}” The result: TASK [debug] ******************************************************************* ok: … Read more

Double loop Ansible

Take a look at with_subelements in Ansible’s docs for loops. You need to create directories: Iterate though objs and create files: Here is an example: — – hosts: localhost gather_facts: no vars: objs: – { key1: value1, key2: [ value2, value3] } – { key1: value4, key2: [ value5, value6] } tasks: – name: create … Read more

How to split an ansible role’s `defaults/main.yml` file into multiple files?

The feature I’m describing below has been available since Ansible 2.6, but got a bugfix in v2.6.2 and another (minor) one in v2.7. To see a solution for older versions, see Paul’s answer. defaults/main/ Instead of creating defaults/main.yml, create a directory — defaults/main/ — and place all YAML files in there. defaults/main.yml → defaults/main/*.yml Ansible … Read more

Run an Ansible task only when the hostname contains a string

Below should do the trick: – name: Install PHP Modules command: <command> with_dict: php_modules when: “‘batch’ in inventory_hostname” Note you’ll have a couple of skipped hosts during playbook run. inventory_hostname is one of Ansible’s “magic” variables: Additionally, inventory_hostname is the name of the hostname as configured in Ansible’s inventory host file. This can be useful … Read more

Run Ansible playbook without inventory

Prerequisites. You need to have ssh server running on the host (ssh localhost should let you in). Then if you want to use password authentication (do note the trailing comma): $ ansible-playbook playbook.yml -i localhost, -k In this case you also need sshpass. In case of public key authentication: $ ansible-playbook playbook.yml -i localhost, And … Read more

How to pass variables to included tasks in ansible?

Faced the same issue in my project. It turns out that the variable name in the playbook and the task have to be different. — – name: The name hosts: all vars: my_var_play: “I need to send this value to the task” some_other_var: “This is directly accessible in task” tasks: – include:my_tasks.yml vars: my_var: “{{ … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)