Run task only if host does not belong to a group

Here’s another way to do this: – name: my command command: echo stuff when: “‘groupname’ not in group_names” group_names is a magic variable as documented here: https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html : group_names is a list (array) of all the groups the current host is in.

Ansible: Set variable to file content

You can use lookups in Ansible in order to get the contents of a file, e.g. user_data: “{{ lookup(‘file’, user_data_file) }}” Caveat: This lookup will work with local files, not remote files. Here’s a complete example from the docs: – hosts: all vars: contents: “{{ lookup(‘file’, ‘/etc/foo.txt’) }}” tasks: – debug: msg=”the value of foo.txt … Read more

Ansible: Can I execute role from command line?

With ansible 2.7 you can do this: $ cd /path/to/ansible/ $ ansible localhost -m include_role -a name=<role_name> localhost | SUCCESS => { “changed”: false, “include_variables”: { “name”: “<role_name>” } } localhost | SUCCESS => { “msg”: “<role_name>” } This will run role from /path/to/ansible/roles or configured role path. Read more here: https://github.com/ansible/ansible/pull/43131

Specifying ssh key in ansible playbook file

The variable name you’re looking for is ansible_ssh_private_key_file. You should set it at ‘vars’ level: in the inventory file: myHost ansible_ssh_private_key_file=~/.ssh/mykey1.pem myOtherHost ansible_ssh_private_key_file=~/.ssh/mykey2.pem in the host_vars: # host_vars/myHost.yml ansible_ssh_private_key_file: ~/.ssh/mykey1.pem # host_vars/myOtherHost.yml ansible_ssh_private_key_file: ~/.ssh/mykey2.pem in a group_vars file if you use the same key for a group of hosts in the vars section of an … Read more

Ansible Playbooks vs Roles

Playbook vs Role vs [databases] and similar entries in /etc/ansible/hosts [databases] is a single name for a group of hosts. It allows you to reference multiple hosts by a single name. Role is a set of tasks and additional files to configure host to serve for a certain role. Playbook is a mapping between hosts … Read more

Ansible: get current target host’s IP address

A list of all addresses is stored in a fact ansible_all_ipv4_addresses, a default address in ansible_default_ipv4.address. — – hosts: localhost connection: local tasks: – debug: var=ansible_all_ipv4_addresses – debug: var=ansible_default_ipv4.address Then there are addresses assigned to each network interface… In such cases you can display all the facts and find the one that has the value … Read more

How to check if a file exists in Ansible?

You can first check that the destination file exists or not and then make a decision based on the output of its result: tasks: – name: Check that the somefile.conf exists stat: path: /etc/file.txt register: stat_result – name: Create the file, if it doesnt exist already file: path: /etc/file.txt state: touch when: not stat_result.stat.exists

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