V
V
Valery Ryaboshapko2015-12-14 04:47:18
Ansible
Valery Ryaboshapko, 2015-12-14 04:47:18

How to mark all tasks in playbook to run as superuser?

Greetings.
I am writing my first playbook for Ansible. The task is simple: make sure that the current transmission-daemon is installed, feed it the config assembled from the template, reload it and make sure that it is successfully launched.
The question is how to mark that all tasks in the script must be executed on behalf of the superuser. Now I add become and become_pass keys to each task:

- name: ensure transmission-daemon is up to date
      become: true
      become_pass: "{{ become_pass }}"
      apt:
        name: transmission-daemon
        state: latest
    - name: write transmission config and reload transmission-daemon
      become: true
      become_pass: "{{ become_pass }}"
      template:
        src: configs/transmission.j2
        dest: /etc/transmission-daemon/settings.json

For my taste, this is wrong, because it violates the DRY principle in every possible way. How to get rid of this repetition? At least from password repetition.
The second question is, is there a more kosher way to fix the config file? After all, it's just JSON, and Python can work wonderfully with JSON. Is it possible not to collect the entire config from the template, but to load it on the spot, replace two or three variables and write it back? In order not to overwrite the default values.
Well, since such a dance has begun, the third question. When overwriting a config from a template, the value of the changed variable becomes true, even if there were no actual changes. How can I make the file be overwritten (and the changed variable changed) only if the file changes? I want to make the daemon reboot only if the config is actually changed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Erokhin, 2016-07-04
@erohin_d

---
- hosts: web-301-okt
remote_user: root

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question