How to switch a user per task or set of tasks?

With Ansible 1.9 or later Ansible uses the become, become_user, and become_method directives to achieve privilege escalation. You can apply them to an entire play or playbook, set them in an included playbook, or set them for a particular task. – name: checkout repo git: repo= version=master dest={{ dst }} become: yes become_user: some_user You … Read more

Specify sudo password for Ansible

The docs strongly recommend against setting the sudo password in plaintext: As a reminder passwords should never be stored in plain text. For information on encrypting your passwords and other secrets with Ansible Vault, see Encrypting content with Ansible Vault. Instead you should be using –ask-become-pass on the command line when running ansible-playbook Previous versions … Read more

Not possible to source .bashrc with Ansible

You have two options to use source with ansible. One is with the “shell:” command and /bin/sh (the ansible default). “source” is called “.” in /bin/sh. So your command would be: – name: source bashrc sudo: no shell: . /home/username/.bashrc && [the actual command you want run] Note you have to run a command after … Read more

How to loop over this dictionary in Ansible?

Hows this – hosts: localhost vars: war_files: server1: – file1.war – file2.war server2: – file1.war – file2.war – file3.war tasks: – name: Loop over subelements of the dictionary debug: msg: “Key={{ item.0.key }} value={{ item.1 }}” loop: “{{ war_files | dict2items | subelements(‘value’) }}” dict2items, subelements filters are coming in Ansible 2.6. FYI, if a … Read more

Only check whether a line present in a file (ansible)

Use check_mode, register and failed_when in concert. This fails the task if the lineinfile module would make any changes to the file being checked. Check_mode ensures nothing will change even if it otherwise would. – name: “Ensure /tmp/my.conf contains ‘’” lineinfile: name: /tmp/my.conf line: “” state: present check_mode: yes register: conf failed_when: (conf is changed) … Read more