有没有办法只在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
Mxx.. 223
您应该使用tags:
在记录http://docs.ansible.com/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: foo.yml tags=web,foo
这两者都具有标记include语句中每个任务的功能.
您应该使用tags:
在记录http://docs.ansible.com/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: foo.yml tags=web,foo
这两者都具有标记include语句中每个任务的功能.
使用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"
并调用playbook:ansible-playbook test.yml
获得:
TASK [test : say something else] ************* changed: [127.0.0.1]
我希望能够将角色用作任务集合,以便在我的剧本中,我可以选择要运行的任务子集.不幸的是,剧本只能加载它们然后你必须使用--tags
cmdline上的选项来选择要运行的任务.这个问题是除非你记得设置或,否则所有任务都会运行.--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}
有一种方法,虽然不是很优雅:
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