Ansible playbook error - ansible

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.

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

Why does creating a droplet via Ansible fail complaining about "dopy" dependency?

I'm trying to create a droplet on DigitalOcean using Ansible. I have written the following script for that
- name: launch DO droplet
hosts: localhost
gather_facts: False
tasks:
- name: spin up DO droplet
local_action:
module: digital_ocean
state=present
command=droplet
name=test1
api_token=***
ssh_key_ids=DigitalOcean_MAC
size_id=2gb
region_id=ams2
image_id=ubuntu-18-04-x64
wait_timeout=500
register: my_droplet
- name: print info about my_droplet
local_action:
module: debug
msg="ID is {{ my_droplet.droplet.id }} IP is {{ my_droplet.droplet.ip_address }}"
- name: Add new droplet to host group
local_action: add_host hostname={{ my_droplet.droplet.ip_address }} groupname=launched
- name: Wait for SSH to come up
local_action: wait_for host={{ my_droplet.droplet.ip_address }} port=22 delay=60 timeout=320 state=started
I'm executing it via
ansible-playbook create_droplet.yml -c local -i localhosts
But I'm always getting the following error
> PLAY [launch DO droplet]
> ****************************************************************************************************************
>
> TASK [spin up DO droplet]
> *************************************************************************************************************** fatal: [localhost -> localhost]: FAILED! => {"changed": false, "msg":
> "dopy >= 0.3.2 required for this module"} to retry, use: --limit
> #/Users/wim/Dropbox/Programming/Ansible/DigitalOcean/create_droplet.retry
>
> > PLAY RECAP
> > ****************************************************************************************************************************** localhost : ok=0 changed=0 unreachable=0
> > failed=1
I'm installing that dopy thing using:
sudo pip install 'dopy>=0.3.5,<=0.3.5'
But also that does not help.
Any suggestions what's wrong...or is there another way to create a droplet on DigitalOcean.
sudo pip install 'dopy>=0.3.5,<=0.3.5'
Won't help without knowing what ansible_python_interpreter you are using, since -c local requires the local that ansible is using to contain the module, and not whatever random python you ran pip against. I have a strong suspicion ansible will default to /usr/bin/python which might be fine, but I'm guessing in your circumstance it's not otherwise you wouldn't be asking this question.
It is very likely you can achieve success via ansible-playbook -e ansible_python_interpreter=$(which python) ... since the pip from your $PATH is probably the same from the python on your path. You can, of course, be certain via python -c "import dopy" in that same terminal and ensure nothing explodes.

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.

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

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.

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