ansible 1.9.4 to 2.4.1.0 migration issue

Hi all,

I have been avoiding to upgrade Ansible 1.9.4 on Centos 6 for the past few months, however since the upgrade project is again on my desk, I am actively working through the warnings and errors e.g. substituting include: statements in tasks with import_tasks:, editing playbooks and removing duplicate when: statements and so on.

There is one issue that I cannot resolve, it seems. I maintain a list of vars for each server in the event that I need to rebuild the server ( NODES file below ). It essentially is a list of settings for each server like sshd extra options, interface and cacti settings and so on. The issue is that in 2.4 I seem not to be able to run this playbook as I was able in 1.9.4. I see that jinja2 in when statement gets a warning.

Any ideas on how I would be able to run this playbook ? I am willing to redo the nodes file if there is a better way to do this. Looks like jinja2 in when statements is not supported any longer ?
Thank you

Version details:

root [/etc/ansible/deployment]# rpm -q ansible
ansible-2.4.1.0-2.el6.noarch

root [/etc/ansible/deployment]# rpm -qa | egrep jinja
python-jinja2-26-2.6-3.el6.noarch
python-jinja2-2.7.2-1.el6.noarch # <— This was needed for filters

root [/etc/ansible/deployment]# rpm -q centos-release
centos-release-6-9.el6.12.3.x86_64

VARS file

hname: “‘{{ inventory_hostname }}’”

hostname_hostname: “{{inventory_hostname}}”
hostname_shortname: “{{inventory_hostname.split(‘.’)[0]}}”

remove dash from host name as Ansible cannot handle it properly in vars

hostname_mounts: “{{hostname_shortname.replace(‘-’,‘’)}}”
hostname_short: “{{hostname_shortname.replace(‘-’,‘’)}}”

NODES file

nodes:
umsrv3s1: # <— short hostname
ifconfig: # <— interface info

  • { iface: ‘eth0’, IP: ‘91.X.Y.Z1’, netmask: ‘255.255.255.0’, gw: ‘91.X.Y.G1’, enabled: ‘1’ }
    mounts:
  • {source: ‘proc’, target: ‘/proc’, opts: ‘defaults,hidepid=1’, fstype: ‘proc’}
    cacti:
  • { enabled: ‘1’, tree: ‘Citi1’, template: ‘ucd/net SNMP Host’ }
    httpd:
  • { enable: false }
    yumextra: # <— extra packages for this server only
  • { packages: [ ansible-inventory-puppetdb, ansible-cmdb, VMware-vSphere-CLI, python-psphere, python-sphere, python-pyvmomi ] }
    sshdextra: # <— extra sshd options for this server
  • { name: ‘MaxSessions’, value: ‘6’}
  • { name: ‘MaxStartups’, value: ‘100:30:150’}

DisableForwarding added in 7.4

  • { name: ‘DisableForwarding’, value: ‘yes’}

umsrv3s2:
ifconfig:

  • { iface: ‘eth0’, IP: ‘91.X.Y.Z2’, netmask: ‘255.255.255.0’, gw: ‘91.X.Y.G1’, enabled: ‘1’ }
    mounts:
  • {source: ‘proc’, target: ‘/proc’, opts: ‘defaults,hidepid=1’, fstype: ‘proc’}
    cacti:
  • { enabled: ‘1’, tree: ‘City1’, template: ‘ucd/net SNMP Host’ }
    yumextra:
  • { packages: }
    sshdextra:
    httpd:
  • { enable: false }
    […]

TASKS

  • name: setfacts - set hostname vars
    set_fact:
    hostname_short_nodash: “{{ inventory_hostname.split(‘.’)[0].replace(‘-’,‘’) }}”
    hostname_short: “{{inventory_hostname.split(‘.’)[0]}}”
    tags: [ setfacts ]

  • name: yum - set_fact - get list of host-specific packeges to be installed
    set_fact:
    __packages: “{{ item.packages }}”
    with_items: nodes.{{hostname_mounts}}.yumextra
    when: nodes.{{hostname_mounts}}.yumextra is defined
    tags: [ setfacts, yum ]

  • name: yum - echo __packages
    debug: var=__packages
    when: nodes.{{hostname_mounts}}.yumextra is defined
    tags: [ setfacts, yum ]

  • name: yum - set_fact - get list of host-specific packeges to be installed
    set_fact:
    __packages: “{{ item.packages }}”
    with_items: nodes.{{hostname_mounts}}.yumextra
    when: nodes.{{hostname_mounts}}.yumextra is defined
    tags: [ setfacts, yum ]

  • name: yum - echo __packages
    debug: var=__packages
    when: nodes.{{hostname_mounts}}.yumextra is defined
    tags: [ setfacts, yum ]

/usr/bin/ansible-playbook -l hostname -f -c local /etc/ansible/deployment/deployment.yml --vault-password-file .vault --tags=setfacts
[…]

TASK [yum - set_fact - get list of host-specific packeges to be installed] *********************************************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: nodes.{{hostname_mounts}}.yumextra is defined

fatal: [umsrv10]: FAILED! => {“failed”: true, “msg”: “The task includes an option with an undefined variable. The error was: ‘ansible.utils.unsafe_proxy.AnsibleUnsafeText object’ has no attribute ‘packages’\n\nThe error appears to have been in ‘/etc/ansible/deployment/tasks/setfacts.yml’: line 49, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# YUM #\n- name: yum - set_fact - get list of host-specific packeges to be installed\n ^ here\n\nexception type: <class ‘ansible.errors.AnsibleUndefinedVariable’>\nexception: ‘ansible.utils.unsafe_proxy.AnsibleUnsafeText object’ has no attribute ‘packages’”}

Try nodes[hostname_mounts][“yumextra”]

-Toshio

Toshi,

Thank you for pointing me in the right direction, I was able to move forward with this, for posterity this is what I ended up with:

  • hosts: all
    gather_facts: yes
    become: no

vars:
hostname_shortname: “{{inventory_hostname.split(‘.’)[0]}}”
hostname_mounts: “{{hostname_shortname.replace(‘-’,‘’)}}”

nodes:
umsrv10:
packages: [ drssl, ansible-cmdb ]

tasks:

  • name: yum - echo __packages
    debug: var=“{{item}}”
    with_items: nodes[hostname_mounts][“packages”]

  • name: yum - install main rpms
    yum: name={{item}} state=installed
    with_items:

  • “{{nodes[hostname_mounts][‘packages’]}}”

root@umsrv10[/etc/ansible/playbooks]# /usr/bin/ansible-playbook -l hostname -f -c local /etc/ansible/playbooks/setfacts3.yml

PLAY [all] *****************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************************
ok: [umsrv10.sec.hostopia.com]

TASK [yum - echo __packages] ***********************************************************************************************************************************************************************************
ok: [umsrv10*****] => (item=nodes[hostname_mounts][“packages”]) => {
“item”: “nodes[hostname_mounts]["packages"]”,
“nodes[hostname_mounts]["packages"]”: [
“drssl”,
“ansible-cmdb”
]
}

TASK [yum - install main rpms] *********************************************************************************************************************************************************************************
ok: [umsrv10.sec.hostopia.com] => (item=[u’drssl’, u’ansible-cmdb’])

PLAY RECAP *****************************************************************************************************************************************************************************************************
umsrv10***** : ok=3 changed=0 unreachable=0 failed=0