有没有办法在一个可行的剧本中只运行一个任务?

例如,“roles/hadoop_primary/tasks/hadoop_master.yml”。我有“启动hadoop作业跟踪服务”任务。我能只运行一个任务吗?

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

当前回答

你熟悉教练吗?我觉得这就是你想要的。从hadoop_master移动重启。到roles/hadoop_primary/handlers/main.yml:

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

现在在hadoop_master.yml中调用use 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

其他回答

你熟悉教练吗?我觉得这就是你想要的。从hadoop_master移动重启。到roles/hadoop_primary/handlers/main.yml:

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

现在在hadoop_master.yml中调用use 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

您应该使用标签:如https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html中所述


如果您有一个很大的剧本,那么能够运行配置的特定部分而不运行整个剧本可能会变得很有用。

出于这个原因,游戏和任务都支持“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语句:

- include: foo.yml tags=web,foo

这两个函数都具有在include语句中标记每个任务的功能。

使用标签可以很容易地做到这一点

标签的例子定义如下:

---
hosts: localhost
tasks:
 - name: Creating s3Bucket
   s3_bucket:
        name: ansiblebucket1234567890
   tags: 
       - createbucket

 - name: Simple PUT operation
   aws_s3:
       bucket: ansiblebucket1234567890
       object: /my/desired/key.txt
       src: /etc/ansible/myfile.txt
       mode: put
   tags:
      - putfile

 - name: Create an empty bucket
   aws_s3:
       bucket: ansiblebucket12345678901234
       mode: create
       permission: private
   tags:
       - emptybucket

要执行标记,我们使用命令

ansible-playbook creates3bucket.yml --tags "createbucket,putfile"

请看我的回答:只运行ansible playbook中的一个任务和处理程序

可以运行单独的role (from roles/ dir):

ansible -i stage.yml -m include_role -a name=create-os-user localhost

和单独的任务文件:

ansible -i stage.yml -m include_tasks -a file=tasks/create-os-user.yml localhost

如果将任务从role外部化到根tasks/目录(通过import_tasks: ../../tasks/create-os-user.yml实现重用),则可以独立于playbook/role运行它。

Ansible 2.2的FWIW可以使用include_role:

剧本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调用playbook。Yml获取:

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