Public bug reported:

lsb_release -rd
Description:    Ubuntu 18.04 LTS
Release:        18.04

I have a cloud-init file that creates a user and then tries to write a
file in the user's directory:

#cloud-config

# Hostname management
preserve_hostname: False
hostname: pspdr
fqdn: pspdr.local

users:
 - name: student
   home: /home/student
   shell: /bin/bash
   primary-group: student
   groups: [ admin, sudoers ]
   sudo: ALL=(ALL) NOPASSWD:ALL

write_files:
-   permissions: '0644'
    content: |
        export LC_ALL=C
        LOCALPY=$(python3 -m site --user-base)
        [ -z \"$LOCALPY\" ] || export PATH=$LOCALPY/bin:$PATH
    path: /home/student/.bash_aliases
    owner: student:student

But the .bash_aliases file is created for root:root because it is
created before the user. Relevant parts in the cloud-init boot log are:

2018-07-13 07:13:54,784 - util.py[DEBUG]: Writing to 
/home/student/.bash_aliases - wb: [644] 109 bytes
2018-07-13 07:13:54,785 - handlers.py[DEBUG]: finish: 
init-network/config-write-files: FAIL: running config-write-files with 
frequency once-per-instance
2018-07-13 07:13:54,785 - util.py[WARNING]: Running module write-files (<module 
'cloudinit.config.cc_write_files' from 
'/usr/lib/python3/dist-packages/cloudinit/config/cc_write_files.py'>) failed
2018-07-13 07:13:54,785 - util.py[WARNING]: Running module write-files (<module 
'cloudinit.config.cc_write_files' from 
'/usr/lib/python3/dist-packages/cloudinit/config/cc_write_files.py'>) failed
2018-07-13 07:13:54,785 - util.py[DEBUG]: Running module write-files (<module 
'cloudinit.config.cc_write_files' from 
'/usr/lib/python3/dist-packages/cloudinit/config/cc_write_files.py'>) failed
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/cloudinit/util.py", line 1360, in 
chownbyname
    uid = pwd.getpwnam(user).pw_uid
KeyError: 'getpwnam(): name not found: student'

and

2018-07-13 07:13:56,177 - stages.py[DEBUG]: Running module users-groups 
(<module 'cloudinit.config.cc_users_groups' from 
'/usr/lib/python3/dist-packages/cloudinit/config/cc_users_groups.py'>) with 
frequency once-per-instance
2018-07-13 07:13:56,178 - handlers.py[DEBUG]: start: 
init-network/config-users-groups: running config-users-groups with frequency 
once-per-instance
2018-07-13 07:13:56,178 - util.py[DEBUG]: Writing to 
/var/lib/cloud/instances/pspdr/sem/config_users_groups - wb: [644] 24 bytes
2018-07-13 07:13:56,180 - helpers.py[DEBUG]: Running config-users-groups using 
lock (<FileLock using file 
'/var/lib/cloud/instances/pspdr/sem/config_users_groups'>)
2018-07-13 07:13:56,181 - util.py[DEBUG]: Reading from /etc/os-release 
(quiet=True)
2018-07-13 07:13:56,181 - util.py[DEBUG]: Read 382 bytes from /etc/os-release
2018-07-13 07:13:56,183 - util.py[DEBUG]: Reading from 
/etc/system-image/channel.ini (quiet=True)
2018-07-13 07:13:56,183 - util.py[DEBUG]: Read 0 bytes from 
/etc/system-image/channel.ini
2018-07-13 07:13:56,184 - util.py[DEBUG]: Reading from /etc/os-release 
(quiet=True)
2018-07-13 07:13:56,184 - util.py[DEBUG]: Read 382 bytes from /etc/os-release
2018-07-13 07:13:56,186 - util.py[DEBUG]: Reading from 
/etc/system-image/channel.ini (quiet=True)
2018-07-13 07:13:56,186 - util.py[DEBUG]: Read 0 bytes from 
/etc/system-image/channel.ini
2018-07-13 07:13:56,186 - util.py[DEBUG]: Running command ['groupadd', 
'sudoers'] with allowed return codes [0] (shell=False, capture=True)
2018-07-13 07:13:56,462 - __init__.py[INFO]: Created new group sudoers
2018-07-13 07:13:56,463 - __init__.py[DEBUG]: created group 'sudoers' for user 
'student'
2018-07-13 07:13:56,463 - util.py[DEBUG]: Reading from /etc/os-release 
(quiet=True)
2018-07-13 07:13:56,463 - util.py[DEBUG]: Read 382 bytes from /etc/os-release
2018-07-13 07:13:56,464 - util.py[DEBUG]: Reading from 
/etc/system-image/channel.ini (quiet=True)
2018-07-13 07:13:56,464 - util.py[DEBUG]: Read 0 bytes from 
/etc/system-image/channel.ini
2018-07-13 07:13:56,464 - util.py[DEBUG]: Running command ['groupadd', 
'student'] with allowed return codes [0] (shell=False, capture=True)
2018-07-13 07:13:56,701 - __init__.py[INFO]: Created new group student
2018-07-13 07:13:56,702 - __init__.py[DEBUG]: created group 'student' for user 
'student'
2018-07-13 07:13:56,703 - __init__.py[DEBUG]: Adding user student

IMHO the execution order should be reversed

** Affects: cloud-init (Ubuntu)
     Importance: Undecided
         Status: New

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/1781549

Title:
  cloud-init: users should be created before write-files

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/cloud-init/+bug/1781549/+subscriptions

-- 
ubuntu-bugs mailing list
ubuntu-bugs@lists.ubuntu.com
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to