Sachiko
Sachiko

Reputation: 924

How can we execute only new task in Ansible playbook?

We have bit huge ansible tasks in main.yaml and we don't need to execute all tasks, only new task 'House Keep Task' is enough. So tried '--start-at-task' option as below, but Ansible can't find that task;

command :

ansible-playbook -u sysadmin  -i ./inventories/dev/hosts --start-at-task='House Keep Task' --step batchservers.yml -K

message : [ERROR]: No matching task "House Keep Task" found. Note: --start-at-task can only follow static includes.

batchserver.yaml

---
- hosts: batchservers
  become: true
  tasks:
  - import_role:
      name: batchservers
  tags: [batch]

ansible/roles/batchservers/tasks/main.yaml

---
- name: Add SHARED_DATA_PATH env
  lineinfile:
    dest: ~/.bash_profile
    line: export SHARED_DATA_PATH=/data

- name: Create /data if it does not exist
  file:
    path: /data
    state: directory
    mode: og+w
... other tasks include reboot task ...

- name: House Keep Task
      cron:
        name: "House Keep Task"
        user: "{{ batch_user }}"
        special_time: daily
        job: "/usr/bin/find /var/log -name '*.log' -mtime +6 -type f -delete"
        state: present

Is there any good way to execute particular task, House Keep Task? Our ansible version is core 2.11.12. Any advice would be highly apprciated.

Upvotes: 1

Views: 1084

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68144

Q: "Execute particular task House Keep Task."

A: The ansible-playbook option --start-at-task works as expected

--start-at-task 'START_AT_TASK' start the playbook at the task matching this name

Given the tree

shell> tree .
.
├── ansible.cfg
├── hosts
├── pb.yml
└── roles
    └── batchservers
        └── tasks
            └── main.yaml

3 directories, 4 files

The playbook

shell> cat pb.yml 
- hosts: localhost
  gather_facts: false
  tasks:
    - import_role:
        name: batchservers

and the role

shell> cat roles/batchservers/tasks/main.yaml 
- name: Add SHARED_DATA_PATH env
  debug:
    msg: Add SHARED_DATA_PATH env
- name: Create /data if it does not exist
  debug:
    msg: Create /data if it does not exist
- name: House Keep Task
  debug:
    msg: House Keep Task

give

shell> ansible-playbook pb.yml --start-at-task 'House Keep Task'

PLAY [localhost] *****************************************************************************

TASK [batchservers : House Keep Task] ********************************************************
ok: [localhost] => 
  msg: House Keep Task

PLAY RECAP ***********************************************************************************
localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Upvotes: 2

Related Questions