尝试启动 generic/ubuntu18(20|22) 镜像并通过 ansible.builtin.user 模块在那里添加用户。一切正常,但用户并没有真正添加。为什么会这样?
Ansible:2.16.3
流浪者:2.4.3
主机操作系统:Ubuntu 24.04
config.vm.define "ubuntu18" do |ubuntu18| ubuntu18.vm.box = "generic/ubuntu1804" ubuntu18.vm.boot_timeout = 600 ubuntu18.vm.hostname = "ubuntu18" ubuntu18.vm.network :private_network, ip: "192.168.56.15" PLAYBOOKS = [ "ansible/nix-local-users.yml", ] PLAYBOOKS.each do |playbook| ubuntu18.vm.provision "ansible" do |ansible| ansible.playbook = playbook ansible.verbose = "vvv" ansible.extra_vars = { target_host: "ubuntu18" } end end end
剧本如下:
---- hosts: "{{ target_host }}" connection: local become: yes become_user: root become_method: sudo tasks: - name: Create a local nix users ansible.builtin.user: name: "{{ item.login }}" password: "*" state: present# password: "{{ item.password | password_hash('sha512') }}" create_home: yes with_items: - { login: 'LocalUser1', password: '1Passw0rd@' } - { login: 'LocalUser2', password: '2Passw0rd@' } - { login: 'LocalUser3', password: '3Passw0rd@' }
运行“vagrant up ubuntu18”时我得到了这个输出。
Bringing machine 'ubuntu18' up with 'virtualbox' provider...==> ubuntu18: Importing base box 'generic/ubuntu1804'...==> ubuntu18: Matching MAC address for NAT networking...==> ubuntu18: Setting the name of the VM: test_ubuntu18_1737979065436_47140==> ubuntu18: Fixed port collision for 22 => 2222. Now on port 2200.==> ubuntu18: Clearing any previously set network interfaces...==> ubuntu18: Preparing network interfaces based on configuration... ubuntu18: Adapter 1: nat ubuntu18: Adapter 2: hostonly==> ubuntu18: Forwarding ports... ubuntu18: 22 (guest) => 2200 (host) (adapter 1)==> ubuntu18: Running 'pre-boot' VM customizations...==> ubuntu18: Booting VM...==> ubuntu18: Waiting for machine to boot. This may take a few minutes... ubuntu18: SSH address: 127.0.0.1:2200 ubuntu18: SSH username: vagrant ubuntu18: SSH auth method: private key ubuntu18: ubuntu18: Vagrant insecure key detected. Vagrant will automatically replace ubuntu18: this with a newly generated keypair for better security. ubuntu18: ubuntu18: Inserting generated public key within guest... ubuntu18: Removing insecure key from the guest if it's present... ubuntu18: Key inserted! Disconnecting and reconnecting using new SSH key...==> ubuntu18: Machine booted and ready!==> ubuntu18: Checking for guest additions in VM... ubuntu18: The guest additions on this VM do not match the installed version of ubuntu18: VirtualBox! In most cases this is fine, but in rare cases it can ubuntu18: prevent things such as shared folders from working properly. If you see ubuntu18: shared folder errors, please make sure the guest additions within the ubuntu18: virtual machine match the version of VirtualBox you have installed on ubuntu18: your host and reload your VM. ubuntu18: ubuntu18: Guest Additions Version: 5.2.42 ubuntu18: VirtualBox Version: 7.0==> ubuntu18: Setting hostname...==> ubuntu18: Configuring and enabling network interfaces...==> ubuntu18: Running provisioner: ansible... ubuntu18: Running ansible-playbook...PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --limit="ubuntu18" --inventory-file=/test/.vagrant/provisioners/ansible/inventory --extra-vars=\{\"target_host\":\"ubuntu18\"\} -vvv ansible/nix-local-users.ymlansible-playbook [core 2.17.7] config file = /etc/ansible/ansible.cfg configured module search path = ['/home/study/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3/dist-packages/ansible ansible collection location = /home/study/.ansible/collections:/usr/share/ansible/collections executable location = /usr/bin/ansible-playbook python version = 3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0] (/usr/bin/python3) jinja version = 3.1.2 libyaml = TrueUsing /etc/ansible/ansible.cfg as config filehost_list declined parsing /test/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory as it did not pass its verify_file() methodscript declined parsing /test/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory as it did not pass its verify_file() methodauto declined parsing /test/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory as it did not pass its verify_file() methodParsed /test/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory inventory source with ini pluginSkipping callback 'default', as we already have a stdout callback.Skipping callback 'minimal', as we already have a stdout callback.Skipping callback 'oneline', as we already have a stdout callback.PLAYBOOK: nix-local-users.yml **************************************************1 plays in ansible/nix-local-users.ymlPLAY [ubuntu18] ****************************************************************TASK [Gathering Facts] *********************************************************task path: /test/ansible/nix-local-users.yml:2<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: study<127.0.0.1> EXEC /bin/sh -c 'echo ~study && sleep 0'<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/study/.ansible/tmp `"&& mkdir "` echo /home/study/.ansible/tmp/ansible-tmp-1737979102.1509817-523943-230847070365236 `" && echo ansible-tmp-1737979102.1509817-523943-230847070365236="` echo /home/study/.ansible/tmp/ansible-tmp-1737979102.1509817-523943-230847070365236 `" ) && sleep 0'<ubuntu18> Attempting python interpreter discovery<127.0.0.1> EXEC /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'python3.12'"'"'; command -v '"'"'python3.11'"'"'; command -v '"'"'python3.10'"'"'; command -v '"'"'python3.9'"'"'; command -v '"'"'python3.8'"'"'; command -v '"'"'python3.7'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'python3'"'"'; echo ENDFOUND && sleep 0'<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python3.12 && sleep 0'<ubuntu18> Python interpreter discovery fallback (unsupported Linux distribution: ubuntu)Using module file /usr/lib/python3/dist-packages/ansible/modules/setup.py<127.0.0.1> PUT /home/study/.ansible/tmp/ansible-local-5239273kodvnsp/tmpxuv0pdbw TO /home/study/.ansible/tmp/ansible-tmp-1737979102.1509817-523943-230847070365236/AnsiballZ_setup.py<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/study/.ansible/tmp/ansible-tmp-1737979102.1509817-523943-230847070365236/ /home/study/.ansible/tmp/ansible-tmp-1737979102.1509817-523943-230847070365236/AnsiballZ_setup.py && sleep 0'<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-cgihllizftbsohvxjezexndfhosuwwji ; /usr/bin/python3.12 /home/study/.ansible/tmp/ansible-tmp-1737979102.1509817-523943-230847070365236/AnsiballZ_setup.py'"'"' && sleep 0'<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/study/.ansible/tmp/ansible-tmp-1737979102.1509817-523943-230847070365236/ > /dev/null 2>&1 && sleep 0'[WARNING]: Platform linux on host ubuntu18 is using the discovered Pythoninterpreter at /usr/bin/python3.12, but future installation of another Pythoninterpreter could change the meaning of that path. Seehttps://docs.ansible.com/ansible-core/2.17/reference_appendices/interpreter_discovery.html for more information.ok: [ubuntu18]TASK [Create a local nix users] ************************************************task path: /test/ansible/nix-local-users.yml:10<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: study<127.0.0.1> EXEC /bin/sh -c 'echo ~study && sleep 0'<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/study/.ansible/tmp `"&& mkdir "` echo /home/study/.ansible/tmp/ansible-tmp-1737979102.789001-524110-46585659397742 `" && echo ansible-tmp-1737979102.789001-524110-46585659397742="` echo /home/study/.ansible/tmp/ansible-tmp-1737979102.789001-524110-46585659397742 `" ) && sleep 0'Using module file /usr/lib/python3/dist-packages/ansible/modules/user.py<127.0.0.1> PUT /home/study/.ansible/tmp/ansible-local-5239273kodvnsp/tmpv012owpg TO /home/study/.ansible/tmp/ansible-tmp-1737979102.789001-524110-46585659397742/AnsiballZ_user.py<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/study/.ansible/tmp/ansible-tmp-1737979102.789001-524110-46585659397742/ /home/study/.ansible/tmp/ansible-tmp-1737979102.789001-524110-46585659397742/AnsiballZ_user.py && sleep 0'<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-mleatpfjsqwveasahcifudyghhrkuzwq ; /usr/bin/python3.12 /home/study/.ansible/tmp/ansible-tmp-1737979102.789001-524110-46585659397742/AnsiballZ_user.py'"'"' && sleep 0'<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/study/.ansible/tmp/ansible-tmp-1737979102.789001-524110-46585659397742/ > /dev/null 2>&1 && sleep 0'ok: [ubuntu18] => (item={'login': 'LocalUser1', 'password': '1Passw0rd@'}) => { "ansible_loop_var": "item", "append": false, "changed": false, "comment": "", "group": 1002, "home": "/home/LocalUser1", "invocation": { "module_args": { "append": false, "authorization": null, "comment": null, "create_home": true, "expires": null, "force": false, "generate_ssh_key": null, "group": null, "groups": null, "hidden": null, "home": null, "local": null, "login_class": null, "move_home": false, "name": "LocalUser1", "non_unique": false, "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "password_expire_max": null, "password_expire_min": null, "password_expire_warn": null, "password_lock": null, "profile": null, "remove": false, "role": null, "seuser": null, "shell": null, "skeleton": null, "ssh_key_bits": 0, "ssh_key_comment": "ansible-generated on Dev", "ssh_key_file": null, "ssh_key_passphrase": null, "ssh_key_type": "rsa", "state": "present", "system": false, "uid": null, "umask": null, "update_password": "always" } }, "item": { "login": "LocalUser1", "password": "1Passw0rd@" }, "move_home": false, "name": "LocalUser1", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/sh", "state": "present", "uid": 1002}<127.0.0.1> EXEC /bin/sh -c 'echo ~study && sleep 0'<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/study/.ansible/tmp `"&& mkdir "` echo /home/study/.ansible/tmp/ansible-tmp-1737979102.9724927-524110-48708649402064 `" && echo ansible-tmp-1737979102.9724927-524110-48708649402064="` echo /home/study/.ansible/tmp/ansible-tmp-1737979102.9724927-524110-48708649402064 `" ) && sleep 0'Using module file /usr/lib/python3/dist-packages/ansible/modules/user.py<127.0.0.1> PUT /home/study/.ansible/tmp/ansible-local-5239273kodvnsp/tmp4tqlyxfh TO /home/study/.ansible/tmp/ansible-tmp-1737979102.9724927-524110-48708649402064/AnsiballZ_user.py<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/study/.ansible/tmp/ansible-tmp-1737979102.9724927-524110-48708649402064/ /home/study/.ansible/tmp/ansible-tmp-1737979102.9724927-524110-48708649402064/AnsiballZ_user.py && sleep 0'<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-bcaxqjzbaqhsvieolltmbltywjjyhyke ; /usr/bin/python3.12 /home/study/.ansible/tmp/ansible-tmp-1737979102.9724927-524110-48708649402064/AnsiballZ_user.py'"'"' && sleep 0'<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/study/.ansible/tmp/ansible-tmp-1737979102.9724927-524110-48708649402064/ > /dev/null 2>&1 && sleep 0'ok: [ubuntu18] => (item={'login': 'LocalUser2', 'password': '2Passw0rd@'}) => { "ansible_loop_var": "item", "append": false, "changed": false, "comment": "", "group": 1003, "home": "/home/LocalUser2", "invocation": { "module_args": { "append": false, "authorization": null, "comment": null, "create_home": true, "expires": null, "force": false, "generate_ssh_key": null, "group": null, "groups": null, "hidden": null, "home": null, "local": null, "login_class": null, "move_home": false, "name": "LocalUser2", "non_unique": false, "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "password_expire_max": null, "password_expire_min": null, "password_expire_warn": null, "password_lock": null, "profile": null, "remove": false, "role": null, "seuser": null, "shell": null, "skeleton": null, "ssh_key_bits": 0, "ssh_key_comment": "ansible-generated on Dev", "ssh_key_file": null, "ssh_key_passphrase": null, "ssh_key_type": "rsa", "state": "present", "system": false, "uid": null, "umask": null, "update_password": "always" } }, "item": { "login": "LocalUser2", "password": "2Passw0rd@" }, "move_home": false, "name": "LocalUser2", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/sh", "state": "present", "uid": 1003}<127.0.0.1> EXEC /bin/sh -c 'echo ~study && sleep 0'<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/study/.ansible/tmp `"&& mkdir "` echo /home/study/.ansible/tmp/ansible-tmp-1737979103.0961325-524110-27042937232436 `" && echo ansible-tmp-1737979103.0961325-524110-27042937232436="` echo /home/study/.ansible/tmp/ansible-tmp-1737979103.0961325-524110-27042937232436 `" ) && sleep 0'Using module file /usr/lib/python3/dist-packages/ansible/modules/user.py<127.0.0.1> PUT /home/study/.ansible/tmp/ansible-local-5239273kodvnsp/tmpk4vulkhv TO /home/study/.ansible/tmp/ansible-tmp-1737979103.0961325-524110-27042937232436/AnsiballZ_user.py<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/study/.ansible/tmp/ansible-tmp-1737979103.0961325-524110-27042937232436/ /home/study/.ansible/tmp/ansible-tmp-1737979103.0961325-524110-27042937232436/AnsiballZ_user.py && sleep 0'<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-xyazghqxggmfaljuxketsxytgkcbtwuk ; /usr/bin/python3.12 /home/study/.ansible/tmp/ansible-tmp-1737979103.0961325-524110-27042937232436/AnsiballZ_user.py'"'"' && sleep 0'<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/study/.ansible/tmp/ansible-tmp-1737979103.0961325-524110-27042937232436/ > /dev/null 2>&1 && sleep 0'ok: [ubuntu18] => (item={'login': 'LocalUser3', 'password': '3Passw0rd@'}) => { "ansible_loop_var": "item", "append": false, "changed": false, "comment": "", "group": 1004, "home": "/home/LocalUser3", "invocation": { "module_args": { "append": false, "authorization": null, "comment": null, "create_home": true, "expires": null, "force": false, "generate_ssh_key": null, "group": null, "groups": null, "hidden": null, "home": null, "local": null, "login_class": null, "move_home": false, "name": "LocalUser3", "non_unique": false, "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "password_expire_max": null, "password_expire_min": null, "password_expire_warn": null, "password_lock": null, "profile": null, "remove": false, "role": null, "seuser": null, "shell": null, "skeleton": null, "ssh_key_bits": 0, "ssh_key_comment": "ansible-generated on Dev", "ssh_key_file": null, "ssh_key_passphrase": null, "ssh_key_type": "rsa", "state": "present", "system": false, "uid": null, "umask": null, "update_password": "always" } }, "item": { "login": "LocalUser3", "password": "3Passw0rd@" }, "move_home": false, "name": "LocalUser3", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/sh", "state": "present", "uid": 1004}PLAY RECAP *********************************************************************ubuntu18 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
为什么这里的 ok=2?有 3 个用户,而不是 2 个。当我通过 ssh 访问机器时,我在 /etc/passwd、/etc/shadow 中看不到用户,也看不到新的主目录。