跳至内容

运行您的 EE

您可以使用 ansible-navigator 在命令行中针对 localhost 或远程目标运行您的 EE。

除了 ansible-navigator 之外,您还可以使用其他工具运行 EE。

针对 localhost 运行

  1. 创建一个 test_localhost.yml 剧本。

    cat > test_localhost.yml<<EOF
    - name: Gather and print local facts
      hosts: localhost
      become: true
      gather_facts: true
      tasks:
    
      - name: Print facts
        ansible.builtin.debug:
          var: ansible_facts
    EOF
    
  2. postgresql_ee EE 中运行剧本。

    ansible-navigator run test_localhost.yml --execution-environment-image postgresql_ee --mode stdout --pull-policy missing --container-options='--user=0'
    

您可能会注意到收集的事实是关于容器的,而不是开发人员机器的。这是因为 ansible 剧本是在容器内运行的。

针对远程目标运行

在开始之前,请确保您拥有以下内容

  • 至少一个远程目标的 IP 地址或可解析的主机名。
  • 远程主机的有效凭据。
  • 远程主机上具有 sudo 权限的用户。

postgresql_ee EE 中针对远程主机执行剧本,如下例所示

  1. 为清单文件创建一个目录。

    mkdir inventory
    
  2. inventory 目录中创建 hosts.yml 清单文件。

    cat > inventory/hosts.yml<<EOF
    all:
      hosts:
        192.168.0.2:  # Replace with the IP of your target host
    EOF
    
  3. 创建一个 test_remote.yml 剧本。

    cat > test_remote.yml<<EOF
    - name: Gather and print facts
      hosts: all
      gather_facts: true
      tasks:
    
      - name: Print facts
        ansible.builtin.debug:
          var: ansible_facts
    EOF
    
  4. postgresql_ee EE 中运行剧本。

    student 替换为适当的用户名。命令中的一些参数可以根据您的目标主机身份验证方法是可选的。

    ansible-navigator run test_remote.yml -i inventory --execution-environment-image postgresql_ee:latest --mode stdout --pull-policy missing --enable-prompts -u student -k -K
    

SSH 密钥转发

如果您使用基于 SSH 密钥的身份验证运行 Ansible 以避免身份验证提示,请使用 ssh-agent 将 SSH 密钥转发到您的 EE。

eval $(ssh-agent)
ssh-add </path/to/your/private/key>

要测试前面的剧本,请将 remote_user: <your_ssh_user> 字段添加到剧本,然后运行

ansible-navigator run test_remote.yml -i inventory --execution-environment-image postgresql_ee:latest --mode stdout --pull-policy missing

另请参阅