5.3. 转换 playbook 示例


例子

这个示例是名为 /mydata 的共享目录,我们希望可以在作业运行期间读取和写入文件。请记住,必须在执行节点上已存在,我们将用于自动化运行。

您将以 aape1.local 执行节点为目标来运行此作业,因为底层主机已经有这个问题。

[awx@aape1 ~]$ ls -la /mydata/
total 4
drwxr-xr-x.  2 awx  awx   41 Apr 28 09:27 .
dr-xr-xr-x. 19 root root 258 Apr 11 15:16 ..
-rw-r--r--.  1 awx  awx   33 Apr 11 12:34 file_read
-rw-r--r--.  1 awx  awx    0 Apr 28 09:27 file_write
Copy to Clipboard Toggle word wrap

您将使用简单的 playbook 启动自动化,并定义 sleep 来供您访问,并了解进程,并演示读取和写入文件。

# vim:ft=ansible:
Copy to Clipboard Toggle word wrap
- hosts: all
  gather_facts: false
  ignore_errors: yes
  vars:
    period: 120
    myfile: /mydata/file
  tasks:
    - name: Collect only selected facts
      ansible.builtin.setup:
        filter:
          - 'ansible_distribution'
          - 'ansible_machine_id'
          - 'ansible_memtotal_mb'
          - 'ansible_memfree_mb'
    - name: "I'm feeling real sleepy..."
      ansible.builtin.wait_for:
        timeout: "{{ period }}"
      delegate_to: localhost
    - ansible.builtin.debug:
        msg: "Isolated paths mounted into execution node: {{ AWX_ISOLATIONS_PATHS }}"
    - name: "Read pre-existing file..."
      ansible.builtin.debug:
        msg: "{{ lookup('file', '{{ myfile }}_read'
    - name: "Write to a new file..."
      ansible.builtin.copy:
        dest: "{{ myfile }}_write"
        content: |
          This is the file I've just written to.

    - name: "Read written out file..."
      ansible.builtin.debug:
        msg: "{{ lookup('file', '{{ myfile }}_write') }}"
Copy to Clipboard Toggle word wrap

在 Ansible Automation Platform 2 导航面板中,选择 Settings。然后从 Jobs 选项中选择 Job settings

公开隔离任务的路径:

[
"/mydata:/mydata:rw"
]
Copy to Clipboard Toggle word wrap

卷挂载在容器中使用相同的名称进行映射,并具有读写功能。这将在启动作业模板时使用。

prompt on launch 应该为 extra_vars 设置,以便您可以调整每次运行的 sleep 持续时间,默认为 30 秒。

启动后,会调用 wait_for 模块作为睡眠状态,您可以进入执行节点并查看正在运行的内容。

要验证运行是否已成功完成,请运行以下命令以获得作业的输出:

$ podman exec -it 'podman ps -q' /bin/bash
bash-4.4#
Copy to Clipboard Toggle word wrap

您现在位于运行的执行环境容器中。

查看权限,您会看到 awx 已变为 'root',但这并不像在超级用户中一样真正是 root 用户,因为您使用无根 Podman,这会将用户映射到与沙盒类似的内核命名空间中。了解更多有关 rootless Podman 工作方式的信息,适用于 shadow-utils。

bash-4.4# ls -la /mydata/
Total 4
drwxr-xr-x. 2 root root 41 Apr 28 09:27 .
dr-xr-xr-x. 1 root root 77 Apr 28 09:40 ..
-rw-r—--r–. 1 root root 33 Apr 11 12:34 file_read
-rw-r—--r–. 1 root root  0 Apr 28 09:27 file_write
Copy to Clipboard Toggle word wrap

根据结果,此作业会失败。为了了解原因,需要检查剩余的输出。

TASK [Read pre-existing file…]******************************* 10:50:12
ok: [localhost] =>  {
    “Msg”: “This is the file I am reading in.”

TASK {Write to a new file...}********************************* 10:50:12
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: PermissionError: [Errno 13] Permission denied: b'/mydata/.ansible_tmpazyqyqdrfile_write' -> b' /mydata/file_write'
Fatal: [localhost]: FAILED! => {"changed": false, :checksum": "9f576o85d584287a3516ee8b3385cc6f69bf9ce", "msg": "Unable to make b'/root/.ansible/tmp/anisible-tim-1651139412.9808054-40-91081834383738/source' into /mydata/file_write, failed final rename from b'/mydata/.ansible_tmpazyqyqdrfile_write': [Errno 13] Permission denied: b'/mydata/.ansible_tmpazyqyqdrfile_write' -> b'/mydata/file_write}
...ignoring

TASK [Read written out file...] ****************************** 10:50:13
Fatal: [localhost]: FAILED: => {"msg": "An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError;>, original message: could not locate file in lookup: /mydate/file_write. Vould not locate file in lookup: /mydate/file_write"}
...ignoring
Copy to Clipboard Toggle word wrap

作业失败,即使设置了 :rw,因此它应具有写入功能。进程能够读取现有的文件,但不能写出。这是因为 SELinux 保护要求将正确的标签放在挂载到容器中的卷内容上。如果缺少标签,SELinux 可能会阻止进程在容器内运行。Podman 不会更改操作系统设置的标签。如需更多信息,请参阅 Podman 文档。

这可以是常见的错误解释器。我们已将默认值设置为 :z,它会告知 Podman 在共享卷上重新标记文件对象。

因此,我们可以添加 :z 或将其关闭。

公开隔离任务的路径:

[
   "/mydata:/mydata"
]
Copy to Clipboard Toggle word wrap

playbook 现在可以正常工作:

PLAY [all] **************************************************** 11:05:52
TASK [I'm feeling real sleepy. . .] *************************** 11:05:52
ok: [localhost]
TASK [Read pre-existing file...] ****************************** 11:05:57
ok: [localhost] =>  {
	"Msg": "This is the file I'm reading in."
}
TASK [Write to a new file…] ********************************** 11:05:57
ok: [localhost]
TASK [Read written out file…] ******************************** 11:05:58
ok: [localhost] =>  {
      “Msg”: “This is the file I’ve just written to.”
Copy to Clipboard Toggle word wrap

返回到底层执行节点主机,我们有新写入的内容。

注意

如果您使用容器组在 Red Hat OpenShift 中启动自动化作业,您也可以告诉 Ansible Automation Platform 2 向那个环境公开相同的路径,但您必须在设置下将默认值切换为 On

启用后,这将将其注入用于执行的 pod 规格中的 volumeMounts。它类似如下:

apiVersion: v1
kind: Pod
Spec:
   containers:
   - image: registry.redhat.io/ansible-automation-platform-24/ee-minimal-rhel8
  args:
    - ansible runner
    - worker
    - –private-data-dir=/runner
  volumeMounts:
mountPath: /mnt2
name: volume-0
readOnly: true
mountPath: /mnt3
name: volume-1
readOnly: true
mountPath: /mnt4
name: volume-2
readOnly: true
volumes:
hostPath:
  path: /mnt2
  type: “”
name: volume-0
hostPath:
  path: /mnt3
  type: “”
name: volume-1
hostPath:
  path: /mnt4
  type: “”
name: volume-2
Copy to Clipboard Toggle word wrap

运行的容器内的存储使用覆盖文件系统。运行的容器中的任何修改都会在作业完成后销毁,就像被卸载的 tmpfs 一样。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat