flash
flash

Reputation: 1519

handle all the questions that is asked during Ubuntu upgrade through ansible?

I am working on upgrading my machines to Ubuntu 16 from Ubuntu 14. I have around 200+ machines. I wanted to do this through ansible so that I can automate it instead of doing it manually. When I run below command for the upgrade, it asked me some questions on the screen and I always chose default answers for those and it worked for me.

Most of the time there were some questions on the pop up screen and I answered default for those and sometimes there were questions on the command line which I answered to default 'N' when running the upgrade.

do-release-upgrade

So now if I want to do this through ansible how can we deal with those questions that pops up during the upgrade? I always want to chose default answer for those questions but is there any way to deal with this through ansible? Below is what I got but not sure how to deal with those questions.

---
- hosts: upgrade_test
  serial: "{{ num_serial }}"

  tasks:
    # Use a block to perform tasks conditionally—only if running Ubuntu 14.04.
    - block:

      - debug:
          msg: 'This server is running Ubuntu 14.04 LTS and will be upgraded to 16.04 LTS.'

      - name: Run do-release-upgrade.
        command: do-release-upgrade

      # Note: In Ansible 2.7+, use the reboot module instead.
      - name: Reboot the server.
        command: reboot
        async: 0
        poll: 0

      - name: Wait for server to reboot.
        wait_for:
          host: "{{ ansible_ssh_host }}"
          port: 22
          state: started
        connection: local
        become: no

      when: ansible_distribution == 'Ubuntu' and ansible_distribution_version == '14.04'

Upvotes: 1

Views: 950

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39159

You have two options here:

Either you script all the questions with the expect module

- name: Run do-release-upgrade.
   expect:
     command: do-release-upgrade
     responses:
       Question:
         - N
         - N
         - N
         - ...

It would maybe be the safest solution because you really control what happen and you can even regex script that the question are what you expect with the second form of the module

- name: Run do-release-upgrade.
   expect:
     command: do-release-upgrade
     responses:
        Configuration file '/etc/pam.d/login'.*: N

Still, that forces you to either script all the question or be one hundred percent sure you always have the same amount of questions.

Or you can use the utility command yes

Which answers y per default but could answer whatever you want, passed as argument

Name

yes - output a string repeatedly until killed

Synopsis

yes [STRING]...
yes OPTION

Description

Repeatedly output a line with all specified STRING(s), or 'y'.

Source: https://linux.die.net/man/1/yes

Default usage of yes

$ yes
y
y
y
y
^C

Usage of yes with argument

$ yes N
N
N
N
N
^C

So, with that, you can change you task to

 - name: Run do-release-upgrade.
   shell: yes N | do-release-upgrade

Note: you need to change from the command module to the shell module, because the command module does not accept the usage of pipes (|)

The command(s) will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", ";" and "&" will not work. Use the shell module if you need these features.

Source: https://docs.ansible.com/ansible/latest/modules/command_module.html

Upvotes: 2

Related Questions