not able to echo value of hostvars in another host

28 views
Skip to first unread message

Kunalsing Thakur

unread,
Jul 30, 2019, 10:12:54 AM7/30/19
to Ansible Project
---
- name: RollBack  Rpm versions for  {{ service_name }}
  hosts: local
  connection: local
  gather_facts: false
  tasks:
    - name: Getting host list from ipa
      shell: /etc/ansible/playbooks/hostfind.sh
    - name: Creating in memory inventory group for {{ nodetype }} in ansible playbook itself
      shell: cat /etc/ansible/hosts | grep {{ nodetype }} | grep {{ env }}
      register: host_list
    - add_host:
        name: "{{ item }}"
        group: "{{ nodetype }}"
      with_items: "{{ host_list.stdout_lines }}"

- name: Rollback Execution start on {{ nodetype }}
  hosts: "{{ nodetype }}"
  connection: ssh
  gather_facts: false
  any_errors_fatal: false
  user: test
  become: yes
  serial: 1
  tasks:
    - name: Rolling back  service on {{ inventory_hostname }}
      block:
        - name: Downgrading Rpm {{ service_name }}
          shell: yum downgrade "{{ service_name }}" -y
        - name: Getting Version of  {{ service_name }} in {{ inventory_hostname }}
          shell: rpm --queryformat "%{VERSION}-%{RELEASE}" -q "{{ service_name }}"
          register: downgraderpmversion
        - name: Restarting {{ service_name }} service on {{ inventory_hostname }}
          include_tasks: /etc/ansible/inventories/playbooks/common/service_restart.yaml
          with_items:
            - { servicename: '{{ service_name }}' }
        - name: wait for 60 second to come up the service {{ service_name }} on {{ inventory_hostname }}
          wait_for:
            port: "{{ port_num }}"
            host: '{{ inventory_hostname }}'

- name: Updating Rpm versions for {{ service_name }}
  hosts: local
  connection: local
  gather_facts: false
  tasks:
    - debug:
        var: hostvars[groups['{{ nodetype }}'][0]]['downgraderpmversion']['stdout']
    - shell: echo hostvars[groups['{{ nodetype }}'][0]]['downgraderpmversion']['stdout']

Kunalsing Thakur

unread,
Jul 30, 2019, 10:15:12 AM7/30/19
to Ansible Project
The following Result:-

TASK [debug] *******************************************************************
task path: /etc/ansible/playbooks/test.yaml:59
ok: [127.0.0.1] => {
    "hostvars[groups['msliv'][0]]['downgraderpmversion']['stdout']": "5.42.0-201809042105.733e1aa"
}

TASK [shell] *******************************************************************
task path: /etc/ansible/playbooks/test.yaml:61
changed: [127.0.0.1] => {"changed": true, "cmd": "echo hostvars[groups['msliv'][0]]['downgraderpmversion']['stdout']", "delta": "0:00:01.219563", "end": "2019-07-30 14:14:25.187306", "rc": 0, "start": "2019-07-30 14:14:23.967743", "stderr": "", "stderr_lines": [], "stdout": "hostvars[groups[msliv][0]][downgraderpmversion][stdout]", "stdout_lines": ["hostvars[groups[msliv][0]][downgraderpmversion][stdout]"]}

Kunalsing Thakur

unread,
Jul 31, 2019, 12:28:59 AM7/31/19
to Ansible Project
Guies Anyone help one this.
Reply all
Reply to author
Forward
0 new messages