Funkfan82
Funkfan82

Reputation: 13

How to get ansible to look in the container that it's running from instead of the servers provided in the inventory

Setup: Ansible initializes and runs from inside a docker container. A git repo which contains the playbooks and inventory files is loaded into this container.

What the playbook does: The specific playbook that I'm working on runs a simple findmnt on all of the hosts listed in the inventory, and writes the output to a flat txt file, which I'm then fetching.

Actual Issue: The ansible container isn't running in the detached mode and so when the run is done, there's no way to retrieve these result txt files. Since there is a git repo layered into the container, I tried stashing these into it, but the command again executes on the inventory-hosts instead of the container from where Ansible is running. I've tried multiple methods to do the same, but am unable to find a way to have ansible do a set of operations within the container that it is running from. How do I handle this situation.

Playbook:

---
      - name: get the nfs mounts reports 
        hosts: 2017_CI
        become: true
        vars :
        nfs_results: "/tmp/{{ host_name }}.txt"
        host_name: "{{ inventory_hostname }}"


        tasks:
           - name: "get the list of nfs mounts on {{ host_name }} "
           #shell: 'findmnt -lo source,target,fstype,label,options,used -t  nfs'
           #AIX  nfsstat -m
           shell: 'mount -l -t nfs'
           register: nfs_output
           failed_when: "'FAILED' in nfs_output.stderr"    

           - name: Store the nfs report ouput file  
             copy:
             content: "{{ nfs_output.stdout }}\n"
             dest: "{{ nfs_results }}"
             owner: root
             group: root
             mode: 0777
             force: yes
             register: nfs_results 

             - name:  Fetching the output to ansible host
               fetch:
                src: "/tmp/{{ inventory_hostname }}.txt"
               dest: "/tmp/"
               flat: yes
             - pause:
               minutes: 2


             - name:  copying file with  permissions
               copy:
                src: "/tmp/{{ inventory_hostname }}.txt"
               dest: "/data/web/nfsmountInfo/"
              owner: root
              group: root
               mode: 0777 




          # - name: Transfer file from ServerA to ServerB
                 #   synchronize:
      #     src: "/tmp/{{ inventory_hostname }}.txt"
      #     dest: "/data/web/nfsmountInfo/"
      #     mode: push
      #   delegate_to: "localhost"
      #   become: yes
      #   become_user: root


      # - pause:
      #     minutes: 1

      # - name: git configuration fo email setup
      #   git_config:
      #     name: user.email
      #     scope: global
      #     value: '[email protected]'
      # - name: git configuration fo email setup
      #   git_config:
      #     name: user.name
      #     scope: global
      #     value: 'myUser'

      # - name: Add the files into staging workspace
      #   shell: git add .
      #   args:
      #       chdir: '/home/jenkins/workspace/TestPipelines/NFSTestAnsible/nfsmountInfo/'

      # - name: Commit the changes
      #   shell: git commit -m "Update the nfsmount reports"
      #   args:
      #       chdir: '/home/jenkins/workspace/TestSaddamPipelines/NFSTestAnsible/nfsmountInfo/'

      # - name: Set origin to include username and password.
      #   shell: "git remote set-url origin https://user@http://<gitServer>/inf-build-ansible.git"
      # - name: Push to origin.
      #   shell: "git push origin nfs-mnt-testing"

Upvotes: 0

Views: 791

Answers (1)

masseyb
masseyb

Reputation: 4132

You can delegate_to: 127.0.0.1 or use the shorthand local_action on a per task-basis (ref. https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html) for example:

---
- hosts: all
  tasks:
  - name: run task on inventory hosts
    debug: 
      msg: 'this task will execute on all hosts'

  - name: delegate task to 127.0.0.1
    debug: 
      msg: 'this task is delegated to the ansible control server'
    delegate_to: 127.0.0.1

  - name: use the local_action syntax for delegation
    local_action:
      module: debug
      msg: 'executing on the control server'

Upvotes: 1

Related Questions