How to run only one task in ansible playbook?
How to run only one task in ansible playbook?
https://stackoverflow.com/questions/23945201/how-to-run-only-one-task-in-ansible-playbook
Asked 11 years, 7 months ago Modified 4 years, 11 months ago Viewed 309k times 262
Is there a way to only run one task in ansible playbook?
For example, in roles/hadoop_primary/tasks/hadoop_master.yml. I have “start hadoop job tracker services” task. Can I just run that one task?
hadoop_master.yml file:
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 ansible Share Improve this question Follow edited Dec 21, 2020 at 22:44 Brian Tompsett - 汤莱恩’s user avatar Brian Tompsett - 汤莱恩 5,9377272 gold badges6464 silver badges135135 bronze badges asked May 30, 2014 at 0:27 Billz’s user avatar Billz 8,17588 gold badges3535 silver badges3535 bronze badges Add a comment 7 Answers Sorted by:
Highest score (default) 379
You should use tags: as documented in https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html
If you have a large playbook it may become useful to be able to run a specific part of the configuration without running the whole playbook.
Both plays and tasks support a “tags:” attribute for this reason.
Example:
tasks:
- yum: name={{ item }} state=installed
with_items:
- httpd
- memcached
tags:
- packages
- template: src=templates/src.j2 dest=/etc/foo.conf
tags:
- configuration If you wanted to just run the “configuration” and “packages” part of a very long playbook, you could do this:
ansible-playbook example.yml –tags “configuration,packages” On the other hand, if you want to run a playbook without certain tasks, you could do this:
ansible-playbook example.yml –skip-tags “notification” You may also apply tags to roles:
roles:
-
{ role: webserver, port: 5000, tags: [ ‘web’, ‘foo’ ] } And you may also tag basic include statements:
-
include: foo.yml tags=web,foo Both of these have the function of tagging every single task inside the include statement.
Share Improve this answer Follow edited Feb 19, 2021 at 14:02 Patrick Decat’s user avatar Patrick Decat 63055 silver badges1111 bronze badges answered May 30, 2014 at 4:29 Mxx’s user avatar Mxx 9,42444 gold badges3030 silver badges3737 bronze badges Sign up to request clarification or add additional context in comments.
9 Comments
Hi-Angel Over a year ago I’d recommend against using –tags. The problem is that ansible-playbook will not fail if you typoed the tag, and from what I’ve been told on IRC there’s no way to make it fail. This means it’s easy to introduce hard-to-find bugs during refactoring a playbook. I personally decided instead to go split a playbook to smaller ones, so ansible-playbook would run everything inside a given one. Not something I like, but oh well…
Mxx Over a year ago “from what i’ve been told on irc” is hardly a quality reference source. Perhaps you want to link to some published article or post discussing those issues?
Hi-Angel Over a year ago @Mxx sure, is docs considered a quality source? In this case just type man ansible-playbook and make a search for “tags” keyword. You’ll only find options chose/skip/list tags. No option to make it fail.
Federico Over a year ago This is not flexible and selective enough. Also, when tasks depends on each other it does not work.
Mxx Over a year ago @Federico, please post a better answer. Add a comment | Show 4 more comments 136
There is a way, although not very elegant:
ansible-playbook roles/hadoop_primary/tasks/hadoop_master.yml –step –start-at-task=’start hadoop jobtracker services’ You will get a prompt: Perform task: start hadoop jobtracker services (y/n/c) Answer y You will get a next prompt, press Ctrl-C Share Improve this answer Follow answered Oct 21, 2015 at 16:05 Victor Ashik’s user avatar Victor Ashik 1,49911 gold badge99 silver badges55 bronze badges 3 Comments
lanoxx Over a year ago Combining that with the –check and -vvv option is also quite useful. It will not actually perform the command but give you very verbose output what would have happened.
mgutt Over a year ago Is it possible to skip the prompt?
Gen.Stack Over a year ago @mgutt just omit “–step”, ITIWYM. Add a comment 19
FWIW with Ansible 2.2 one can use include_role:
playbook test.yml:
- name: test
hosts:
- 127.0.0.1 connection: local tasks:
- include_role: name: test tasks_from: other then in roles/test/tasks/other.yml:
- name: say something else shell: echo “I’m the other guy” And invoke the playbook with: ansible-playbook test.yml to get:
TASK [test : say something else] ***** changed: [127.0.0.1] Share Improve this answer Follow edited Dec 12, 2017 at 22:41 Thomas Fritsch’s user avatar Thomas Fritsch 10.3k3333 gold badges4242 silver badges4949 bronze badges answered Dec 12, 2017 at 22:20 ddragosd’s user avatar ddragosd 34622 silver badges33 bronze badges Comments
11
See my answer here: Run only one task and handler from ansible playbook
It is possible to run separate role (from roles/ dir):
ansible -i stage.yml -m include_role -a name=create-os-user localhost and separate task file:
ansible -i stage.yml -m include_tasks -a file=tasks/create-os-user.yml localhost If you externalise tasks from role to root tasks/ directory (reuse is achieved by import_tasks: ../../../tasks/create-os-user.yml) you can run it independently from playbook/role.
Share Improve this answer Follow answered Sep 28, 2020 at 12:50 gavenkoa’s user avatar gavenkoa 49.8k2828 gold badges274274 silver badges343343 bronze badges Comments
9
I would love the ability to use a role as a collection of tasks such that, in my playbook, I can choose which subset of tasks to run. Unfortunately, the playbook can only load them all in and then you have to use the –tags option on the cmdline to choose which tasks to run. The problem with this is that all of the tasks will run unless you remember to set –tags or –skip-tags.
I have set up some tasks, however, with a when: clause that will only fire if a var is set.
e.g.
role/stuff/tasks/main.yml
- name: do stuff when: stuff|default(false) Now, this task will not fire by default, but only if I set the stuff=true
$ ansible-playbook -e ‘{“stuff”:true}’ or in a playbook:
roles:
- {“role”:”stuff”, “stuff”:true} Share Improve this answer Follow answered Feb 5, 2016 at 17:23 ChePazzo’s user avatar ChePazzo 30133 silver badges33 bronze badges 4 Comments
Scott Prive Over a year ago I’m just a newbie, and I hear what you’re saying… but I would explore why you are averse to having the whole playbook run. A proper Ansible play is usually idempotent, and will gather facts and “do nothing” if the state criteria is met. I admit I share this concern as most of my plays “do something” rather than “check if this is the state, and do something if needed”. The former could only be run once, or supervised while the latter could be run at any time and it’ll be harmless.
ChePazzo Over a year ago I normally use this for debug tasks. Normally, I don’t want the debug info to run, but sometimes I do. Looking at the other responses, though, there might be a better way to do that now.
Scott Prive Over a year ago Yes, there is. To be specific, one way to selectively run plays now is by “tagging” the plays. There may be OTHER ways to limit the plays also; I am still learning…
pyansharp Over a year ago At least in my case, the reason to not run the entire playbook is because it 1) is very long and 2) might be running against about 400 hosts. That takes a while. I make liberal use of -t, -l, and –start-at-task because sometimes I need to get something out fast, or I just don’t feel like watching my terminal scroll for an hour. Add a comment 5
This can be easily done using the tags
The example of tags is defined below:
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 to execute the tags we use the command
ansible-playbook creates3bucket.yml –tags “createbucket,putfile” Share Improve this answer Follow answered Jan 9, 2019 at 10:38 Nitesh Jain’s user avatar Nitesh Jain 17722 silver badges33 bronze badges 1 Comment
alexs77 Over a year ago This doesn’t work - ERROR! A playbook must be a list of plays, got a <class ‘ansible.parsing.yaml.objects.AnsibleMapping’> instead 4
are you familiar with handlers? I think it’s what you are looking for. Move the restart from hadoop_master.yml to roles/hadoop_primary/handlers/main.yml:
-
name: start hadoop jobtracker services service: name=hadoop-0.20-mapreduce-jobtracker state=started and now call use notify in hadoop_master.yml:
-
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 Share Improve this answer Follow answered May 30, 2014 at 3:29 300D7309EF17’s user avatar 300D7309EF17 24.8k1414 gold badges9090 silver badges103