expect module does not like an int I am passing in

All, I did some searching here and did not find this specific issue. I am trying to use the expect module to work with fdisk to automatically provision new space on VMs.

tasks:

  • name: “DISK | Create a partition for the added disk space”
    expect:
    command: fdisk /dev/sda
    responses:
    ‘Command (m for help*’:
  • n
  • t
  • w
    ‘Select (default.$': p
    'Partition number.
    $’: “{{ next_part }}”
    ‘First sector.$': \r
    'Last sector.
    $’: \r
    ‘Hex code.*$’: 08e

What i get is:

TASK [DISK | Create a partition for the added disk space] **********************
fatal: [p-db-rhel7-01]: FAILED! => {“changed”: false, “failed”: true, “module_stderr”: “Shared connection to XX.4.XXX.144 closed.\r\n”, “module_stdout”: “Traceback (most recent call last):\r\n File "/tmp/ansible_z0_ZP0/ansible_module_expect.py", line 237, in \r\n main()\r\n File "/tmp/ansible_z0_ZP0/ansible_module_expect.py", line 149, in main\r\n response = u’%s\n’ % value.rstrip(‘\n’).decode()\r\nAttributeError: ‘int’ object has no attribute ‘rstrip’\r\n”, “msg”: “MODULE FAILURE”}

I finally figured out that the next_part variable I am passing in is the number of the partition I want to create (3 in this case), and I think this is complaining because there is no “rstrip” function for integers. However, I am sure peopole have used this module to great effect with responses that are integers. So, any help?

Thanks

You could try to filter it to a string
"{{ next_part | string }}"

Thanks, I’ll have to try that. I actually solved it where I create the variable. What I had in host_vars/node was:

next_part = 3

If I change it to the following it seemed to fix it.

next_part = ‘3’

Your solution is making use of a feature of jinja, right?

Yes, Jinja2 filters.