【问题标题】:Ansible playbook failed when running a shell script运行 shell 脚本时 Ansible playbook 失败
【发布时间】:2022-01-12 15:08:48
【问题描述】:

所以我创建了这个 Ansible 剧本来:

  1. 复制一个 zip 文件并解压
  2. 复制一个 zip 文件并解压
  3. 使文件内的脚本可执行
  4. 运行脚本
  5. 启用 2 项服务
- name: fideliplaybook
  hosts: k8scluster
  tasks:

    - name: copying file with playbook
      become: true
      copy:
        src: /home/lc/lc.zip
        dest: /home/lc/
        mode: 755

    - name: Update apt cache and install unzip
      become: true
      command: apt install unzip


    - name: unzip file
      become: true
      unarchive:
        src: /home/lc/lc.zip
        dest: /home/lc/

    - name: make script executable
      become: true
      file: dest=/home/lc/lc/install.sh mode=755

    - name: Execute the script
      become: true
      command: sh /home/lc/lc/install.sh

    - name: Enable service 1
      become: true
      command: systemctl enable service1.service

    - name: Enable service 2
      become: true
      command: systemctl enable service1.service

我在这里面临的问题是,当 ansible 试图执行脚本“install.sh”时 它以某种方式失败,因为脚本无法找到其他 2 个脚本,尽管其他脚本在同一个文件中。

所以 lc.zip 中有 3 个脚本,但是当使用 ansible 运行 install.sh 时,它找不到其他 2 个脚本。 我通过在第一个脚本中输入 2 个脚本的完整路径来解决此问题。 但任何人都知道为什么会出现这个问题。

当我使用 ansible hosts 文件来定义主机和变量时,还有一个问题,它不能像这样与 INI 一起使用:

[webservers]
www[01:50].example.com

它只在 YAML 中工作:

...
  webservers:
    hosts:
      www[01:50].example.com:

有人知道为什么吗?

【问题讨论】:

    标签: ansible


    【解决方案1】:

    关于脚本找不到其他两个脚本,如果路径始终是固定的,您应该更新其他两个脚本的完整路径,或者如果您想使用相对路径,您可以尝试以下方法,

       - name: Execute the script
         become: true
         command: sh install.sh
         args:
           chdir: /home/lc/lc/
    

    而且库存看起来不错。这是一个测试来确认。

    # cat test.ini
    [webservers]
    tstsrv[7:8]
    
    
    # ansible-playbook -i test.ini test.yaml -u admin -k
    SSH password:
    
    PLAY [webservers] ***********************************************************************************************************************************************************
    
    TASK [Gathering Facts] **********************************************************************************************************************************************
    ok: [tstsrv8]
    ok: [tstsrv7]
    
    TASK [print hostname] ***********************************************************************************************************************************************
    changed: [tstsrv8]
    changed: [tstsrv7]
    
    PLAY RECAP **********************************************************************************************************************************************************
    tstsrv7                 : ok=2    changed=1    unreachable=0    failed=0
    tstsrv8                 : ok=2    changed=1    unreachable=0    failed=0
    
    #
    

    如果可能,请考虑专用于在目标上运行脚本的 script 模块。

    【讨论】:

      猜你喜欢
      • 2013-12-09
      • 2016-12-10
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多