user1256378
user1256378

Reputation: 742

Ansible Simulate "Press Enter" for shell script

I am trying to run this script:

/usr/share/postgresql-common/pgdg/apt.postgresql.org.sh

When I run it without Ansible I get this:

This script will enable the PostgreSQL APT repository on apt.postgresql.org on
your system. The distribution codename used will be focal-pgdg.

Press Enter to continue, or Ctrl-C to abort. 

I tried this and some other variations on this.

 - name: 2) Run the PostgreSQL repository setup script
      become: yes
      expect:
        command: /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
        responses:
          Question:
            - This script will enable the PostgreSQL APT repository on apt.postgresql.org on
            - your system. The distribution codename used will be focal-pgdg.
            -
            - Press Enter to continue, or Ctrl-C to abort.: echo -e '\n\n'
        timeout: 30

Output:

fatal: [eos-test2]: FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "cmd": "/usr/share/postgresql-common/pgdg/apt.postgresql.org.sh",
    "delta": "0:00:30.137319",
    "end": "2022-10-06 14:09:16.421921",
    "invocation": {
        "module_args": {
            "chdir": null,
            "command": "/usr/share/postgresql-common/pgdg/apt.postgresql.org.sh",
            "creates": null,
            "echo": false,
            "removes": null,
            "responses": {
                "Question": [
                    "This script will enable the PostgreSQL APT repository on apt.postgresql.org on",
                    "your system. The distribution codename used will be focal-pgdg.",
                    null,
                    {
                        "Press Enter to continue, or Ctrl-C to abort.": "echo -e '\\n\\n'"
                    }
                ]
            },
            "timeout": 30
        }
    },
    "msg": "command exceeded timeout",
    "rc": null,
    "start": "2022-10-06 14:08:46.284602",
    "stdout": "This script will enable the PostgreSQL APT repository on apt.postgresql.org on\r\nyour system. The distribution codename used will be focal-pgdg.\r\n\r\nPress Enter to continue, or Ctrl-C to abort.",
    "stdout_lines": [
        "This script will enable the PostgreSQL APT repository on apt.postgresql.org on",
        "your system. The distribution codename used will be focal-pgdg.",
        "",
        "Press Enter to continue, or Ctrl-C to abort."
    ]
}

The script itself has

if [ -z "${YES:-}" ]; then
    echo -n "Press Enter to continue, or Ctrl-C to abort."
    read enter
    echo
fi

I was thinking of modifying the original script.

Any suggestions?

Thank you in advance.

Upvotes: 5

Views: 1102

Answers (2)

larsks
larsks

Reputation: 311377

If you look at that /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh script, you'll see at the beginning:

while getopts "c:f:h:ipstv:y" opt ; do
    case $opt in
        c) COMPONENTS="main $OPTARG" ;; # make these extra components available
        f) SOURCESLIST=$OPTARG ;; # sources.list filename to write to
        h) HOST="$OPTARG" ;; # hostname to use in sources.list
        i) INSTALL="yes" ;; # install packages for version given with -v
        p) PURGE="yes" ;; # purge existing postgresql packages
        s) DEB_SRC="deb-src" ;; # include source repository as well
        t) PGDG="pgdg-testing" ;; # use *-pgdg or *-pgdg-testing
        v) PGVERSION="$OPTARG" ;; # set up sources.list to use this version (useful for beta/devel packages)
        y) ;; # don't ask for confirmation
        *) exit 5 ;;
    esac
    YES="yes" # don't ask for confirmation if any option is given
done

This tells us (explicitly, via the comment) that the script won't prompt if you specify any command line options:

- name: install postgres
  command: /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -i -v 14

Furthermore, based on the code you show in your question, you could just set the YES environment variable to a non-empty value to suppress prompting:

- name: install postgres
  environment:
    YES: yes
  command: /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh

Upvotes: 6

Related Questions