1

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.

Sachiko
  • 808
  • 1
  • 12
  • 31
  • 1
    How about adding tag to House Keep Task and executing tasks with only that tag? More here: https://stackoverflow.com/questions/23945201/how-to-run-only-one-task-in-ansible-playbook – unobatbayar Nov 17 '22 at 08:59
  • 1
    @unobatbayar Thanks for your prompt and kind comment. Since original ansible source is written by our customer, without tags, we'd like to control it with little revise as much as possible. But if there is no chance, we'll discuss with our custome to play it with tags. Thank you so much. – Sachiko Nov 18 '22 at 06:46

1 Answers1

1

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
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • I really, really appreciated your clear explanation. Honestly, ansible's relationship is bit complicated for me, but it's clear now thanks to you:) However, I still get No matching task error... May I ask we need some special option in ansible.cfg or --start-at-task is only for local use..? In our case, we'd like to set those task to another server (batch server) in the inventory. – Sachiko Nov 18 '22 at 06:43
  • 1
    There is no configuration setting related to `--start-at-task`. Only the command-line option is available. – Vladimir Botka Nov 18 '22 at 06:58
  • 1
    Open a new question and describe your use case in [mre]. I'm sure there will be a solution. [DevOps](https://devops.stackexchange.com/) would be the best site, I guess. – Vladimir Botka Nov 18 '22 at 07:01
  • Many thanks again, Vladimir for your kind advice. I'll open new question after some more trial. Thank you so much. Have a wonderful day!! – Sachiko Nov 18 '22 at 07:31