cancel
Showing results for 
Search instead for 
Did you mean: 
masharma
Cadet
Cadet
  • 43 Views

Some useful tips for running ansible playbook

This tips are more useful when developing the ansible code. Also if we use roles, many of them will work easily with import_role rather then include_role.

Skip to step 5 , if you are well versed on tags,always,never.

 

1) We can use tags to run a specific task from the playbook

$ cat example1.yaml

 

 

- name: Tags example
  hosts: localhost
  connection: local
  tasks:
   - name: tag example task1
     ansible.builtin.debug:
       msg: "Hello from task1"
     tags: task1
  
   - name: tag example task2
     ansible.builtin.debug:
       msg: "Hello from task2"
     tags: task2

 

 

 

Running only task1:

 

 

$ ansible-playbook example1.yaml -t task1

PLAY [Tags example] **********************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************
ok: [localhost.example.com]

TASK [tag example task1] *****************************************************************************************************
ok: [localhost.example.com] => {
"msg": "Hello from task1"
}

PLAY RECAP *******************************************************************************************************************
localhost.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

 

 

 

Similarly we can run task2, by just calling the tag of task2


2) If we want to run any task always , even if we call other tags then we need to use always tag


$ cat example1.yaml

 

 

- name: Tags example
  hosts: localhost
  connection: local
  tasks:
   - name: tag example task1
     ansible.builtin.debug:
       msg: "Hello from task1"
     tags: [always,task1]
  
   - name: tag example task2
     ansible.builtin.debug:
       msg: "Hello from task2"
     tags: task2

 

 


I am calling task2 yet it will run task1 :

 

 

$ ansible-playbook example1.yaml -t task2

PLAY [Tags example] **********************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************
ok: [localhost.example.com]

TASK [tag example task1] *****************************************************************************************************
ok: [localhost.example.com] => {
    "msg": "Hello from task1"
}

TASK [tag example task2] *****************************************************************************************************
ok: [localhost.example.com] => {
    "msg": "Hello from task2"
}

PLAY RECAP *******************************************************************************************************************
localhost.example.com : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

 

 


3) If we don't want any task to run until we call it, then we can use never tag

$ cat example1.yaml

 

 

- name: Tags example
  hosts: localhost
  connection: local
  tasks:
   - name: tag example task1
     ansible.builtin.debug:
       msg: "Hello from task1"
     tags: [never,task1]
  
   - name: tag example task2
     ansible.builtin.debug:
       msg: "Hello from task2"
     tags: task2

 

 


Only task2 runs as task1 is not called:

 

 

$ ansible-playbook example1.yaml 

PLAY [Tags example] **********************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************
ok: [localhost.example.com]

TASK [tag example task2] *****************************************************************************************************
ok: [localhost.example.com] => {
    "msg": "Hello from task2"
}

PLAY RECAP *******************************************************************************************************************
localhost.example.com : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

 

 


task1 runs as it is called

 

 

$ ansible-playbook example1.yaml -t task1

PLAY [Tags example] **********************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************
ok: [localhost.example.com]

TASK [tag example task1] *****************************************************************************************************
ok: [localhost.example.com] => {
    "msg": "Hello from task1"
}

PLAY RECAP *******************************************************************************************************************
localhost.example.com : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
 

 

 


4) If we want to skip any task during run then we can use "--skip-tags" option

$ cat example1.yaml

 

 

- name: Tags example
  hosts: localhost
  connection: local
  tasks:
   - name: tag example task1
     ansible.builtin.debug:
       msg: "Hello from task1"
     tags: [never,task1]
  
   - name: tag example task2
     ansible.builtin.debug:
       msg: "Hello from task2"
     tags: task2

 

 

 

Here it will not run anything as we are not calling never tag and also telling playbook to skip task2

 

 

$ ansible-playbook example1.yaml --skip-tags task2

PLAY [Tags example] **********************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************
ok: [localhost.example.com]

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

 

 

 

You can explore more on this.

 

5) If we want to run the playbook for checking and not allow it to do any changes , sort of dry run, we can use "--check" option

$ cat example1.yaml

 

 

- name: More examples
  hosts: localhost
  connection: local
  become: true
  gather_facts: False
  tasks:
   - name: Stop a service 
     ansible.builtin.systemd_service:
       name: ollama.service
       state: stopped

 

 

 

Service is active:

 

 

$ systemctl is-active ollama
active

 

 

 

Running the playbook with "--check"

 

 

$ ansible-playbook example1.yaml -K --check
BECOME password:

PLAY [More examples] *********************************************************************************************************

TASK [Stop a service] ********************************************************************************************************
changed: [localhost.example.com]

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

 

 

 

Service is still active:

 

 

$ systemctl is-active ollama
active

 

 

 

Running the playbook without "--check":

 

 

$ ansible-playbook example1.yaml -K
BECOME password:

PLAY [More examples] *********************************************************************************************************

TASK [Stop a service] ********************************************************************************************************
changed: [localhost.example.com]

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

 

 

 

Service is now stopped:

 

 

$ systemctl is-active ollama
inactive

 

 


6) If we want ansible to ask us before running any task we can run the playbook with "--step" option:

$ cat example1.yaml

 

 

- name: More examples
  hosts: localhost
  connection: local
  tasks:
   - name: Print date and message
     ansible.builtin.debug:
       msg: "{{ ansible_date_time.date }} ***Merry Christmas***"

   - name: Run command
     ansible.builtin.shell: whoami

 

 

 

Running the playbook with "--step"

 

 

$ ansible-playbook example1.yaml --step

PLAY [More examples] *********************************************************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: *******************************************************************

TASK [Gathering Facts] *******************************************************************************************************
ok: [localhost.example.com]
Perform task: TASK: Print date and message (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Print date and message (N)o/(y)es/(c)ontinue: ************************************************************

TASK [Print date and message] ************************************************************************************************
ok: [localhost.example.com] => {
"msg": "2024-12-25 ***Merry Christmas***"
}
Perform task: TASK: Run command (N)o/(y)es/(c)ontinue: n

Perform task: TASK: Run command (N)o/(y)es/(c)ontinue: ***********************************************************************

PLAY RECAP *******************************************************************************************************************
localhost.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

 

 


7) If we want to start at specific task of the playbook and proceed till end then we can use "--start-at-task" option

$ cat example1.yaml

 

 

- name: More examples
  hosts: localhost
  connection: local
  tasks:
   - name: Print date and message
     ansible.builtin.debug:
       msg: "{{ ansible_date_time.date }} ***Merry Christmas***"

   - name: Run command
     ansible.builtin.shell: whoami
     register: var1

   - name: Print var1
     ansible.builtin.debug:
        msg: "{{ var1.stdout_lines }}"

 

 

 

I want to run only the last 2 tasks:

 

 

$ ansible-playbook example1.yaml --start-at-task "Run command"

PLAY [More examples] *********************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************
ok: [localhost.example.com]

TASK [Run command] ***********************************************************************************************************
changed: [localhost.example.com]

TASK [Print var1] ************************************************************************************************************
ok: [localhost.example.com] => {
"msg": [
"masharma"
]
}

PLAY RECAP *******************************************************************************************************************
localhost.example.com : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

 

 

 

I would love to hear some more tips or suggestions on this.

 

Thank you!

 

 

 

Labels (2)
Tags (1)
1 Reply
Trevor
Starfighter Starfighter
Starfighter
  • 16 Views

I just want to say thank you very much for these excellent tips!!!

Trevor "Red Hat Evangelist" Chandler
Join the discussion
You must log in to join this conversation.