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.
Related
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.
I have 3 VM's for ansible as follows:
master
host1
host2
ansible is installed and configured correctly. no issue with that part.
So I have this command I need to run. Before going to execute this command using ansible, I will show you how to do this without ansible :
I want to install update the packages first, so I am logged into the VM as a normal user (ubuntut) and execute like this:
sudo apt-get update -y
As you can see, I have executed a sudo command when I am logged in as the ubuntu user but not as a root user.
So above command is executed with sudo but user is ubuntu
I want this scenario to be done using ansible.
My Question: How to execute a command that contains sudo using ansible but when logged in as a different user
So my playbook will look like this:
- hosts: all
tasks:
- name: Update and upgrade apt packages
become: ubuntu
apt:
upgrade: yes
update_cache: yes
But where can I add the sudo part in above playbook ? does it add automatically?
What if I want to execute a command like this, logged in user as ubuntu:
sudo apt-get install docker
How can I do this sudo part in ansible while become: ubuntu
To invoke a command as sudo, You can do something like this :
- hosts: all
tasks:
- name: Update and upgrade apt packages
become: ubuntu
# this will fail because /opt needs permissions
# shell: echo hello > /opt/testfile
# this will work
shell: "sudo sh -c 'echo hello > /opt/testfile'"
# use this to verify output
register: out
- debug:
msg: "{{ out }}"
NOTE: you will get this warning:
"Consider using 'become', 'become_method', and 'become_user' rather
than running sudo"
The following will work, you can find a detailed explanation and examples in ansible documentation.
[1]https://docs.ansible.com/ansible/latest/user_guide/become.html
- hosts: all
tasks:
- name: Update and upgrade apt packages
become: true
become_user: ubuntu
shell: echo hello > /opt/testfile
# use this to verify output
register: out
- debug:
msg: "{{ out }}"
I have a problem with my ansible playbook. Normally i use the console to upgrade a program.
After the dependency check i have to answer with yes. I checked the Shell documentation,
Now i scripted that with Ansible, and the task is pending because no user can do an interaction.
As example this is the part of my testplaybook which is not working.
# Uninstall tree
- name: uninstall
shell: yum remove tree
My question is now how can i submit a decision, as example yes or no in my playbook.
ps: I know i can set the yum module to the state: absent but this is basd on the yum module.
pps: No i do not want to send the -y paramenter in the shell. This does not solve all my problems
For Yum, use the Yum module: https://docs.ansible.com/ansible/latest/modules/yum_module.html
However, your answer can be found here: https://docs.ansible.com/ansible/latest/modules/expect_module.html
Long story short my PC died while I was running Vagrant and when I got power back my subsequent vagrant up attempted to rebuild the box. In doing so I got an error as the subject is titled:
ERROR: yum is not a legal parameter in an Ansible task or handler
I've tried double checking the ansible documentation, checking my structure and indentation, saving in different text editors etc but the error persists. I'm stuck as there was no issue previously so I am a bit stumped as to why it's no longer working.
My playbook is as follows, though i've cut a lot of it out for now while I resolve the issue:
---
- hosts: all
sudo: yes
tasks:
- name: Update yum packages
yum: name=* state=latest
Many thanks!
It turns out that I was missing a return after tasks.
---
- hosts: all
sudo: yes
tasks:
- name: Update yum packages
yum: name=* state=latest
However ansible playbook docs: http://docs.ansible.com/ansible/playbooks_intro.html examples show that no return is required after the tasks declaration.
Perhaps a nuance with my particular version (1.7.2)
The issue: When I try to install a package ansible does not proceed. The CLI just sits there idle.
SSH is cofigured to connect with out prompting for a password. I have created a user called "test" and my sudoers file has the following configuration:
test ALL=(ALL) NOPASSWD:ALL
Also in /etc/ansible/ansible.cfg
inventory = /etc/ansible/hosts
#library = /usr/share/my_modules/
remote_tmp = $HOME/.ansible/tmp
pattern = *
forks = 5
poll_interval = 15
sudo_user = root
#ask_sudo_pass = True
#ask_pass = True
transport = smart
#remote_port = 22 module_lang = C
When as user "test" I do
yum install lynx
the package specified gets installed.
But if I do
ansible local -s -m shell -a 'yum install lynx'
Nothing happens.
I am not sure what is going on :(
Try using the yum module instead:
ansible local -s -m yum -a 'name=lynx state=present'
You have to say "yes" to yum:
Try this instead:
ansible local -s -m shell -a 'yum install lynx -y'
You should be using either the yum module or even better, the package module which is OS generic.
On the other hand, you other option is the raw module which runs an SSH-dirty command against your nodes!
This is an example for using the package module in a task:
----
- hosts: <hosts_names>
sudo: yes
tasks:
- name: install lynx
register: result
package: name=lynx state=latest
become: yes
become_user: root
become_method: sudo
ignore_errors: True
Using the above playbook, hopefully helps you to install you desired package and even if it did not do that, you'll be able to easy go through the result variable and see what's going wrong.
And of course before all these, you should've made sure that your YUM repository/ies work propeprly!