{"id":5567,"date":"2018-10-31T12:57:38","date_gmt":"2018-10-31T12:57:38","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=5567"},"modified":"2021-11-17T09:38:33","modified_gmt":"2021-11-17T09:38:33","slug":"how-to-access-ansible-remote-machine-using-ssh-user-and-key","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-access-ansible-remote-machine-using-ssh-user-and-key\/","title":{"rendered":"How to access Ansible remote machine using SSH user and key?"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5588\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/10\/How-to-access-Ansible.png\" alt=\"\" width=\"600\" height=\"315\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/10\/How-to-access-Ansible.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/10\/How-to-access-Ansible-300x158.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p><strong>How to access Ansible remote machine using SSH user and key?<\/strong><\/p>\n<p>There are following ways, you can autheticte linux remote server using Ansible.<\/p>\n<p><strong>Method 1 &#8211; Password Less authentication<\/strong><br>\nhttps:\/\/www.tecmint.com\/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps\/<\/p>\n<p>If You want to automate this process using Ansible in mutiple machine, follow this<br>\nhttps:\/\/everythingshouldbevirtual.com\/ansible-ssh-key-distribution-for-password-less-ssh\/<\/p>\n<p><strong>Method 2 &#8211; SSH key file using command line<\/strong><\/p>\n<p>[code]ansible-playbook -i hosts playbook.yml &#8211;key-file=remote-access.pem -u ec2-user &#8211;become[\/code]<\/p>\n<p><strong>Method 3 &#8211; SSH key file in the inventory file:<\/strong><\/p>\n<p>[code]myHost1 ansible_ssh_private_key_file=remote-access.pem ansible_user=ec2-user<br>\nmyHost2 ansible_ssh_private_key_file=remote-access.pem ansible_user=ec2-user[\/code]<\/p>\n<p><strong>Method 4 &#8211; SSH key file in the host_vars:<\/strong><\/p>\n<p>[code]# hosts_vars\/myHost1.yml<br>\nansible_ssh_private_key_file: ~\/.ssh\/mykey1.pem<br>\nansible_user=ec2-user<br>\n# hosts_vars\/myHost2.yml<br>\nansible_ssh_private_key_file: ~\/.ssh\/mykey2.pem<br>\nansible_user=ec2-user[\/code]<\/p>\n<p><strong>Method 5 &#8211; SSH key file in the group_vars:<\/strong><\/p>\n<p>[code]# hosts_vars\/myHost1.yml<br>\nansible_ssh_private_key_file: ~\/.ssh\/mykey1.pem<br>\nansible_user=ec2-user<br>\n# hosts_vars\/myHost2.yml<br>\nansible_ssh_private_key_file: ~\/.ssh\/mykey2.pem<br>\nansible_user=ec2-user[\/code]<\/p>\n<p><strong>Method 6 &#8211; SSH key file in the vars section of your play:<\/strong><\/p>\n<p>[code]- hosts: myHost<br>\nremote_user: ubuntu<br>\nvars_files:<br>\n&#8211; vars.yml<br>\nvars:<br>\nansible_ssh_private_key_file: &#8220;{{ key1 }}&#8221;<br>\ntasks:<br>\n&#8211; name: Echo a hello message<br>\ncommand: echo hello[\/code]<\/p>\n<p><strong>Method 7 &#8211; SSH key file in the ansible.cfg:<\/strong><\/p>\n<p>[code][defaults]<br>\ninventory: &lt;YOUR INVENTORY&gt;<br>\nremote_user = &lt;YOUR USER&gt;<br>\nprivate_key_file = &lt;PATH TO KEY_FILE&gt;[\/code]<\/p>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_12424\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_33778\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_12424\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>How to access Ansible remote machine using SSH user and key? There are following ways, you can autheticte linux remote server using Ansible. Method 1 &#8211; Password Less authentication https:\/\/www.tecmint.com\/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps\/ If You want to automate this process using Ansible in mutiple machine, follow this https:\/\/everythingshouldbevirtual.com\/ansible-ssh-key-distribution-for-password-less-ssh\/ Method 2 &#8211; SSH key file using command line [code]ansible-playbook&#8230;<\/p>\n","protected":false},"author":1,"featured_media":5588,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5038],"tags":[639,5122,5123,4665,2235,2239,534,383],"class_list":["post-5567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","tag-ansible","tag-ansible_ssh_private_key_file","tag-ansible_user","tag-authentication","tag-less","tag-machine","tag-password","tag-remote"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=5567"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5567\/revisions"}],"predecessor-version":[{"id":25564,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5567\/revisions\/25564"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/5588"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=5567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=5567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=5567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}