Error while running simple ansible playbook - ansible

playbook is as below...
[ansible#ansible2 outline]$ cat webserver.yaml
--- #Create an YAML from an outline
- hosts: web
connection: ssh
remote_user: ansible
become: yes
become_method: sudo
gather_facts: yes
vars:
test: raju
vars_files:
- /home/ansible/playbooks/conf/copyright.yaml
vars_prompt:
- name: web_domain
prompt: WEB DOMAIN
tasks:
- name: install apache web server
yum: pkg=httpd state=latest
notify: start the service
- name: check service
command: service httpd status
register: result
- debug: var=result
handlers:
- name: start the service
service: name=httpd state=restarted
[ansible#ansible2 outline]$
and the error as below...
[ansible#ansible2 outline]$ ansible-playbook webserver.yaml
WEB DOMAIN:
PLAY [web] *********************************************************************
TASK [setup] *******************************************************************
ok: [web2.bharathkumarraju.com]
TASK [install apache web server] ***********************************************
changed: [web2.bharathkumarraju.com]
TASK [check service] ***********************************************************
fatal: [web2.bharathkumarraju.com]: FAILED! => {"changed": true, "cmd": ["service", "httpd", "status"], "delta": "0:00:00.039489", "end": "2016-10-30 04:53:51.833760", "failed": true, "rc": 3, "start": "2016-10-30 04:53:51.794271", "stderr": "", "stdout": "httpd is stopped", "stdout_lines": ["httpd is stopped"], "warnings": ["Consider using service module rather than running service"]}
NO MORE HOSTS LEFT *************************************************************
RUNNING HANDLER [start the service] ********************************************
to retry, use: --limit #/home/ansible/outline/webserver.retry
PLAY RECAP *********************************************************************
web2.bharathkumarraju.com : ok=2 changed=1 unreachable=0 failed=1

The exit code of running service httpd status is different then 0 because the service was not started. Handlers are always ran at the end of a playbook not when they are notified.
One solution is to put an "ignore_errors: true" at the check service status. Another solution would be to remove the handler+notify and put a service module after the yum:
- service: name=httpd status=started enabled=yes

Try check service's status using /etc/init.d
- name: check service
stat: path=/etc/init.d/httpd
register: result

Related

ansible playbook run with play tag gathers facts even that gathering facts set to 'no'

Below ansible-playbook is run using ansible-playbook playbook.yml --tags=rancher
- name: instal docker
hosts: rancher-server
become: yes
gather_facts: yes
roles:
- role: some_galaxy_role
- name: install rancher
hosts: rancher-server
become: yes
gather_facts: no
tasks:
- name: install rancher
debug:
tags:
- rancher
Only install rancher play is selected by rancher tag and runs as expected. However fact gathering of the first play install docker still runs and takes time. Why and is there a way to avoid it?
Below is the output of the playbook run:
PLAY [install docker] *********************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************
ok: [rancher-server1]
ok: [rancher-server2]
PLAY [install rancher]
You can put a tag on the play level so the whole Instal Docker play is skipped.
Given:
- name: Install Docker
hosts: localhost
gather_facts: yes
tags:
- docker
tasks:
- debug:
- name: Install rancher
hosts: localhost
gather_facts: yes
tags:
- rancher
tasks:
- debug:
When run with --tags rancher, this yields:
PLAY [Install Docker] *********************************************************************************************
PLAY [Install rancher] ********************************************************************************************
TASK [Gathering Facts] ********************************************************************************************
ok: [localhost]
TASK [debug] ******************************************************************************************************
ok: [localhost] =>
msg: Hello world!
PLAY RECAP ********************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
On the other hand, please mind that you are not forced to gather all the facts, you can also gather subsets, to speed up plays.
For example, you can use a minimal subset of the facts only:
- name: Install Docker
hosts: localhost
gather_subset:
- min
tasks:
- debug:
Of course, it all just depends what is needed in the some_galaxy_role that requires you to gather facts.

Playbook failing execution due to permission denied

Here is the inventory content:
[osm]
osm_host ansible_port=22 ansible_host=10.20.20.11 ansible_user=ubuntu ansible_ssh_private_key_file=/path/to/key/key
And here is the playbook content:
- hosts: osm
user: ubuntu
become: yes
tasks:
- name: Download the OSM installer
get_url: url=https://osm-download.etsi.org/ftp/osm-8.0-eight/install_osm.sh dest=/tmp/install_osm.sh
- name: Execute the OSM installer
shell: /tmp/install_osm.sh
When I run ansible-playbook -i inventory play.yaml, I get the following error:
PLAY [osm]
TASK [Gathering Facts]
********************************************************* ok: [osm_host]
TASK [Download the OSM installer]
********************************************** ok: [osm_host]
TASK [Execute the OSM installer]
*********************************************** fatal: [osm_host]: FAILED! => {"changed": true, "cmd": "/tmp/install_osm.sh", "delta":
"0:00:00.001919", "end": "2020-09-04 19:26:46.510381", "msg":
"non-zero return code", "rc": 126, "start": "2020-09-04
19:26:46.508462", "stderr": "/bin/sh: 1: /tmp/install_osm.sh:
Permission denied", "stderr_lines": ["/bin/sh: 1: /tmp/install_osm.sh:
Permission denied"], "stdout": "", "stdout_lines": []}
PLAY RECAP
********************************************************************* osm_host : ok=2 changed=0 unreachable=0
failed=1 skipped=0 rescued=0 ignored=0
I tried to use true and yes for the become clause, but nothing changed. What am I missing?
You have to be sure that the root user has executable permissions on the new OSM download. When you use a become: yes without become_user, the default user is root
So you need to be sure that root user can execute your script.
Try the get_url like that:
- hosts: osm
user: ubuntu
become: yes
tasks:
- name: Download the OSM installer
get_url:
url: https://osm-download.etsi.org/ftp/osm-8.0-eight/install_osm.sh
dest: /tmp/install_osm.sh
mode: "0555"
- name: Execute the OSM installer
shell: /tmp/install_osm.sh
Play with the mode param of the get_url module.

What is the Ansible equivalent playbook for "lxc launch ubuntu: new-container"

What is the Ansible equivalent of playbook of lxc launch ubuntu: new-container.
I can successfully ping the machine on which I want to create the container, and when logged into that machine I can create a container without any problems. When I try to use the below playbooks however, I get the following results:
Attempt 1:
- hosts: node0
tasks:
- name: Create a started container
lxd_container:
name: mycontainer
state: started
profiles: ["default"]
Result:
# ansible-playbook play
PLAY [node0] ***************************************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************************
ok: [node0]
TASK [Create a started container] ******************************************************************************************************************************************************
fatal: [node0]: FAILED! => {"actions": [], "changed": false, "failed": true, "msg": "unknown source type "}
to retry, use: --limit #/root/play.retry
PLAY RECAP *****************************************************************************************************************************************************************************
node0 : ok=1 changed=0 unreachable=0 failed=1
Attempt 2:
- hosts: node0
connection: local
gather_facts: false
tasks:
- name: create a container
connection: local
become: false
lxd_container:
name: test
state: started
source:
type: image
mode: pull
server: https://images.linuxcontainers.org
protocol: lxd
alias: "ubuntu/xenial/amd64"
profiles: ["default"]
wait_for_ipv4_addresses: false
timeout: 600
Result:
# ansible-playbook play
PLAY [node0] ***************************************************************************************************************************************************************************
TASK [create a container] **************************************************************************************************************************************************************
fatal: [node0]: FAILED! => {"actions": [], "changed": false, "failed": true, "msg": "Failed to change ownership of: /var/lib/lxd/containers/test/rootfs"}
to retry, use: --limit #/root/play.retry
PLAY RECAP *****************************************************************************************************************************************************************************
node0 : ok=0 changed=0 unreachable=0 failed=1
Attempt 3 seems to work however it seems to download a new image instead of using the one which already exists on the machine:
# An example for creating a Ubuntu container and install python
- hosts: node0
connection: local
tasks:
- name: Create a started container
lxd_container:
name: mycontainer
state: started
source:
type: image
mode: pull
server: https://images.linuxcontainers.org
protocol: lxd
alias: ubuntu/xenial/amd64
profiles: ["default"]
wait_for_ipv4_addresses: true
timeout: 600
How to write a playbook equivalent of lxc launch ubuntu: new-container?
Answer from comments:
Why do you use connection: local? It means to run commands on local ansible host.
You should connect to target host and execute lxd_container module there.

Ansible: Shared connection to xxx closed

Hello guys I make a simple playbook to practice with Ansible but I have a problem when I try to run the playbook (ansible-playbook -i hosts.ini playbook.yml) to configure an instance ec2 the output returns:
> fatal: [XX.XXX.XXX.XXX]: FAILED! => {
> "changed": false,
> "failed": true,
> "invocation": {
> "module_name": "setup"
> },
> "module_stderr": "Shared connection to XXX.XXX.XXX.XXX closed.\r\n",
> "module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n",
> "msg": "MODULE FAILURE" } to retry, use: --limit #/home/douglas/Ansible/ansible_praticing/projeto2.retry
>
> PLAY RECAP
> *********************************************************************
> XX.XXX.XXX.XXX : ok=0 changed=0 unreachable=0 failed=1
When I try to connect with the instance via ssh -i ~/.ssh/key.pem ubuntu#public.ip it works well but the provisioning not.
My playbook:
- hosts: projeto
sudo: True
remote_user: ubuntu
vars_files:
- vars.yml
tasks:
- name: "Update"
apt: update_cache=yes
- name: "Install the Ansible"
apt: name=ansible state=latest
- name: "Installt the mysql"
apt:
args:
name: mysql-server
state: latest
- name: "Install the Nginx"
apt:
args:
name: nginx
state: latest
My hosts.ini is also ok (with public ip of aws ec2 instance) and I put the public key (~/.ssh/id_rsa.pem of local machine) in the ~/.ssh/authorized_keys file, inside of the instance.
In the last week (Friday) this playbook was working well.
What am I doing wrong?
Maybe my answer is too late but I faced the same problem today. I have an Ubuntu 16.04 instance running on my EC2. I think, since it has Python 3 (Python 3.5) as its default Python installation. Hence, ansible is not able to find the required Python directory (/usr/bin/python). I got around this issue by changing the ansible Python interpreter to Python 3.
I added ansible_python_interpreter=/usr/bin/python3 to my inventory file and did not have to change the playbook.
Reference - http://docs.ansible.com/ansible/latest/python_3_support.html

Pysphere error when running playbook

---
- hosts: my-host
tasks:
- vsphere_guest:
vcenter_hostname: vcenter.mydomain.local
username: myuser
password: mypass
guest: newvm001
vmware_guest_facts: yes
When I run this playbook, I get this error
PLAY [my-host]
TASK [setup]
******************************************************************* ok: [19.3.112.97 ]
TASK [vsphere_guest]
*********************************************************** fatal: [19.3.112.97 ]: FAILED! => {"changed": false, "failed": true, "msg":
"pysphere module required"}
NO MORE HOSTS LEFT
************************************************************* [WARNING]: Could not create retry file 'createvms.retry'.
[Errno 2] No such file or directory: ''
PLAY RECAP
19.3.112.97 : ok=1 changed=0 unreachable=0 failed=1
Why do I get this error? I have uninstalled and installed pysphere. I have used previous and current versions of it but I still get this error.
You usually want to run cloud/VM management modules from your control machine (localhost).
This would look like this:
---
- hosts: localhost
connection: local
tasks:
- vsphere_guest:
vcenter_hostname: vcenter.mydomain.local
username: myuser
password: mypass
guest: newvm001
vmware_guest_facts: yes
In this case ansible use PySphere installed on your control host to connect to vcenter.mydomain.local and provision VMs.
In your example PySphere should be installed on 19.3.112.97 and vcenter.mydomain.local should be accessible from that host.

Resources