首页 文章

如何在ansible playbook中只运行一个任务?

提问于
浏览
109

有没有办法只在ansible playbook中运行一个任务?

例如,在 roles/hadoop_primary/tasks/hadoop_master.yml 中 . 我有 "start hadoop job tracker services" 任务 . 我可以运行那一项任务吗?

hadoop_master.yml文件:

---
# Playbook for  Hadoop master servers

- name: Install the namenode and jobtracker packages
  apt: name={{item}} force=yes state=latest
  with_items: 
   - hadoop-0.20-mapreduce-jobtracker
   - hadoop-hdfs-namenode
   - hadoop-doc
   - hue-plugins

- name: start hadoop jobtracker services
  service: name=hadoop-0.20-mapreduce-jobtracker state=started
  tags:
   debug

5 回答

  • 6

    您应该使用 tags: 中记录的 tags:


    如果你有一个大型的剧本,那么在不运行整个剧本的情况下运行配置的特定部分可能会很有用 .

    出于这个原因,播放和任务都支持“tags:”属性 .

    例:

    tasks:
    
        - yum: name={{ item }} state=installed
          with_items:
             - httpd
             - memcached
          tags:
             - packages
    
        - template: src=templates/src.j2 dest=/etc/foo.conf
          tags:
             - configuration
    

    如果你想只运行一个很长的剧本的“配置”和“包”,你可以这样做:

    ansible-playbook example.yml --tags "configuration,packages"
    

    另一方面,如果你想在没有特定任务的情况下运行一本剧本,你可以这样做:

    ansible-playbook example.yml --skip-tags "notification"
    

    您还可以将标记应用于角色:

    roles:
      - { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }
    

    您还可以标记基本的包含语句:

    - include: foo.yml tags=web,foo
    

    这两者都具有标记include语句中每个任务的功能 .

  • 58

    有一种方法,虽然不是很优雅:

    • ansible-playbook roles/hadoop_primary/tasks/hadoop_master.yml --step --start-at-task='start hadoop jobtracker services'

    • 您将收到提示: Perform task: start hadoop jobtracker services (y/n/c)

    • 回答 y

    • 您将收到下一个提示,按 Ctrl-C

  • 4

    我希望能够将角色用作任务集合,以便在我的剧本中,我可以选择要运行的任务子集 . 不幸的是,playbook只能加载它们然后你必须使用cmdline上的 --tags 选项来选择要运行的任务 . 这个问题是 all 任务将运行,除非你记得设置 --tags--skip-tags .

    我已经设置了一些任务,但只有一个 when: 子句只有在设置了var时才会触发 .

    例如

    # role/stuff/tasks/main.yml
    - name: do stuff
      when: stuff|default(false)
    

    现在,此任务默认不会触发,但仅当我设置 stuff=true

    $ ansible-playbook -e '{"stuff":true}'
    

    或在剧本中:

    roles:
    - {"role":"stuff", "stuff":true}
    
  • 4

    你熟悉handlers吗?我想这就是你要找的东西 . 将重启从 hadoop_master.yml 移至 roles/hadoop_primary/handlers/main.yml

    - name: start hadoop jobtracker services
      service: name=hadoop-0.20-mapreduce-jobtracker state=started
    

    现在在 hadoop_master.yml 中调用 notify

    - name: Install the namenode and jobtracker packages
      apt: name={{item}} force=yes state=latest
      with_items: 
       - hadoop-0.20-mapreduce-jobtracker
       - hadoop-hdfs-namenode
       - hadoop-doc
       - hue-plugins
      notify: start hadoop jobtracker services
    
  • 163

    使用Ansible 2.2的FWIW可以使用include_role

    playbook test.yml

    - name: test
      hosts:
        - 127.0.0.1
      connection: local
      tasks:
        - include_role:
            name: test
            tasks_from: other
    

    然后在 roles/test/tasks/other.yml

    - name: say something else
      shell: echo "I'm the other guy"
    

    并调用剧本: ansible-playbook test.yml 得到:

    TASK [test : say something else] *************
    changed: [127.0.0.1]
    

相关问题