How do ansible host_vars work?

Since the documentation is not very specific on this topic here is the order of precedence for vars in the current version of Ansible (the first item has the highest precedence): Vars set on the command line -e foo=set_on_cmd_line Vars set in the vars_files: block in the play Vars set in the vars: block in … Read more

Ansible non-root sudo user and “become” privilege escalation

Why am I getting permission denied? Because APT requires root permissions (see the error: are you root?) and you are running the tasks as david. Per these settings: become: true become_user: david become_method: sudo Ansible becomes david using sudo method. It basically runs its Python script with sudo david in front. the user ‘david’ on … Read more

Ansible failed to transfer file to /command

do you have sftp subsystem enabled in sshd on the remote server? You can check it in /etc/sshd/sshd_config, the config file name depends on your distribution…anyway, look there for: Subsystem sftp /usr/lib/ssh/sftp-server If this line is commented-out, the sftp is disabled. To fix it, you can either enable sftp, or change Ansible configuration. I prefer … Read more

Ansible: How to call a playbook from another?

Here you have examples in official documentation. https://docs.ansible.com/ansible/2.4/playbooks_reuse_includes.html I had same error as yours after applying the aproved answer. I resolved problem by creating master playbook like this: — – import_playbook: master-okd.yml – import_playbook: infra-okd.yml – import_playbook: compute-okd.yml

Copy local file if exists, using ansible

A more comprehensive answer: If you want to check the existence of a local file before performing some task, here is the comprehensive snippet: – name: get file stat to be able to perform a check in the following task local_action: stat path=/path/to/file register: file – name: copy file if it exists copy: src=/path/to/file dest=/destination/path when: … Read more

How can I check if a string exists in a file?

It’s a tricky one. the lineinfile module is specifically intended for modifying the content of a file, but you can use it for a validation check as well. – name: find lineinfile: dest: /etc/passwd line: “user” check_mode: yes register: presence failed_when: presence.changed check_mode ensures it never updates the file. register saves the variable as noted. … Read more

How to Make Ansible variable mandatory

As Arbab Nazar mentioned, you can use {{ variable | mandatory }} (see Forcing variables to be defined) inside Ansible task. But I think it looks nicer to add this as first task, it checks is app_name, app_user and app_user_group exist: – name: ‘Check mandatory variables are defined’ assert: that: – app_name is defined – … Read more

Ansible with_items vs loop

Update: The most recent Documentation lists down the differences as below The with_ keywords rely on Lookup Plugins – even items is a lookup. The loop keyword is equivalent to with_list, and is the best choice for simple loops. The loop keyword will not accept a string as input, see Ensuring list input for loop: … Read more

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