O1ahmad/ansible-role-systemd

Role fails with systemd template units

Closed this issue · 2 comments

Description

When using systemd template files, the role fails because the service unit has an unknown state. The role shouldn't try to restart or activate systemd units that are templates.

Role version: 0x0i.systemd, v0.3.4

How to reproduce:

Create a test role, put this in tasks/main.yml

---

- name: Create test systemd service
  import_role:
    name: 0x0i.systemd
  vars:
    unit_config:
      - name: "role-test@"
        path: /etc/systemd/system
        Unit:
          Description: "Role %i test"
          After: network-online.target
        Service:
          User: root
          Group: root
          ExecStart: "touch /root/tested-%i"
          Restart: on-failure
        Install:
          WantedBy: multi-user.target

In a test playbook, import the test role:

---

- hosts: all
  roles:
    - test.role

Expected result

Role applies successfully.

Current result

Role fails with message: Service is in unknown state

RUNNING HANDLER [0x0i.systemd : Reload systemd units] **************
failed: [xxx.xxx.xxx.xxx] (item=role-test@.service) => {
  "ansible_loop_var": "restart_item",
  "changed": false,
  "msg": "Service is in unknown state",
  "restart_item": {
    "ansible_loop_var": "unit_item",
    "changed": true,
    "checksum": "c6402bf9d4b29255ff507d4aaad71751c5a0011c",
    "dest": "/etc/systemd/system/role-test@.service",
    "diff": [],
    "failed": false,
    "gid": 0,
    "group": "root",
    "invocation": {
      "module_args": {
        "_original_basename": "systemd.unit.j2",
        "attributes": null,
        "backup": true,
        "checksum": "c6402bf9d4b29255ff507d4aaad71751c5a0011c",
        "content": null,
        "dest": "/etc/systemd/system/role-test@.service",
        "directory_mode": null,
        "follow": false,
        "force": true,
        "group": "root",
        "local_follow": null,
        "mode": 420,
        "owner": "root",
        "remote_src": null,
        "selevel": null,
        "serole": null,
        "setype": null,
        "seuser": null,
        "src": "/root/.ansible/tmp/ansible-tmp-1612443695.773779-216780-159766918639327/source",
        "unsafe_writes": false,
        "validate": null
      }
    },
    "md5sum": "4b8b640dd7f2ca07008889fe7e6c1b07",
    "mode": "0644",
    "owner": "root",
    "size": 318,
    "src": "/root/.ansible/tmp/ansible-tmp-1612443695.773779-216780-159766918639327/source",
    "state": "file",
    "uid": 0,
    "unit_item": {
      "Install": {
        "WantedBy": "multi-user.target"
      },
      "Service": {
        "ExecStart": "touch /root/tested-%i",
        "Group": "root",
        "Restart": "on-failure",
        "User": "root"
      },
      "Unit": {
        "After": "network-online.target",
        "Description": "Role %i test"
      },
      "name": "role-test@",
      "path": "/etc/systemd/system"
    }
  },
  "status": {}
}
...ignoring

TASK [0x0i.systemd : Activate configured Systemd units] **************
failed: [xxx.xxx.xxx.xxx] (item=role-test@.service) => {
  "ansible_loop_var": "unit_item",
  "changed": false,
  "msg": "Service is in unknown state",
  "status": {},
  "unit_item": {
    "Install": {
      "WantedBy": "multi-user.target"
    },
    "Service": {
      "ExecStart": "touch /root/tested-%i",
      "Group": "root",
      "Restart": "on-failure",
      "User": "root"
    },
    "Unit": {
      "After": "network-online.target",
      "Description": "Role %i test"
    },
    "name": "role-test@",
    "path": "/etc/systemd/system"
  }
}

Got it @joseliber - will take a look!

This works nicely, thanks for the fix! Any chance you could also release it to Ansible Galaxy? (I ran into the issue with the latest released version, only figuring out it is already fixed when I wanted to file an issue.)