Ansible cant run any command or shell - ansible

I have troubles running any command or shell on my RPi.
When I use the following code:
- name: Example command
ansible.builtin.command:
cmd: "cat /etc/motd"
I get this error:
Unsupported parameters for (ansible.builtin.command) module: cmd Supported parameters include: _raw_params, _uses_shell, argv, chdir, creates, executable, removes, stdin, stdin_add_newline, strip_empty_ends, warn"}
When i try this:
- name: Example command
ansible.builtin.command: cat /etc/motd
I get this error:
ERROR! this task 'ansible.builtin.command' has extra params, which is only allowed in the following modules: import_tasks, raw, include, include_tasks, include_vars, include_role, script, set_fact, win_command, add_host, shell, import_role, group_by, command, win_shell, meta
I get the same errors when I try to use ansible.builtin.shell. I tried several other commands but without any luck. It seems I cant run ANY command without these 2 errors.
I use ansible 2.9.6. I tried to upgrade it but apt said its the newest available.
Any help would be appreciated.

Replace ansible.builtin.command with command. Your version of Ansible is too old for the newer syntax. This works with Ansible 2.9.6:
- hosts: localhost
gather_facts: false
tasks:
- command: cat /etc/motd

I fixed this by removing the version of Ansible that was installed by apt and then installing it again via pip by following the install instructions on the Ansible docs website.

Related

Ansible script to uninstall multiple versions of OMS agent

I wanted to do an uninstallation of OMS agent in our Linux machines. Unfortunately, we do have different OMS agent versions assigned to each machine. I hard coded the version from my Ansible script
command: sudo {{ file_path }}/omsagent-1.13.9-0.universal.x64.sh —-purge
It only works for machine with that same OMS agent version else, it will fail.
I tried adding wildcard syntax, but it is getting an error stating that command not found
stderr: “sudo :/home/filename/omsagent-* : command not found
if I changed my previous command to
command: sudo {{file_path}}/omsagent-*.universal.x64.sh —-purge
Since I do not have this specific agent in place, I can't provide a full tested working example, but some some guidance.
According the package documentation and All bundle operations, the bundle has an option for
--version-check Check versions already installed to see if upgradable.
which should provide the installed version. Furthermore any installed agent has an directory with service control script
/opt/microsoft/omsagent/bin/service_control ...
and probably others, like scxadmin --version. By executing one or the other it should be possible to gather the correct installed version of the agent.
- name: Gather installed OMS agent version
become: true
become_method: sudo
shell:
cmd: /opt/microsoft/omsagent/bin/service_control status | grep <whatever is necessary to get the version string only>
register: VERSION
changed_when: false
check_mode: false
Please take note that instead of using sudo within the command, you should use become. Since it is a version reporting task only, you should also use changed_when and check_mode.
After the correct version is gathered you use it like
- name: Purge installed OMS agent version
become: true
become_method: sudo
shell:
cmd: "omsagent-{{ VERSION }}.universal.x64.sh —-purge"
Is there any reason why the option --upgrade or --force can`t be used?
You may also have a look into How to troubleshoot issues with the Log Analytics agent for Linux, there is a standalone versionless purge script available.

How can I avoid getting a .travis.yml file automatically created inside my ansible role?

I have created several roles with ansible-galaxy init my_role and after a while I realized a .travis.yml file was created automatically for a basic syntax testing. I am not planning to use Travis CI, so this .travis.yml file is useless.
I am sure there is a good reason why this occurred, so if you may also explain it I would be thankful.
This is the file:
---
language: python
python: "2.7"
# Use the new container infrastructure
sudo: false
# Install ansible
addons:
apt:
packages:
- python-pip
install:
# Install ansible
- pip install ansible
# Check ansible version
- ansible --version
# Create ansible.cfg with correct roles_path
- printf '[defaults]\nroles_path=../' >ansible.cfg
script:
# Basic role syntax check
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
It's part of the default role skeleton. If you want to use a different skeleton, pass it to ansible-galaxy using the --role-skeleton flag:
ansible-galaxy init --role-skeleton ~/ansible-skeletons/role roles/foo

how to get commands from ansible playbook

Is there a simple way how to get back what precisely ansible script(playbook) is trying to do?
E.g. I have
- name: Install required packages
yum: name={{item}} state=present
with_items:
- nmp
become: True
And I want to get:
sudo yum install nmp
That said, I want to know what commands (OS level) ansible is running.
Basically, I need the reverse process to: Ansible and Playbook. How to convert shell commands into yaml syntax?
Summarizing here the central points from comments above [1][2].
Ansible rarely runs external commands, it's mostly Python code & Python libraries being used to control hosts. Therefor, Ansible's "translation" isn't necessarily converting yum (Ansible module) usage into yum (CLI) invocations.
While you may be able to depend on the output, in some cases, by parsing for command sequences in the output of ansible-playbook -vvv foo.yml - the implementation would be flaky at best.
I'd encourage you to keep discussing on this thread what you're trying to accomplish. It's likely there is already a solution, and someone can point you to a tool that already exists.

ERROR! conflicting action statements (expect, command) in Ansible

I'm trying to install java on several hosts with Ansible.
I looked for some examples of expect module to provide answers to the prompts.
I think this syntax is quite fine:
- hosts: datanode
sudo: yes
sudo_user: root
tasks:
- expect:
name: install java jdk 7
command: apt-get install openjdk-7-jdk
responses:
Question:
'Do you want to continue? [Y/n]': 'Y'
But when I try to execute ansible-playbook file.yml I receive the error:
ERROR! conflicting action statements (expect, command)
The error appears to have been in '/root/scp.yml': line 5, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- expect:
^ here
Where is the problem?
(I have installed ansible 2.0.1.0, pexpect, python)
Thanks!
NOTE that Ansible works with yaml files, and this kind of files are indented. This means that the spaces you put before each statement are important to let Ansible to understand how are they nested. More info about yaml.
Corrected task:
- hosts: datanode
sudo: yes
sudo_user: root
tasks:
- name: install java jdk 7
expect:
command: apt-get install openjdk-7-jdk
responses:
Question:
- 'Y'
- 'n'
This will avoid your syntax error.
Source: http://docs.ansible.com/ansible/expect_module.html
Alternatively, if you always want to say "yes" to your apt-get install commands, you can add the -y argument:
apt-get install -y openjdk-7-jdk
Or even better, use the apt Ansible module.

In Ansible, how can I accept a WebSphere MQ licensing agreement as part of a playbook?

I'm in the process of converting a manual server provisioning process into a Ansible playbook. Part of the process involves installing WebSphere MQ client. One step involves manually running a script, mqlicense.sh, and accepting a licensing agreement in response to a prompt. How can I accomplish this in Ansible?
If I run the script, the ansible process hangs. If I skip that step, I get the following error:
ERROR: Product cannot be installed until the license
agreement has been accepted.
Run the 'mqlicense' script, which is in the root
directory of the install media, or see the
Quick Beginnings book for more information.
Update
Further Googling brought me to this ibm.com page, where it states:
If want to accept the license without it being displayed, you can run
the mqlicense.sh script with the -accept option.
./mqlicense.sh -accept
However, that does not seem to work in my case. When I run that command from the command line, the interactive prompt still comes up.
The issue turned out to be with the mqlicense.sh script. Apparently, it uses some syntax incompatible with bash. So when I ran it on my Debian server, the script complained:
./mqlicense.sh: 99: ./mqlicense.sh: [[: not found
The solution, alluded to in this ibm.com forum thread, was to install the korn shell (ksh), and use that to accept the license. The following tasks were included in my playbook:
Include ksh with system packages:
- name: Install debian packages
apt: pkg=${item} state=installed
with_items:
- alien
- ksh
Call the script using ksh:
# Need to run this with ksh; script syntax is not bash-compliant
- name: Accept MQ Client license
command: ksh mqlicense.sh -accept chdir=${vsphere_wd}
Ansible playbook can be found here:
https://gist.github.com/klenwell/cdf2dc92ddfe2e7e18fc

Resources