arensb / ansible-truenas

Ansible collection to manage TrueNAS
Apache License 2.0
34 stars 7 forks source link

truenas.service doesn't start nor enables services #6

Closed paulo-erichsen closed 1 year ago

paulo-erichsen commented 1 year ago

setup:

for some reason, it doesn't seem to start services I have defined

this is my roles/truenas/tasks/main.yml that I'm using

---
- name: Create an ordinary user and their group
  arensb.truenas.user:
    name: testuser
    create_group: true
    sudo_commands: ALL
    password: "testpwd"

- name: Configure NFS exports
  arensb.truenas.sharing_nfs:
    enabled: true
    name: media nfs share
    path: /mnt/tank/data/media
    maproot_user: testuser
    maproot_group: testuser

- name: Enable NFSv4
  arensb.truenas.nfs:
    nfsv4: true

- name: Enable services
  arensb.truenas.service:
    name: "{{ item }}"
    enabled: true
    state: started
  with_items:
    - ssh
    - nfs

The last step is to enable ssh and nfs. SSH is already started which allows ansible to run, but it doesn't enable it. Also it doesn't seem to enable and start the nfs service.

Running ansible-playbook -vvv playbooks/truenas.yml gives me

<root@192.168.122.100> ESTABLISH SSH CONNECTION FOR USER: None
<root@192.168.122.100> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/paulo/.ansible/cp/e04ab7b04b"' root@192.168.122.100 '/bin/sh -c '"'"'chmod u+x /root/.ansible/tmp/ansible-tmp-1691781720.9827127-9421-115081788509640/ /root/.ansible/tmp/ansible-tmp-1691781720.9827127-9421-115081788509640/AnsiballZ_service.py && sleep 0'"'"''
<root@192.168.122.100> (0, b'', b'')
<root@192.168.122.100> ESTABLISH SSH CONNECTION FOR USER: None
<root@192.168.122.100> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/paulo/.ansible/cp/e04ab7b04b"' -tt root@192.168.122.100 '/bin/sh -c '"'"'/usr/local/bin/python3.9 /root/.ansible/tmp/ansible-tmp-1691781720.9827127-9421-115081788509640/AnsiballZ_service.py && sleep 0'"'"''
<root@192.168.122.100> (0, b'\r\n{"changed": false, "msg": "", "service_id": 11, "name": "ssh", "enabled": false, "state": "RUNNING", "pids": [1540], "invocation": {"module_args": {"name": "ssh", "enabled": true, "state": "started", "ha_propagate": null}}}\r\n', b'Shared connection to 192.168.122.100 closed.\r\n')
root@192.168.122.100> ESTABLISH SSH CONNECTION FOR USER: None
<root@192.168.122.100> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/paulo/.ansible/cp/e04ab7b04b"' root@192.168.122.100 '/bin/sh -c '"'"'rm -f -r /root/.ansible/tmp/ansible-tmp-1691781720.9827127-9421-115081788509640/ > /dev/null 2>&1 && sleep 0'"'"''
<root@192.168.122.100> (0, b'', b'')
ok: [root@192.168.122.100] => (item=ssh) => {
    "ansible_loop_var": "item",
    "changed": false,
    "enabled": false,
    "invocation": {
        "module_args": {
            "enabled": true,
            "ha_propagate": null,
            "name": "ssh",
            "state": "started"
        }
    },
    "item": "ssh",
    "msg": "",
    "name": "ssh",
    "pids": [
        1540
    ],
    "service_id": 11,
    "state": "RUNNING"
}
...
...
<root@192.168.122.100> ESTABLISH SSH CONNECTION FOR USER: None
<root@192.168.122.100> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/paulo/.ansible/cp/e04ab7b04b"' -tt root@192.168.122.100 '/bin/sh -c '"'"'/usr/local/bin/python3.9 /root/.ansible/tmp/ansible-tmp-1691781721.2552562-9421-21221030135877/AnsiballZ_service.py && sleep 0'"'"''
<root@192.168.122.100> (0, b'\r\n{"changed": false, "msg": "", "service_id": 9, "name": "nfs", "enabled": false, "state": "STOPPED", "pids": [], "invocation": {"module_args": {"name": "nfs", "enabled": true, "state": "started", "ha_propagate": null}}}\r\n', b'Shared connection to 192.168.122.100 closed.\r\n')
<root@192.168.122.100> ESTABLISH SSH CONNECTION FOR USER: None
<root@192.168.122.100> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/paulo/.ansible/cp/e04ab7b04b"' root@192.168.122.100 '/bin/sh -c '"'"'rm -f -r /root/.ansible/tmp/ansible-tmp-1691781721.2552562-9421-21221030135877/ > /dev/null 2>&1 && sleep 0'"'"''
<root@192.168.122.100> (0, b'', b'')
ok: [root@192.168.122.100] => (item=nfs) => {
    "ansible_loop_var": "item",
    "changed": false,
    "enabled": false,
    "invocation": {
        "module_args": {
            "enabled": true,
            "ha_propagate": null,
            "name": "nfs",
            "state": "started"
        }
    },
    "item": "nfs",
    "msg": "",
    "name": "nfs",
    "pids": [],
    "service_id": 9,
    "state": "STOPPED"
}

Notice that ansible doesn't return errors, but returns state: STOPPED when it shouldn't

image

paulo-erichsen commented 1 year ago

it looks like the code to start the service is commented out

https://github.com/arensb/ansible-truenas/blob/main/plugins/modules/service.py#L165-L167

similarly for enabling

https://github.com/arensb/ansible-truenas/blob/main/plugins/modules/service.py#L194-L202

arensb commented 1 year ago

Looks like you're right. This looks like a module that wasn't released; it escaped prematurely.

arensb commented 1 year ago

I believe this is fixed on the bugfix/6-services branch. @paulo-erichsen, can you please try it out and see if it works for you?

paulo-erichsen commented 1 year ago

sorry for taking a while to get back. I was out of town for a few days.

I've tried the updated code and it looks good with one caveat.

The NFS service is able to start, but I think it takes a second or so to turn on. I see ansible returning

changed: [root@192.168.122.100] => (item=nfs) => {
    "ansible_loop_var": "item",
    "changed": true,
    "enable_err": 9,
    "invocation": {
        "module_args": {
            "enabled": true,
            "ha_propagate": null,
            "name": "nfs",
            "state": "started"
        }
    },
    "item": "nfs",
    "msg": "service started; service enabled",
    "service_state": {
        "enabled": false,
        "id": 9,
        "name": "nfs",
        "pids": [],
        "state": "STOPPED"
    }
}

I would expect it to not show "state": "STOPPED", but it does start eventually. I guess this is fine as is

arensb commented 1 year ago

Oh! This isn't a bug, or at least not the bug you think it is: the service_state field in the return value is from the beginning of the module, when it's checking to see whether it needs to do anything, not from the end when the service has been started.

In other words, what it's showing you here is that the service was in "state": "STOPPED", which is why it decided that it needed to make a change, and start the service.

paulo-erichsen commented 1 year ago

that makes sense, thanks for the clarification!