Score:0

How to make Ansible run batch of tasks on Cluster Node's - node by node

cn flag

I have an ansible playbook which looks roughly like the following. I need Zero Downtime Upgrade start and Zero Downtime Upgrade completed steps. will run once to put\remove cluser into upgrade mode. in addition I need all the tasks into Block will be run only on one node on same time and continue to next node once we get HTTP 200

Could you please review the yml file ? currently I get ERROR! 'uri' is not a valid attribute for a Block and if you have any suggestion to improve it.

---
- name: Zero Downtime Upgrade start
  hosts: atl
  serial: 1
  gather_facts: false
  vars_files:
    vars.yml
  tasks:
   - name: get Cluster state
     run_once: true
     uri:
      url: https://{{ base_url }}.XXX.com/rest/api/2/cluster/zdu/state
      headers:
       Content-Type: application/json
      force_basic_auth: true
      validate_certs: false
      user: XXX
      password: XXX
     register: response
  #  - name: debug.
  #    debug: var=response
  #    run_once: true
   - name: trigger Zero Downtime upgrade API
     run_once: true
     uri:
      url: https://{{ base_url }}.XXX.com/rest/api/2/cluster/zdu/start
      method: POST
      validate_certs: false
      headers:
       Content-Type: application/json
      force_basic_auth: true
      user: XXX
      password: XXX
      status_code: 201
#     register: response
     when: response.json.state == 'STABLE'
  #  - name: debug.
  #    run_once: true
  #    debug: var=response
   - block:
      - name: create dir for backup configuration
        file:
          path: "{{ atl_backup_conf }}"
          state: directory
          mode: '0755'
          owner: atl
          group: atl
      - name: backup configuration files
        copy:
          src: "{{ item }}"
          dest: "{{ atl_backup_conf }}"
    #      backup: true
          remote_src: true
          owner: atl
          group: atl
        with_items:
          - "{{ atl_app }}/bin/setenv.sh"
        ignore_errors: true
      - name: "stop atl service on {{ ansible_hostname }} atl node"
        #shell: /etc/init.d/atl stop
        systemd:
          name: atl
          state: stopped
        become: yes
- name: Zero Downtime Upgrade completed steps.
     hosts: atl
     vars_files:
      vars.yml
     tasks: 
     - name: trigger Zero Downtime upgrade API change to Stable status.
       run_once: true
       uri:
        url: https://{{ base_url }}.XXX.com/rest/api/2/cluster/zdu/approve
        method: POST
        validate_certs: false
        headers:
          Content-Type: application/json
        force_basic_auth: true
        user: XXX
        password: XXX
        status_code: 409 , 200

Score:1
in flag

You missed a dash in front of block.

Removing all attributes your tasks look like this:

  tasks:
  - name: get Cluster state
  - name: trigger Zero Downtime upgrade API
    block:
# ^-- dash missing
      - name: "Upgrade {{ ansible_hostname }} atl node."
      - name: create dir for backup configuration
      - name: "stop atl service on {{ ansible_hostname }} atl node"
      - name: Install New atl Version.
    - name: Configure systemd service.
#   ^-- indented incorrectly
      - name: Reload Enable and Start atl.service
      - name: health checks - trigger GET node state.

As you see, block is currently an attribute for the previous task, it should be at the level of tasks instead.

Additionally, your task Configure systemd service. is not indented correctly, it should be at the same level as the other tasks.

  tasks:
  - name: get Cluster state
  - name: trigger Zero Downtime upgrade API
  - block:
# ^-- added missing dash
      - name: "Upgrade {{ ansible_hostname }} atl node."
      - name: create dir for backup configuration
      - name: "stop atl service on {{ ansible_hostname }} atl node"
      - name: Install New atl Version.
      - name: Configure systemd service.
#     ^-- indented correctly
      - name: Reload Enable and Start atl.service
      - name: health checks - trigger GET node state.

YAML is extremely picky about indentation.

cn flag
`- name: Zero Downtime Upgrade start hosts: atl serial: 1 gather_facts: false vars_files: vars.yml tasks: - name: trigger Zero Downtime upgrade API - block: - name: "Upgrade {{ ansible_hostname }} atl node." - name: create dir for backup configuration - name: "stop atl service atl node" - name: health checks - trigger GET node state. retries: 720 delay: 5 - name: Zero Downtime Upgrade completed steps. hosts: atl tasks: - name: trigger Zero Downtime upgrade run_once: true uri:`
cn flag
thank you for your reply ,not sure how can I make it more readable in comment area.?! I need 2 first commands under tasks: will be run one time but all commands under - block: will be exec on all nodes but **node by node** and in the end again from **Upgrade completed steps.** will be run one time on one of the node.
in flag
You can't make it readable in the comments. If you have something to add to your existing question, edit it. If you encounter a new problem, it's better to accept the answer that helped you and ask a new question with the new problem.
cn flag
once I run the playbook I get the error : `ERROR! no module/action detected in task. The error appears to be in main_new.yml': line 41, column 9, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: - block: - name: Upgrade atl node. ^ here ` I use ansible-playbook 2.10.9 , do you sure for `- block:` module syntax ? I see there is exist `block:` with out -
in flag
Yes, [I'm sure](https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html).
in flag
We can't tell you what's wrong exactly without seeing the current state of your playbook.
cn flag
unfortunately,I can't shared here because the comments not readable.any idea how can I share the current state ?
in flag
As I already told you: edit your question. Don't remove what's already in there, just add it below.
cn flag
it gave me error while try to add so I update it based on your last comment
cn flag
I just want to we on same page - I need all tasks under 'get Cluster state' and 'get Cluster state' will be exec one time (run_once=true) and all the tasks inside the -block: will be exec node by node, I mean first to run all tasks on first node in atl hosts group once node is up mean I get HTTP=200 it can proceed to next node in atl hosts group and so on. once complete the '-block:' tasks can proceed and run tasks under 'Zero Downtime Upgrade completed steps.' only on one of the node (run_once=true) I hope it more clear now
cn flag
thank you, I managed it.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.