Ansible - msg: No package matching '$item' is available - ansible

I need to set-up my server. I've the following ansible playbook.
---
- hosts: webservers
user: root
sudo: yes
tasks:
- name: add nginx ppa
action: apt_repository repo=ppa:nginx/stable state=present
- name: install common packages needed for python application development
action: apt pkg=$item state=installed
with_items:
- libpq-dev
- libmysqlclient-dev
- libxml2-dev
- libxslt1-dev
- mysql-client
- python-dev
- python-setuptools
- python-mysqldb
- build-essential
- git
- nginx
- name: install pip
action: easy_install name=pip
- name: install various libraries with pip
action: pip name=$item state=present
with_items:
- uwsgi
handlers:
- name: restart nginx
service: name=nginx state=restarted
When I run this script, following is the output
PLAY [webservers] *************************************************************
GATHERING FACTS ***************************************************************
ok: [IP]
TASK: [add nginx ppa] *********************************************************
ok: [IP]
TASK: [install common packages needed for python application development] *****
failed: [IP] => (item=libpq-dev,libmysqlclient-dev,libxml2-dev,libxslt1-dev,mysql-client,python-dev,python-setuptools,python-mysqldb,build-essential,git,nginx) => {"failed": true, "item": "libpq-dev,libmysqlclient-dev,libxml2-dev,libxslt1-dev,mysql-client,python-dev,python-setuptools,python-mysqldb,build-essential,git,nginx"}
msg: No package matching '$item' is available
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit #/home/praful/setup_server.yaml.retry
IP : ok=2 changed=0 unreachable=0 failed=1
I've referred this link for the same.
I'm new to ansible and dont understand the error, since there is no typo in the package name provided in with_items. What exactly is the error??

You need the jinja variable syntax {{item}} where you have $item.

Related

ansible - playbook error ERROR! the field 'hosts' is required but was not set

New to ansible: trying a playbook, in which there is 1 task and 1 pretask. The task is copying a file which gets executed successfully. The pretask is checking for homebrew and if not present installed. This is throwing me this error.
ERROR! the field 'hosts' is required but was not set
I have already set the hosts value in top of the playbook as localhost at i am using only local host.
I have pasted the entire playbook here:
Your help is appreciated. Thanks
---
- hosts: localhost
tasks:
- copy:
src: "~/.master.testconfig"
dest: "~/.testconfig"
- pre_tasks:
- name: Ensure Homebrew is Installed
stat:
path: /usr/local/bin/brew
register: homebrew_check
- name: Fail If Homebrew Is Not Installed
fail:
msg: Homebrew is missing.. Install from http://brew.sh/
when:
- not homebrew_check.stat.exists
- name: Installing Homebrew
shell: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
when:
- not homebrew_check.stat.exists
- install_homebrew_if_missing
Output:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [localhost] ************************************************************************************
TASK [Gathering Facts] ******************************************************************************
ok: [localhost]
TASK [copy] *****************************************************************************************
ok: [localhost]
ERROR! the field 'hosts' is required but was not set
The error is due to syntax error: You don't need - for pre_tasks. Another issue is that indentation is incorrect.
The correct syntax would be:
---
- hosts: localhost
tasks:
- copy:
src: "~/.master.testconfig"
dest: "~/.testconfig"
pre_tasks:
- name: Ensure Homebrew is Installed
stat:
path: /usr/local/bin/brew
register: homebrew_check
- name: Fail If Homebrew Is Not Installed
fail:
msg: Homebrew is missing.. Install from http://brew.sh/
when: not homebrew_check.stat.exists
- name: Installing Homebrew
shell: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
when:
- not homebrew_check.stat.exists
- install_homebrew_if_missing

Can't install python, pip related apps through Ansible

I am using below ansible yml file to install python, pip, etc.
roles/python/main.yml:
---
- name: python
apt:
pkg: python
- name: python-pip
apt:
pkg: python-pip
- name: mongopy
pip:
pkg: mongopy
- name: mtools
pip:
pkg: mtools
when I run ansible-playbook on this script, I get below
PLAY [ec2] ***********************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************
ok: [xxxxx.ap-southeast-2.compute.amazonaws.com]
PLAY RECAP ***********************************************************************************************************************************************************************************************
xxxxxap-southeast-2.compute.amazonaws.com : ok=1 changed=0 unreachable=0 failed=0
there is no error on them but I checked these apps are not installed on the remote host. What wrong with my yml file? Is there any place I can check what the error is?
below is my playbook:
python.yml:
---
- hosts: ec2
remote_user: ubuntu
roles:
- python
below is the command I run:
ansible-playbook -i hosts python.yml
There are no tasks in your python role. Please have a look at the role structure.
If roles/x/tasks/main.yml exists, tasks listed therein will be added to the play
Tasks file (main.yml) should be placed in the tasks subdirectory of the role, not in the main role's directory.
And this has nothing to do with how you described the problem (installing Python or Pip). Even if you replaced the tasks with a single debug task which displays Hello world by default, it would not run.

No package matching 'mesos' is available on Ansible

I am configuring a mesos-marathon cluster.
I have the next role to install java and mesos.
---
- name: importar key Mesosphere
shell: gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E56151BF
- name: ppa java8
apt_repository: repo='ppa:webupd8team/java' state=present
- name: seleccionar licencia Oracle
shell: echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections
- name: actualizar
apt: update_cache=yes
- name: instalar java8
apt: name=oracle-java8-installer state=latest update-cache=yes force=yes
- name: actualizar sources list
shell: DISTRO=$(lsb_release -is | tr '[:upper:]' '[:lower:]') && CODENAME=$(lsb_release -cs) && echo "deb http://repos.mesosphere.io/${DISTRO} ${CODENAME} main" | sudo tee /etc/apt/sources.list.d/mesosphere.list
- name: actualizar paquetes
apt: update_cache=yes cache_valid_time=3600
- name: instalar mesos
apt: name=mesos state=present install_recommends=yes force=yes
- name: instalar mesosphere
apt: name=mesosphere state=present install_recommends=yes force=yes
My problem is that when I execute the playbook, it gives me the next error:
TASK [common : actualizar sources list] ****************************************
changed: [172.16.8.191]
TASK [common : actualizar paquetes] ********************************************
ok: [172.16.8.191]
TASK [common : instalar mesos] *************************************************
fatal: [172.16.8.191]: FAILED! => {"changed": false, "failed": true, "msg": "No package matching 'mesos' is available"}
PLAY RECAP *********************************************************************
172.16.8.191 : ok=8 changed=5 unreachable=0 failed=1
But if I execute ansible for a second time it works perfectly you can see executing a second time:
TASK [common : actualizar paquetes] ********************************************
ok: [172.16.8.191]
TASK [common : instalar mesos] *************************************************
changed: [172.16.8.191]
TASK [common : instalar mesosphere] ********************************************
changed: [172.16.8.191]
What could be the problem?
Thanks.
SOLUTION BY #ydaetskcoR
Change the task 'instalar mesos':
- name: instalar mesos
apt: name=mesos state=present install_recommends=yes update_cache=yes force=yes
The issue you have is that the actualizar paquetes task is only doing an apt-get update to refresh your repo lists if the last update was more than an hour ago.
Considering you've only just added the Mesos repo in the previous task you won't then be able to find the package. Re-running the playbook triggers the actualizar task before that which doesn't have a cache_valid_time setting and so will force an apt-get update which will then allow you to use the Mesos repo that you added in the last playbook run.
To fix it you could just remove the cache_valid_time from the actualizar paquetes task.
As mentioned in the comments, you can also move the update_cache only apt tasks into the main apt task that actually installs packages and Ansible will run the apt-get update before the apt-get install.

Ansible playbook error

I am testing with ansible, what I'm trying to do is install apache2 on another ubuntu server, I already have the group "test" defined with 1 ip. but what happens is that ansible throws me some errors when executing it, I've searched a lot of sites and a lot of people have had this issue, but on different situations and I amd starting to get frustrated with it. Can somebody help me?
Ansible Playbook:
---
- hosts: test
sudo: yes
tasks:
- name: Check if Im sudo
command: echo $USER
- name: install packages
apt: name:apache2 update_cache=yes state=latest
notify: start apache2
handlers:
- name: start apache2
service: name=apache2 state=started
STDOUT
root#ip-172-31-35-33:/etc/ansible/example# ansible-playbook example.yml
PLAY [test] *******************************************************************
GATHERING FACTS ***************************************************************
ok: [172.31.36.176]
TASK: [Check if Im sudo] ******************************************************
changed: [172.31.36.176]
TASK: [install packages] ******************************************************
failed: [172.31.36.176] => {"failed": true}
msg: this module requires key=value arguments (['name:apache2', 'update_cache=yes', 'state=latest'])
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit #/root/example.retry
172.31.36.176 : ok=2 changed=1 unreachable=0 failed=1
BTW, the host is reachable, I can ssh into it, even with ansible, this is the proof
root#ip-172-31-35-33:/etc/ansible/example# ansible -m shell -a "ifconfig | grep 'inet addr'" test
172.31.36.176 | success | rc=0 >>
inet addr:172.31.36.176 Bcast:172.31.47.255 Mask:255.255.240.0
inet addr:127.0.0.1 Mask:255.0.0.0
another thing is that I'm able to install apache2 by hand on the other server, BUT IT IS NOT INSTALLED BECAUSE I WANT TO INSTALL IT USING ANSIBLE
Thanks
Within an individual task, Ansible requires you to make the choice between standard YAML syntax and their own parsed version with equals signs. In this task, you are mixing the two:
- name: install packages
apt: name:apache2 update_cache=yes state=latest
notify: start apache2
This could be either written:
- name: install packages
apt:
name: apache2
update_cache: yes
state: latest
notify: start apache2
Or:
- name: install packages
apt: name=apache2 update_cache=yes state=latest
notify: start apache2
YAML also allows for using bracket and comma syntax to allow you to specify your key-value information on the same line:
- name: install packages
apt: {name: apache2, update_cache: yes, state: latest}
notify: start apache2
Any of these are valid.
You're using a colon where an equal is needed. You need to change the name:apache2 to name=apache2.

Ansible Roles and handlers - Cannot get role handlers to work

I need to set up Apache/mod_wsgi in Centos 6.5 so my main YAML file is as such:
---
- hosts: dev
tasks:
- name: Updates yum installed packages
yum: name=* state=latest
- hosts: dev
roles:
- { role: apache }
This should update all yum-installed packages then execute the apache role.
The apache role is configured to install Apache/mod_wsgi, set Apache to start at boot time and restart it. The following are the contents of roles/apache/tasks/main.yml:
---
- name: Installs httpd and mod_wsgi
yum: name={{ item }} state=latest
with_items:
- httpd
- mod_wsgi
notify:
- enable httpd
- restart httpd
And the handlers in roles/apache/handlers/main.yml:
---
- name: enable httpd
service: name=httpd enabled=yes
- name: restart httpd
service: name=httpd state=restarted
The handlers do not seem to run since the following output is given when I execute the playbook:
PLAY [dev] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [dev.example.com]
TASK: [Updates yum installed packages] ****************************************
ok: [dev.example.com]
PLAY [dev] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [dev.example.com]
TASK: [apache | Installs httpd and mod_wsgi] **********************************
ok: [dev.example.com] => (item=httpd,mod_wsgi)
PLAY RECAP ********************************************************************
dev.example.com : ok=4 changed=0 unreachable=0 failed=0
And when I vagrant ssh into the virtual machine, sudo service httpd status shows httpd is stopped and sudo chkconfig --list shows it has not been enabled to be started by init.
I'm just starting out with Ansible, so is there something obvious I could be missing?
Well, to answer my own question, I realized that there's a subtle point I missed:
http://docs.ansible.com/playbooks_intro.html#handlers-running-operations-on-change
Specifically, the notify signal is produced only if the task introduces a change. So for my use case I think I'll go with enabling and starting Apache in standalone tasks instead of relying on change signal handlers.

Resources