I have a config file :
[ansible-master .ssh]# cat ~/.ssh/config
Host *
StrictHostKeyChecking no
RAKA
Host node1
Hostname 10.160.0.7
IdentityFile ~/.ssh/RAKA
Host node2
Hostname 10.128.0.11
IdentityFile ~/.ssh/RAKA
Host node3
Hostname 10.160.0.8
IdentityFile ~/.ssh/RAKA
[root@ansible-master .ssh]#
My Playbook is —
- hosts: localhost
become: true
become_user: root
vars:
SITEID: RAKA
Configfile: ‘~/.ssh/config’
ssh_key_location: ‘~/.ssh/“{{ SITEID }}”.pub’
ssh_key_create_path: ‘~/.ssh/“{{ SITEID }}”’
vars_prompt: - name: “some_password”
prompt: “Enter Password”
private: yes
tasks:
-
stat:
path: “{{ ssh_key_location }}”
register: sshkey_result -
name: If SSH key Present then Ignore teh creation of SSH
delegate_to: localhost
when: sshkey_result.stat.exists == true
debug:
msg: “SSH keys already present. No need for creation” -
name: Generating a new SSH key for the current user if it Does’nt exists already
delegate_to: localhost
command: ssh-keygen -q -b 2048 -t rsa -N “” -f “{{ ssh_key_create_path }}”
changed_when: False
when: sshkey_result.stat.exists == false -
name: Deploy teh Key generated on teh required machines
shell: grep ‘Host’ “{{ SITEID }}” “{{ Configfile }}” |awk '{print $2}'
raw: grep ‘Host’ “{{ SITEID }}” “{{ Configfile }}”
register: desired_hosts
- debug:
var: desired_hosts.stdout_lines
command: ssh-copy-id -i “{{ ssh_key_location }}” “{{ desired_hosts }}”
While running i am getting an error:
TASK [debug] ********************************************************************************************************************************************************************************
ok: [localhost] => {
“desired_hosts.stdout_lines”:
}
TASK [Deploy] *******************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {“changed”: true, “cmd”: [“ssh-copy-id”, “-i”, “~/.ssh/RAKA.pub”, “{‘stderr_lines’: [u’grep: RAKA: No such file or directory’, u’grep: ~/.ssh/config: No such file or directory’], u’changed’: True, u’end’: u’2019-06-21 06:54:43.497017’, ‘failed’: False, u’stdout’: u’‘, u’cmd’: u’grep \‘Host\’ RAKA ~/.ssh/config |awk \\'{print $2}\\'
‘, u’rc’: 0, u’start’: u’2019-06-21 06:54:43.488159’, u’stderr’: u’grep: RAKA: No such file or directory\ngrep: ~/.ssh/config: No such file or directory’, u’delta’: u’0:00:00.008858’, ‘stdout_lines’: }”], “delta”: “0:00:00.069500”, “end”: “2019-06-21 06:54:43.798178”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2019-06-21 06:54:43.728678”, “stderr”: “/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/RAKA.pub"\n/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed\n\n/usr/bin/ssh-copy-id: ERROR: ssh: Could not resolve hostname {‘stderr_lines’: [u’grep: raka: no such file or directory’, u’grep: ~/.ssh/config: no such file or d: Name or service not known”, “stderr_lines”: [“/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/RAKA.pub"”, “/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed”, “”, “/usr/bin/ssh-copy-id: ERROR: ssh: Could not resolve hostname {‘stderr_lines’: [u’grep: raka: no such file or directory’, u’grep: ~/.ssh/config: no such file or d: Name or service not known”], “stdout”: “”, “stdout_lines”: }
PLAY RECAP *****************