0

Thanks for giving me some of your time reading this question. :)

My complete playbook is:

---

- hosts: all
  remote_user: secret_user
  become: yes

  tasks:

    - include_vars: vars.yml

    - name: Copy set of rpms/binaries/and Java scripts to server.
      copy:
        src: "{{ item }}"
        dest: "{{ tempdir }}"
      with_items:
        - files/mongo-10gen-2.4.4-5.el6.x86_64.rpm
        - files/mongo-10gen-server-2.4.4-5.el6.x86_64.rpm
        - files/mongo-10gen-2.6-1.el6.x86_64.rpm
        - files/mongo-10gen-server-2.6-1.el6.x86_64.rpm
        - files/mongo26
        - bash/javascript/populateDB.js

    - name: Create mongo user.
      user:
        name: mongod
        comment: For mongo installation.
        home: /var/lib/mongo
        shell: /bin/false

    - name: Install mongo-2.4
      yum:
        name: "{{ item }}"
        state: present
      with_items:
        - "{{ tempdir }}/mongo-10gen-2.4.4-5.el6.x86_64.rpm"
        - "{{ tempdir }}/mongo-10gen-server-2.4.4-5.el6.x86_64.rpm"

    - name: Send configuration files.
      template:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
      with_items:
              - { src: 'templates/mongod.j2', dest: '/etc/mongod.conf' }
              - { src: 'templates/mongodb-configsvr.j2', dest: '/etc/mongodb-configsvr.conf' }

    - name: Create directories to storing logs.
      file:
        path: "{{ item }}"
        state: directory
        owner: mongod
        group: mongod
        mode: u=rwx,g=rw,o=rw
        recurse: yes
      with_items:
        - /var/log/mongodb/
        - /var/lib/mongodb/
        - /var/lib/mongodb-config/

    - name: Create files to storing logs.
      file:
        path: "{{ item }}"
        state: touch
        owner: mongod
        group: mongod
      with_items:
        - /var/log/mongodb/mongod.log

    - name: Start mongo2.4 service
      shell: "{{ item }}"
      with_items:
        - /usr/bin/mongod -f /etc/mongod.conf
        - /usr/bin/mongod -f /etc/mongodb-configsvr.conf
      ### Remove or comment once test is finished.
      ignore_errors: yes

      #name: mongod
      # state: started
      #  args: -f /etc/mongod.conf

      #### Finished Pre tasks

    - name: Check who is the master.
      script:
        cmd: bash/checkmaster.sh
        chdir: "{{ tempdir }}"
      register: master
      when: inventory_hostname == play_hosts[0]

    - name: Check who are the secondary nodes.
      script:
        cmd: bash/checksecond.sh
        chdir: "{{ tempdir }}"
      register: secondary
      when: inventory_hostname == play_hosts[0]

    - name: debug var name.
      debug:
        var: master

    - name: debug var name - second.
      debug:
        var: secondary

    - name: Populate DB. (Execute only if is a NEW INSTALLATION).
      script:
        cmd: bash/populateDB.sh
        chdir: "{{ tempdir }}"
      register: populateDB
      with_items:
        - "{{ master.stdout_lines }}"

    - name: (Before upgrade.) Execute V2.6 script to validate if is good to proceed.
      script: 
        cmd: bash/mongo2.6.sh
        chdir: "{{ tempdir }}"
      register: Mongo26
      failed_when:
        - '"Everything is ready for the upgrade!" not in Mongo26.stdout'
      with_items:
        - "{{ master.stdout_lines }}"

      # Starting with upgrade.

    - name: Upgrading on SECONDARY NODES - Stop and installing service node:1.
      shell: pgrep mongo | xargs kill -2; rpm -Uvh "{{ tempdir }}/mongo-10gen-2.6-1.el6.x86_64.rpm"; rpm -Uvh "{{ tempdir }}/mongo-10gen-server-2.6-1.el6.x86_64.rpm"
      throttle: 1
      with_items:
        - "{{ secondary.stdout_lines[0] }}"
        - "{{ secondary.stdout_lines[1] }}"

My problem is with the last task called Upgrading on secondary nodes . What I'm trying to achieve is to get the value of the listsecondary.stdoutlines to pass the last task only to those servers. The content of this variable has:

TASK [debug var name - second.] ****************************************************************************************************************************************************************************
ok: [192.168.33.2] => {
    "secondary": {
        "changed": true,
        "failed": false,
        "rc": 0,
        "stderr": "Shared connection to 192.168.33.2 closed.\r\n",
        "stderr_lines": [
            "Shared connection to 192.168.33.2 closed."
        ],
        "stdout": "\r\nmongodb02.ppc.us\r\nmongodb03.ppc.us\r\n",
        "stdout_lines": [
            "",
            "mongodb02.ppc.us",
            "mongodb03.ppc.us"
        ]
    }
}
ok: [192.168.33.3] => {
    "secondary": {
        "changed": false,
        "skip_reason": "Conditional result was False",
        "skipped": true
    }
}
ok: [192.168.33.4] => {
    "secondary": {
        "changed": false,
        "skip_reason": "Conditional result was False",
        "skipped": true
    }
}

So, I'm trying to run the last task on the nodes listed on {{secondary.stdout_lines}}. Every time that I run the playbook, with:

      with_items:
        - "{{ secondary.stdout_lines[0] }}"
        - "{{ secondary.stdout_lines[1] }}"

this task is executed on the first node (mongodb01) and not in the secondary.stdout_lines (mongodb02, mongodb03).

Has anyone any idea of how to achieve this?

Thanks in advance!

Edit: here is the new issue:

Content of inventory:

---
all:
  hosts:
    mongodb01.ppc.us:
      ansible_host: 192.168.33.2
    mongodb02.ppc.us:
      ansible_host: 192.168.33.3
    mongodb03.ppc.us:
      ansible_host: 192.168.33.4
  vars:
    ansible_python_interpreter: /usr/bin/python

Debug of tasks:

  • var: master
TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [mongodb01.ppc.us] => {
    "master": {
        "changed": true,
        "failed": false,
        "rc": 0,
        "stderr": "Shared connection to mongodb01.ppc.us closed.\r\n",
        "stderr_lines": [
            "Shared connection to mongodb01.ppc.us closed."
        ],
        "stdout": "\r\nmongodb01.ppc.us\r\n",
        "stdout_lines": [
            "",
            "mongodb01.ppc.us"
        ]
    }
}
ok: [mongodb02.ppc.us] => {
    "master": {
        "changed": true,
        "failed": false,
        "rc": 0,
        "stderr": "Shared connection to mongodb02.ppc.us closed.\r\n",
        "stderr_lines": [
            "Shared connection to mongodb02.ppc.us closed."
        ],
        "stdout": "\r\nmongodb01.ppc.us\r\n",
        "stdout_lines": [
            "",
            "mongodb01.ppc.us"
        ]
    }
}
ok: [mongodb03.ppc.us] => {
    "master": {
        "changed": true,
        "failed": false,
        "rc": 0,
        "stderr": "Shared connection to mongodb03.ppc.us closed.\r\n",
        "stderr_lines": [
            "Shared connection to mongodb03.ppc.us closed."
        ],
        "stdout": "\r\nmongodb01.ppc.us\r\n",
        "stdout_lines": [
            "",
            "mongodb01.ppc.us"
        ]
    }
}
  • debug of inventory_hostname
TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [mongodb01.ppc.us] => {
    "inventory_hostname": "mongodb01.ppc.us"
}
ok: [mongodb02.ppc.us] => {
    "inventory_hostname": "mongodb02.ppc.us"
}
ok: [mongodb03.ppc.us] => {
    "inventory_hostname": "mongodb03.ppc.us"
}
[WARNING]: Could not match supplied host pattern, ignoring: role_master

PLAY [role_master] *****************************************************************************************************************************************************************************************
skipping: no hosts matched

PLAY [role_secondary] **************************************************************************************************************************************************************************************
  • Check master node task:
TASK [check master node] ***********************************************************************************************************************************************************************************
changed: [mongodb01.ppc.us] => {"changed": true, "rc": 0, "stderr": "Shared connection to 192.168.33.2 closed.\r\n", "stderr_lines": ["Shared connection to 192.168.33.2 closed."], "stdout": "\r\nmongodb01.ppc.us\r\n", "stdout_lines": ["", "mongodb01.ppc.us"]}
changed: [mongodb02.ppc.us] => {"changed": true, "rc": 0, "stderr": "Shared connection to 192.168.33.3 closed.\r\n", "stderr_lines": ["Shared connection to 192.168.33.3 closed."], "stdout": "\r\nmongodb01.ppc.us\r\n", "stdout_lines": ["", "mongodb01.ppc.us"]}
changed: [mongodb03.ppc.us] => {"changed": true, "rc": 0, "stderr": "Shared connection to 192.168.33.4 closed.\r\n", "stderr_lines": ["Shared connection to 192.168.33.4 closed."], "stdout": "\r\nmongodb01.ppc.us\r\n", "stdout_lines": ["", "mongodb01.ppc.us"]}
2
  • Can you show us specifically what you've tried? Show us your playbook in which you're trying to use this value.
    – larsks
    Jun 8, 2020 at 16:58
  • Done! I added the complete playbook and better description :) Thanks for your help @larsks
    – barra51
    Jun 8, 2020 at 17:07

1 Answer 1

1

First, let's look at your problem.

On the first host in your play (play_hosts[0]), you're setting the secondary variable to the result of running bash/checksecond.sh. You're explicitly not doing this on any other hosts in the play. That means when you run this task...

    - name: Upgrading on SECONDARY NODES - Stop and installing service node:1.
      shell: pgrep mongo | xargs kill -2; rpm -Uvh "{{ tempdir }}/mongo-10gen-2.6-1.el6.x86_64.rpm"; rpm -Uvh "{{ tempdir }}/mongo-10gen-server-2.6-1.el6.x86_64.rpm"
      throttle: 1
      with_items:
        - "{{ secondary.stdout_lines[0] }}"
        - "{{ secondary.stdout_lines[1] }}"

...the secondary variable only has a useful value on play_hosts[0]. It will be undefined on any other hosts in the play. You don't show the output of your playbook, but I would expect this playbook to fail on your secondary nodes.

You really want to handle this sort of "run only on some nodes" behavior by having plays that only target the relevant nodes. You can great dynamic groups of nodes using the group_by module. For example, if we assume that your bash/checkmaster.sh script outputs the name of the master node, we could do something like this:

---
- hosts: all
  gather_facts: false
  tasks:
    - name: check master node
      script:
        cmd: bash/checkmaster.sh
      register: master

    - set_fact:
        is_master: "{{ master.stdout_lines[0] == inventory_hostname }}"

    - group_by:
        key: "role_{{ 'master' if is_master else 'secondary' }}"

After this play completes, you will have a group named role_master that contains the master node, and a group named role_secondary that contains the secondary nodes. You can then target these in subsequent plays like this:

- hosts: role_master
  gather_facts: false
  tasks:
    - debug:
        msg: "tasks on master node"

- hosts: role_secondary
  gather_facts: false
  tasks:
    - debug:
        msg: "tasks on secondary nodes"

I've put together a runnable example in https://github.com/larsks/so-example-barra51.

9
  • You are a life saver! Thanks!!! This is all I need! Sadly, I'm having issues at the moment of execution: ``` ok: [mongodb01.ppc.us] => { "is_master": false } ok: [mongodb02.ppc.us] => { "is_master": false } ok: [mongodb03.ppc.us] => { "is_master": false } My inventory file: --- hosts: mongodb01.ppc.us: ansible_host: 192.168.33.2 mongodb02.ppc.us: ansible_host: 192.168.33.2 mongodb03.ppc.us: ansible_host: 192.168.33.2
    – barra51
    Jun 8, 2020 at 20:44
  • You should check that the output of your checkmaster.sh script matches the assumptions I've made here (i.e. that the first line of output contains the necessary information).
    – larsks
    Jun 8, 2020 at 21:05
  • This fact is very very weird. The content of checkmaster.sh is echo $'rs.status()' | $MONGO_BIN | grep -B 3 "PRIMARY" | grep -v -E "health|state" | awk '{ print $3 }' | awk -F ':' '{ print $1 }' | sed 's/"//g' which result on mongodb01.ppc.us. When this occurs, all of the conditions result in false
    – barra51
    Jun 8, 2020 at 22:12
  • Stick a debug task in the playbook that shows both the content of the master variable and the content of inventory_hostname. Maybe add that to your question? You can't really format that usefully in a comment.
    – larsks
    Jun 8, 2020 at 22:15
  • I just updated with the new issue. I'm really appreciate your help!!! I'm sure that I'm missing something really easy, but I can't figure out what
    – barra51
    Jun 8, 2020 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.