{"id":1286,"date":"2024-04-27T16:51:50","date_gmt":"2024-04-27T16:51:50","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1286"},"modified":"2024-04-28T12:16:53","modified_gmt":"2024-04-28T12:16:53","slug":"ansible-user-management","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1286","title":{"rendered":"Ansible: User management &#8211; Add user &#8211; ssh key"},"content":{"rendered":"<p>In this section we will try to understand few of the operations we can do using User module.<\/p>\n<p>In the below example, we will create anew user named shiju in the managed node, and will try to see the users secondary group and lilst content of .ssh folder:<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat adduser_playbook.yml<\/p>\n<p><em>==== ===<\/em><\/p>\n<p><em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Playbook to add a new user<\/em><br \/>\n<em> &nbsp; &nbsp; hosts: 192.168.48.129<\/em><br \/>\n<em> &nbsp; &nbsp; vars:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; theName: shiju<\/em><br \/>\n<em> &nbsp; &nbsp; tasks:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Task to add a new user<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.user:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: &#8220;{{ theName }}&#8221;<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; groups: developers<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; append: true<\/em><\/p>\n<p><em>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Tast to check information in user home folder<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.command:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd: &#8220;ls -A \/home\/{{ theName }}&#8221;<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; register: results<\/em><\/p>\n<p><em>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Display the listing of home directory<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.debug:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var: results.stdout<\/em><\/p>\n<p><em>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Create sn SSH key for user {{ theName }}<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.user:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: &#8220;{{ theName }}&#8221;<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; generate_ssh_key: yes<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ssh_key_bits: 2048<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ssh_key_file: .ssh\/id_rsa<\/em><\/p>\n<p><em>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Tast again to check information in user home folder<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.command:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd: &#8220;ls -A \/home\/{{ theName }}&#8221;<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; register: newResults<\/em><\/p>\n<p><em>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Display the listing of home directory to see if ssh folder was created<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.debug:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var: newResults.stdout<\/em><\/p>\n<p>===== ====<br \/>\n<strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout adduser_playbook.yml<\/p>\n<p>===== ===<\/p>\n<p><em>PLAY [Playbook to add a new user] **********************************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Task to add a new user] **************************************************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Tast to check information in user home folder] ***************************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Display the listing of home directory] ***********************************<\/em><br \/>\n<em>ok: [192.168.48.129] =&gt; {<\/em><br \/>\n<em> &#8220;results.stdout&#8221;: &#8220;.bash_logout\\n.bash_profile\\n.bashrc&#8221;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><em>TASK [Create sn SSH key for user shiju] ****************************************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Tast again to check information in user home folder] *********************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Display the listing of home directory to see if ssh folder was created] ***<\/em><br \/>\n<em>ok: [192.168.48.129] =&gt; {<\/em><br \/>\n<em> &#8220;newResults.stdout&#8221;: &#8220;.bash_logout\\n.bash_profile\\n.bashrc\\n<strong>.ssh<\/strong>&#8220;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><\/p>\n<p>===== ===<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this section we will try to understand few of the operations we can do using User module. In the below example, we will create <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1286\" title=\"Ansible: User management &#8211; Add user &#8211; ssh key\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,21,3],"tags":[],"class_list":["post-1286","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1286"}],"version-history":[{"count":8,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1286\/revisions"}],"predecessor-version":[{"id":1302,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1286\/revisions\/1302"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1286"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}