Ansible writing to a file

222 views
Skip to first unread message

Chris Bidwell - NOAA Federal

unread,
Mar 3, 2021, 6:17:56 PM3/3/21
to ansible...@googlegroups.com
Hi all,

So I'm trying to write to a local file from my ansible server based off information I've gotten from my inventory hosts.  For some reason, it keeps creating the file ON the inventory host instead of my ansible server.  Here is my playbook:

---
- hosts: ALL_LINUX
  become: yes
  vars_files:
    - vars.yml
 
  vars:
    - SVR: "rhel.svr"
    - WKSTN: "rhel.wkstn"

  tasks:
    - name: Make sure files exist
      file:
        path: "{{ item }}"
        state: touch
      with_items:
        - "{{ SVR }}"
        - "{{ WKSTN }}"

    - name: Zero out files
      shell: cat /dev/null > "{{ item }}"
      with_items:
        - "{{ SVR }}"
        - "{{ WKSTN }}"
      delegate_to: localhost
       
    - name: Workstation or Server?
      shell: cat /etc/redhat-release | awk '{print $5}'
      register: rh_type
      tags: name

    - name: "Find rhel servers"                  
      lineinfile:                
        dest: "{{ SVR }}"        
        line: "{{ inventory_hostname }}"
        create: yes              
        insertafter: EOF          
      delegate_to: localhost      
      when: rh_type.stdout == "Server"
      with_items: "{{ ansible_play_hosts }}"

    - name: "Find rhel workstations"                  
      lineinfile:                
        dest: "{{ WKSTN }}"        
        line: "{{ inventory_hostname }}"
        create: yes              
        insertafter: EOF          
      delegate_to: localhost      
      when: rh_type.stdout == "Workstation"
      with_items: "{{ ansible_play_hosts }}"

Rajthecomputerguy

unread,
Mar 4, 2021, 12:38:29 AM3/4/21
to ansible...@googlegroups.com
looks like you have missed " delegate_to: localhost" in first task.

--
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/CAHKi8CgrQ9KZoT1ViiPBuAr7UrwuUQ54DZc%3DDSnbZq3-Q7M%2B4Q%40mail.gmail.com.


--

Thanks,

Pushparaj G


Chris Bidwell

unread,
Mar 9, 2021, 6:28:09 PM3/9/21
to Ansible Project
Thanks for the update, however, it looks like it's not using the sudoer I'm wanting it to use.  I have become: yes up top which I would think would run as the sudoer user I'm intending.  

If I'm trying to run this playbook in my homedir which is where all my others are initiated from and added become: yes and knowing that the sudoer user is listed in my ansible.cfg file, it doesn't seem to be running as that.

Somasundaram R

unread,
Mar 10, 2021, 11:36:51 AM3/10/21
to Ansible Project
Have you included become_method: sudo ?
Reply all
Reply to author
Forward
0 new messages