Ansible 错误反复出现

Ansible 错误反复出现

错误!冲突的操作语句:主机、任务

错误似乎出现在“/home/a899444/aerospike-ansible/roles/java_install/tasks/main.yml”:第 1 行,第 3 列,但可能出现在文件的其他位置,具体取决于确切的语法问题。

有问题的一行似乎是:

- name: Java installation
 ^ here
- name: Java installation
  hosts: remote_server
  become: true
  tasks:
    - name: Download the JDK binaries
      get_url:
        url: https://download.java.net/java/GA/jdk18.0.2.1/db379da656dc47308e138f21b33976fa/1/GPL/openjdk-18.0.2.1_linux-x64_bin.tar.gz
        dest: /opt/openjdk-18.0.2.1_linux-x64_bin.tar.gz

    - name: Unzip the downloaded file
      unarchive:
        src: /opt/openjdk-18.0.2.1_linux-x64_bin.tar.gz
        dest: /opt
        remote_src: yes

    - name: Set the JAVA_HOME in /etc/profile file
      lineinfile:
        path: /etc/profile
        state: present
        line: "{{ item }}"
      with_items:
        - 'export JAVA_HOME="/opt/jdk-18.0.2.1"'
        - 'export PATH=$PATH:$JAVA_HOME/bin'

    - name: Reload /etc/profile file
      shell:
        cmd: source /etc/profile

答案1

在角色中,任务文件只能包含任务,但您使用剧本的格式。从顶层删除主机和其他所有内容:

- name: Download the JDK binaries
  get_url:
    url: https://download.java.net/java/GA/jdk18.0.2.1/db379da656dc47308e138f21b33976fa/1/GPL/openjdk-18.0.2.1_linux-x64_bin.tar.gz
    dest: /opt/openjdk-18.0.2.1_linux-x64_bin.tar.gz

- name: Unzip the downloaded file
  unarchive:
    src: /opt/openjdk-18.0.2.1_linux-x64_bin.tar.gz
    dest: /opt
    remote_src: yes

- name: Set the JAVA_HOME in /etc/profile file
  lineinfile:
    path: /etc/profile
    state: present
    line: "{{ item }}"
  with_items:
    - 'export JAVA_HOME="/opt/jdk-18.0.2.1"'
    - 'export PATH=$PATH:$JAVA_HOME/bin'

- name: Reload /etc/profile file
  shell:
    cmd: source /etc/profile

主机定义属于您使用角色的剧本:

hosts: remote_server
become: true
roles:
  - java_install

不过,您可能希望使用一些变量而不是固定路径和文件名。最后一项任务完全没用。每个任务都会启动一个新的 shell,无论如何都会读取该文件。

还有unarchive模块接受 URL。您可以将其简化为两个任务:

- name: Unzip JDK
  unarchive:
    src: https://download.java.net/java/GA/jdk18.0.2.1/db379da656dc47308e138f21b33976fa/1/GPL/openjdk-18.0.2.1_linux-x64_bin.tar.gz
    dest: /opt

- name: Set the JAVA_HOME in /etc/profile file
  lineinfile:
    path: /etc/profile
    state: present
    line: "{{ item }}"
  with_items:
    - 'export JAVA_HOME="/opt/jdk-18.0.2.1"'
    - 'export PATH=$PATH:$JAVA_HOME/bin'

相关内容