How to get IP address from controller's /etc/hosts file?

749 views
Skip to first unread message

Robert F

unread,
Mar 3, 2020, 3:38:38 PM3/3/20
to Ansible Project
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

Robert F

unread,
Mar 3, 2020, 3:42:44 PM3/3/20
to Ansible Project
Note that my awk command should say "print $1" to get the IP address.  I don't see a way to edit my original question here to make this change.

Jean-Yves LENHOF

unread,
Mar 3, 2020, 3:54:05 PM3/3/20
to ansible...@googlegroups.com

Hi,

Not read everything but first thing is that if you have a pipe in a command call.... you should use shell module instead... That should fix your first issue.

Regards,


JYL

--
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-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/21a85286-91fb-404c-a9d5-f30608be1c3d%40googlegroups.com.

Juerg Ritter

unread,
Mar 3, 2020, 4:32:24 PM3/3/20
to ansible...@googlegroups.com
Hi Robert,

Try using the shell module instead of the command module. The command module doesn't execute a command in a shell, hence you cannot use shell functionality such as a pipes and redirections.

Hope that helps!

--Juerg

--
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-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/21a85286-91fb-404c-a9d5-f30608be1c3d%40googlegroups.com.


--

Jürg Ritter, RHCE

Consultant

Red Hat Switzerland

Europaallee 41

CH-8021 Zürich

jri...@redhat.com
M: +41-(0)79-715-09-90

Robert F

unread,
Mar 3, 2020, 4:47:00 PM3/3/20
to Ansible Project
I neglected to mention that I did try the shell and it did not fix the problem.  But thanks for the suggestion!

Dick Visser

unread,
Mar 3, 2020, 6:42:24 PM3/3/20
to ansible...@googlegroups.com
Hii

If you

1. Properly format your playbook to be a list of plays
2. Use shell
3. Don't use {{ }} around the var in your debug task

Then it works:

TASK [get file server's IP address] ****************************************************************
changed: [villa -> localhost]

TASK [debug] ***************************************************************************************
ok: [villa] =>
  fs_ip_addr:
    changed: true
    cmd: grep prod-fs1 /etc/hosts | awk '{ print $0 }'
    delta: '0:00:00.008973'
    end: '2020-03-04 00:36:05.873228'
    failed: false
    rc: 0
    start: '2020-03-04 00:36:05.864255'
    stderr: ''
    stderr_lines: []
    stdout: 10.10.10.20 prod-fs1
    stdout_lines:
    - 10.10.10.20 prod-fs1

But I would also recommend:

4. Dropping privilege escalation for local actions
5. Use "print $1" as this will match the actual IP
6. Use 'stdout' sa this will be the actual IP

Thus the playbook would be:

---
- hosts: all

  gather_facts: yes
  become: yes

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

    - debug: var=fs_ip_addr.stdout


And then output would be:


TASK [debug] ***********************************************************************************************
ok: [villa] =>
  fs_ip_addr.stdout: 10.10.10.20



Although shell+grep+awk still sounds fragile


Dick







--
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-proje...@googlegroups.com.


--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Robert F

unread,
Mar 3, 2020, 11:50:02 PM3/3/20
to Ansible Project
Dick,

Thank you very much!  Your solution worked.  I realized after I posted my question that I should have said "print $1" but I didn't see a way to edit my question.  I had also forgotten that when you do "debug: var=..." that you have to drop the double braces; they're only used when you do "debug: msg=...".  Removing privilege escalation and using stdout were new to me.  Thanks again.
Reply all
Reply to author
Forward
0 new messages