【问题标题】:How to get ansible to look in the container that it's running from instead of the servers provided in the inventory如何查看运行它的容器而不是清单中提供的服务器
【发布时间】:2019-01-04 11:58:50
【问题描述】:

设置:Ansible 在 docker 容器内初始化和运行。包含剧本和库存文件的 git 存储库被加载到此容器中。

playbook 的作用:我正在编写的特定 playbook 在清单中列出的所有主机上运行一个简单的 findmnt,并将输出写入一个平面 txt 文件,然后我正在获取该文件。

实际问题:ansible 容器未在分离模式下运行,因此运行完成后,无法检索这些结果 txt 文件。由于容器中有一个 git repo 分层,我尝试将这些存储到其中,但该命令再次在库存主机上执行,而不是在运行 Ansible 的容器上执行。我尝试了多种方法来做同样的事情,但我无法找到一种方法让 ansible 在运行它的容器中执行一组操作。我该如何处理这种情况。

剧本:

---
      - 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: 'xxxx@x.com'
      # - 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"

【问题讨论】:

  • 在我们开始回答您的问题之前,您确实需要向我们展示您的剧本(开始)。一般来说,如果你有一个以localhost为目标的剧本,那意味着“Ansible 正在运行的地方”。
  • 当然,这是剧本。我不得不替换一些值以避免违规,编辑主要问题以添加剧本部分:

标签: docker rhel ansible-2.x


【解决方案1】:

您可以delegate_to: 127.0.0.1 或在每个任务的基础上使用简写local_action(参考https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html),例如:

---
- 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'

【讨论】:

  • @Funkfan82 太好了,如果它有帮助和/或如果您相信它可以帮助其他人,请不要犹豫接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 2011-01-15
  • 2012-04-14
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
相关资源
最近更新 更多