How to run only one task in an unoccupied play?

Is there a way to run only one task in a playbook inaccessible to view?

For example, in roles/hadoop_primary/tasks/hadoop_master.yml . I have a task "start hadoop job tracker services" . Is it possible to run only 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 
+111
ansible
May 30 '14 at
source share
6 answers

You should use tags: as described in http://docs.ansible.com/playbooks_tags.html




If you have a large playbook, it may be useful to run a specific part of the configuration without starting the entire tutorial.

Both games and tasks support the "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 want to just run part of the โ€œconfigurationโ€ and โ€œpackagesโ€ from a very long piece, you can do this:

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

On the other hand, if you want to run a playbook without specific tasks, you can do this:

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

You can also apply tags to roles:

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

And you can also flag basic include statements:

 - include: foo.yml tags=web,foo 

Both of them have the function of marking each individual task inside the include statement.

+167
May 30 '14 at 4:29
source share
โ€” -

There is a way, though not very elegant:

  • ansible-playbook roles/hadoop_primary/tasks/hadoop_master.yml --step --start-at-task='start hadoop jobtracker services'
  • You will receive an invitation: Perform task: start hadoop jobtracker services (y/n/c)
  • Answer y
  • The following prompt appears, press Ctrl-C
+59
Oct 21 '15 at 16:05
source share

I would love the opportunity to use the role as a set of tasks, so in my book I can choose which subset of the tasks to run. Unfortunately, a playbook can only load them all, and then you have to use the --tags option in cmdline to select the tasks to run. The problem is that all tasks will be performed if you do not remember the installation of --tags or --skip-tags .

I installed some tasks, however, with the when: clause, which will only work when var is installed.

eg.

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

Now this task will not work by default, but only if I set stuff=true

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

or in the textbook:

 roles: - {"role":"stuff", "stuff":true} 
+6
Feb 05 '16 at 17:23
source share

FWIW with Ansible 2.2 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 call the playbook with: ansible-playbook test.yml to get:

 TASK [test : say something else] ************* changed: [127.0.0.1] 
+5
Dec 12 '17 at 22:20
source share

Do you know handlers ? I think this is 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 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 
+4
May 30 '14 at 3:29
source share

This can be easily done using tags.

An example tag 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 tags we use the command

 ansible-playbook creates3bucket.yml --tags "createbucket,putfile" 
+1
Jan 09 '19 at 10:38
source share



All Articles