# Not getting desired o/p Help required

**URL:** <https://forum.ansible.com/t/not-getting-desired-o-p-help-required/30623>\
**Category:** Ansible Project\
**Created:** [June 21, 2019, 6:56am UTC](https://forum.ansible.com/t/not-getting-desired-o-p-help-required/30623 "2019-06-21T06:56:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [June 21, 2019, 6:56am UTC](https://forum.ansible.com/t/not-getting-desired-o-p-help-required/30623/1 "2019-06-21T06:56:36Z")

</div>

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 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 21, 2019, 8:55am UTC](https://forum.ansible.com/t/not-getting-desired-o-p-help-required/30623/2 "2019-06-21T08:55:55Z")

</div>

Hii

You seem to be running ansible as root (bad practice) yet also use become (not necessary), to generate SSH keys using a shell command and using the deploy user’s own ssh\_config as some sort of inventory.

The subject of the message says “Not getting desired o/p” - so what IS the desired output (assuming that’s what o/p means)?

Or perhaps a better question: what are you trying to achieve?

Dick

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [June 21, 2019, 9:15am UTC](https://forum.ansible.com/t/not-getting-desired-o-p-help-required/30623/3 "2019-06-21T09:15:59Z")

</div>

Hi Dick,  
Thanks for the reply . My target is i want to copy the pub key to all hosts present inside the ~/.ssh/config file(viz. node1,node2,node3).  
I had tried doing it using authorized\_key module , but as I dont want to make changes in the “ansibleconfig” for disabling the stict host checking. So i had to modify the playbook using shell module.  
But the shell module says that grep is not working. I think iam making something wrong in syntax…  
Would really appreciate , if any one can help me. achieving this goal.

Changing teh subject lien as you mentioned:)

.  
I had tried to do it using On Friday, June 21, 2019 at 2:26:18 PM UTC+5:30, Dick Visser wrote:

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 21, 2019, 11:34pm UTC](https://forum.ansible.com/t/not-getting-desired-o-p-help-required/30623/4 "2019-06-21T23:34:59Z")

</div>

Hi

The code you posted seems to generate a set of (unprotected) key pairs  
in the deploy user's home directory, and then tries to manually copy  
those hosts, using a mix of shell/grep/awk.  
And you want to copy "the pub key" to all hosts that are listed in the  
deploy user's ~/ssh/config.  
This is done using root locally, and also root on the remote hosts?  
It's still not clear to me what you want to achieve? I.e. the purpose  
of this playbook. Please explain more clearly.

In any case I would strongly advise to stop using root's ~/.ssh/config  
and instead use an inventory file:  
[https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html)

Also, if your purpose is to set up passwordless authentication to a  
number of hosts, then you should use the authorized\_key module.

Dick
