如何让 ansible 查看它来自 运行 的容器而不是清单中提供的服务器
How to get ansible to look in the container that it's running from instead of the servers provided in the inventory
设置:Ansible 从 docker 容器中初始化和 运行s。包含剧本和清单文件的 git 存储库已加载到此容器中。
剧本的作用:我正在研究的具体剧本 运行 是对清单中列出的所有主机的简单查找,并将输出写入一个平面 txt 文件,我m 然后取。
实际问题:ansible 容器未 运行 处于分离模式,因此当 运行 完成后,无法检索这些结果 txt 文件。由于有一个 git 存储库分层到容器中,我尝试将它们存储到其中,但命令再次在库存主机上执行,而不是 Ansible 运行ning 所在的容器。我已经尝试了多种方法来做同样的事情,但我无法找到一种方法来让 ansible 在它来自 运行ning 的容器中执行一组操作。我该如何处理这种情况。
剧本:
---
- 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"
您可以 delegate_to: 127.0.0.1
或在每个任务的基础上使用 shorthand 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'
设置:Ansible 从 docker 容器中初始化和 运行s。包含剧本和清单文件的 git 存储库已加载到此容器中。
剧本的作用:我正在研究的具体剧本 运行 是对清单中列出的所有主机的简单查找,并将输出写入一个平面 txt 文件,我m 然后取。
实际问题:ansible 容器未 运行 处于分离模式,因此当 运行 完成后,无法检索这些结果 txt 文件。由于有一个 git 存储库分层到容器中,我尝试将它们存储到其中,但命令再次在库存主机上执行,而不是 Ansible 运行ning 所在的容器。我已经尝试了多种方法来做同样的事情,但我无法找到一种方法来让 ansible 在它来自 运行ning 的容器中执行一组操作。我该如何处理这种情况。
剧本:
---
- 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"
您可以 delegate_to: 127.0.0.1
或在每个任务的基础上使用 shorthand 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'