Run Same Ansible Playbook for Different Local Users

4.1k Views Asked by At

I would like to use an ansible playbook to setup identical configurations for two different users on my localhost (i.e., admin & brian). Currently, the "common" role installs programs that are accessible by both users. In addition, I have settings that are user specific (e.g., desktop wallpaper). When I run my playbook, the user specific settings are updated from one user but not the other. For example, if I run my playbook the wallpaper for brian is changed but the wallpaper for admin is left untouched. I am aware of become_user, but do not want to that for every task that I run. Is it possible to define the hosts file or playbook in such a way that I can simply specify the users on localhost I want the playbook to run against?

I have tried Is there anyway to run multiple Ansible playbooks as multiple users more efficiently? on the per role level but am getting the following error:

fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "/usr/bin/python2: can't open file '/home/brian/.ansible/tmp/ansible-tmp-1525409723.54-208533437554058/apt.py': [Errno 13] Permission denied\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 2}


site.yml

---
- name: ansible master playbook
  hosts: localhost
  connection: local

  roles:
    - role: common


roles/common/tasks/main.yml

---
  - import_tasks: gsettings.yml


roles/common/tasks/gsettings.yml

---
- name:  Use 12 hr. clock format  
  dconf:
    key: "/org/gnome/desktop/interface/clock-format"
    value: "'12h'"
2

There are 2 best solutions below

4
On

In Ansible you have the option to launch the playbook as:

ansible-playbook playbooks/playbook.yml --user user

Please note that specifying a user can sometime conflict with a user defined in /etc/ansible/hosts.

(From Ansible documentation)

0
On

My solution was to simply log into each user on my local machine and run my ansible playbooks locally. An underlying issue with using the dconf module to change gsettings appears to be that D-Bus for the other user is not set, so the gsettings for the other user do not stick. See related questions below.

https://askubuntu.com/questions/655238/as-root-i-can-use-su-to-make-dconf-changes-for-another-user-how-do-i-actually

http://docs.ansible.com/ansible/latest/modules/dconf_module.html

Access another user's D-Bus session