【问题标题】:When I run my Ansible-Playbook, I want my task name to execute specifically当我运行我的 Ansible-Playbook 时,我希望我的任务名称专门执行
【发布时间】:2020-09-13 03:41:17
【问题描述】:

我的剧本文件:

---
- hosts : localhost
  become: yes
  tasks:
  - name: To install "{{ item }}"
    yum:
      name: "{{ item }}"
    with_items: 
    - git
    - tree
    - wget

当我执行上面的剧本文件时,我的输出是:

PLAY [localhost] ****************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************
ok: [localhost]

TASK [To install "{{ item }}"] **************************************************************************************************************************************************
changed: [localhost] => (item=[u'git', u'tree', u'wget'])

PLAY RECAP **********************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

这里TASK [To install "{{ item }}"] => 我不想要item这个词,我想要它是具体的。就像循环进入 git 时一样,它应该是“安装 git”。然后在树上,应该是“安装树”等,由于 3 个安装,我应该得到 3 个任务。 我尝试使用调试模块,但它失败了。

注意:我希望这些变量(git、tree、wget)出现在我的 playbook.yaml 文件中。

如果您有任何疑问,请告诉我。 提前致谢!!

【问题讨论】:

    标签: ansible


    【解决方案1】:

    不会在循环的每次迭代中评估任务的名称。为了实现您想要的,可以将任务放入单独的文件中并将其包含在循环中。比如下面的文件和剧本

    shell> cat install_pkg.yml 
    - name: "To install {{ item }}"
      debug:
        msg: "name: {{ item }}"
    
    shell> cat playbook.yml
    - hosts: localhost
      tasks:
        - include_tasks: install_pkg.yml
          loop:
            - git
            - tree
            - wget
    

    给(删节)

    shell> ansible-playbook playbook.yml 
    
    TASK [include_tasks] ****
    included: /scratch/install_pkg.yml for localhost
    included: /scratch/install_pkg.yml for localhost
    included: /scratch/install_pkg.yml for localhost
    
    TASK [To install git] ****
    ok: [localhost] => 
      msg: 'name: git'
    
    TASK [To install tree] ****
    ok: [localhost] => 
      msg: 'name: tree'
    
    TASK [To install wget] ****
    ok: [localhost] => 
      msg: 'name: wget'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多