Score:0

How to make Ansible run batch of tasks on few nodes but one by one (nodes running in cluster)

cn flag

I have an ansible playbook which looks roughly like this:

- hosts: node1
      tasks:
      - name: get cluster state 
        shell: "RESTAPI 1 command"
      - name: put cluster in upgrade mode
        shell: "RESTAPI 2 command"
- hosts: node 1
      tasks:
      - name: upgrade Apache
        shell: "upgrade Apache command"
      - name: start Apache
        shell: "start Apache command"
- hosts: node 1
      tasks:
      - name: healthy check for Apache is running
        shell: "RESTAPI command"
- hosts: node 2
      tasks:
      - name: upgrade Apache
        shell: "upgrade Apache command"
      - name: start Apache
        shell: "start Apache command"
- hosts: node 2
      tasks:
      - name: healthy check for Apache is running
        shell: "RESTAPI command"
- hosts: node 3
      tasks:
      - name: upgrade Apache
        shell: "upgrade Apache command"
      - name: start Apache
        shell: "start Apache command"
- hosts: node 3
      tasks:
      - name: healthy check for Apache is running
        shell: "RESTAPI command"

I want to improve the playbook that will run as a loop because for each node I have same commands that should be exec on each node one by one because all nodes running in cluster and I need only one node will be in upgrade process at the same time. also I have few environment (Test/Dev) that I want to use this playbook but I have different nodes number in cluster how can I make run the playbook no matter how many nodes I have into the cluster?

Score:1
in flag

You can place all recurring tasks in a block. Combined with serial: 1 you make sure that only one block is executed at a time.

- hosts: nodes
  serial: 1
  tasks:
    - name: get cluster state 
      shell: "RESTAPI 1 command"
      run_once: yes
    - name: put cluster in upgrade mode
      shell: "RESTAPI 2 command"
      run_once: yes
    - block:
      - name: upgrade Apache
        shell: "upgrade Apache command"
      - name: start Apache
        shell: "start Apache command"
      - name: healthy check for Apache is running
        shell: "RESTAPI command"
#       retries: 20
#       delay: 15

I'm assuming that the command to put the cluster in upgrade mode can be run on any node, so you can just use run_once. If this is not the case and it has to be run on a specific node you could use a when restriction for a specific host.

For the health check the parameters delay and retries could be of interest, if the possibility exists that the check fails at first and succeeds when apache is fully running.

To use the playbook on different host groups you could use hosts: all and then limit the hosts to a group during execution.

ansible-playbook upgrade.yml --limit dev_nodes
cn flag
Hi @GeraldSchneider thank you for your reply , I tried to edit my yml file based on your suggestion but I get syntax errors ERROR! 'uri' is not a valid attribute for a Block `- hosts: nodes serial: 1 tasks: - name: get cluster state uri: run_once: yes block: - name: copy copy: - name: file file - name: systemd check systemd: - name: blockinfile blockinfile # retries: 20 # delay: 15 `
in flag
That's hard to read in the comment, but it looks like you use a colon in the task name. You need to quote it when you do that.
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.