I'm trying to get the IP address of one of my remote hosts by querying my 
controller machine's /etc/hosts file which happens to contain the IP 
addresses of all the servers I build with Ansible.  My Ansible playbook 
looks like this:

---
hosts: all
gather_facts: yes
become: yes


pre_tasks:
  - name: get file server's IP address
    command: "grep prod-fs1 /etc/hosts | awk '{ print $0 }'"
    register: fs_ip_addr
    delegate_to: localhost


  - debug: var={{ fs_ip_addr }}


However, I'm getting this error which I don't know how to fix:

TASK [get file server's IP address] 
*************************************************************************************************************************************************
fatal: [prod-web1.example.com -> localhost]: FAILED! => {"changed": true, 
"cmd": ["grep", "prod-fs1", "/etc/hosts", "|", "awk", "{ print $0 }"], 
"delta": "0:00:00.010303", "end": "2020-03-03 12:24:36.207656", "msg": 
"non-zero return code", "rc": 2, "start": "2020-03-03 12:24:36.197353", 
"stderr": "grep: |: No such file or directory\ngrep: awk: No such file or 
directory\ngrep: { print $0 }: No such file or directory", "stderr_lines": 
["grep: |: No such file or directory", "grep: awk: No such file or 
directory", "grep: { print $0 }: No such file or directory"], "stdout": 
"/etc/hosts:45.79.93.135    prod-fs1.example.com    prod-fs1", 
"stdout_lines": ["/etc/hosts:45.79.93.135    prod-fs1.example.com   
 prod-fs1"]}


PLAY RECAP 
**************************************************************************************************************************************************************************
prod-web1.example.com : ok=7    changed=0    unreachable=0    failed=1   
 skipped=0    rescued=0    ignored=0;p

It appears that the error is caused by the pipe symbol which is why I 
enclosed the entire command in double quotes.  But the error occurs even if 
I remove them.

I should add that I know I could use this method of getting the IP address 
of my file server...

   - name: get file server's IP address
     set_fact: fs_ip_addr=hostvars[groups['fileservers'][0]]['ansible_eth0
']['ipv4']['address']

However, this option isn't available to me since I have to call the 
playbook with the "limit" argument which cause the variable to not be 
available.

deploy-webservers.yml -I inventory -l webservers

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/087403db-1994-47be-ac20-f22e131361c7%40googlegroups.com.

Reply via email to