Pysphere error when running playbook - yaml

---
- 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.

Related

Using Netbox Ansible Modules

I've been wanting to try out Ansible modules available for Netbox [1].
However, I find myself stuck right in the beginning.
Here's what I've tried:
Add prefix/VLAN to netbox [2]:
cat setup-vlans.yml
---
- hosts: netbox
tasks:
- name: Create prefix 192.168.10.0/24 in Netbox
netbox_prefix:
netbox_token: "{{ netbox_token }}"
netbox_url: "{{ netbox_url }}"
data:
prefix: 192.168.10.0/24
state: present
That gives me the following error:
ansible-playbook setup-vlans.yml
PLAY [netbox] *********************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [NETBOX]
TASK [Create prefix 192.168.10.0/24 in Netbox] ************************************************************************************************
fatal: [NETBOX]: FAILED! => {"changed": false, "msg": "Failed to establish connection to Netbox API"}
PLAY RECAP ************************************************************************************************************************************
NETBOX : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Can someone please point me where I am going wrong?
Note: The NetBox URL is an https://url setup with nginx and netbox-docker [3].
Thanks & Regards,
Sana
[1] https://github.com/netbox-community/ansible_modules
[2] https://docs.ansible.com/ansible/latest/modules/netbox_prefix_module.html
[3]
https://github.com/netbox-community/netbox-docker
I had the same. Apparently the pynetbox api has changed in instantiation (ssl_verify is now replaced by requests session parameters).
I had to force ansible galaxy to update to the latest netbox module with:
ansible-galaxy collection install netbox.netbox -f
The force option did the trick for me.
All playbooks using API modules like netbox (but this is the same for gcp or aws) must use as host not the target but the host that will execute the playbook to call the API. Most of the time this is localhost, but that can be also a dedicated node like a bastion.
You can see in the example on the documentation you linked that it uses hosts: localhost.
Hence I think your playbook should be
---
- hosts: localhost
connection: local
gather_facts: False
tasks:
- name: Create prefix 192.168.10.0/24 in Netbox
netbox_prefix:
netbox_token: "{{ netbox_token }}"
netbox_url: "{{ netbox_url }}"
data:
prefix: 192.168.10.0/24
state: present

Need ansible inventory file details

Could someone please help me to write ansible inventory file to connect to bitbucket - clone a file and place into ansible machine.
Playbook
---
- hosts: bitbucketURL
tasks:
- git:
repo: https://p-bitbucket.com:5999/projects/VIT/repos/sample-playbooks/browse/hello.txt
dest: /home/xxx/demo/output/
Inventory file
[bitbucketURL]
p-bitbucket.com:5999
[bitbucketURL:vars]
ansible_connection=winrm
ansible_user=xxx
ansible_pass=<passwd>
I am getting error while using this playbook and inventory file
-bash-4.2$ ansible-playbook -i inv demo_draft1.yml
PLAY [bitbucketURL] *****************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
fatal: [p-bitbucket.nl.eu.abnamro.com]: UNREACHABLE! => {"changed": false, "msg": "ssl: auth method ssl requires a password", "unreachable": true}
to retry, use: --limit #/home/c55016a/demo/demo_draft1.retry
PLAY RECAP **************************************************************************************************************************************************
p-bitbucket.nl.eu.abnamro.com : ok=0 changed=0 unreachable=1 failed=0
Please help me write a proper inventory file with correct parameters
You need no inventory at all. All you need to do is to set the play to execute on localhost:
---
- hosts: localhost
connection: local
gather_facts: false
tasks:
- git:
repo: https://p-bitbucket.com:5999/projects/VIT/repos/sample-playbooks/browse/hello.txt
dest: /home/xxx/demo/output/
That said, URL should point to Git repository, not a single file (if hello.txt is a single file).

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.

Error while running simple ansible playbook

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

Error trying to create a new vm in ansible

I just started learning Ansible. It has been a pain so far. I have this code to create a new vm. I followed this tutorial.
---
- hosts: localhost
connection: local
tasks:
- vsphere_guest:
vcenter_hostname:1.1.1.12
username: root
password: pasword
guest: newvm001
state: powered_on
validate_certs: no
vm_extra_config:
vcpu.hotadd: yes
mem.hotadd: yes
notes: This is a test VM
folder: MyFolder
vm_disk:
disk1:
size_gb: 10
type: thin
datastore: storage001
vm_nic:
nic1:
type: vmxnet3
network: VM Network
network_type: standard
vm_hardware:
memory_mb: 256
num_cpus: 1
osid: ubuntu64Guest
scsi: paravirtual
esxi:
datacenter: 1.1.1.12
hostname: 1.1.1.12
I however keep getting this error.
[WARNING]: Host file not found: /etc/ansible/hosts
[WARNING]: provided hosts list is empty, only localhost is available
PLAY [localhost]
TASK [setup]
******************************************************************* ok: [localhost]
TASK [vsphere_guest]
*********************************************************** fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg":
"Cannot find datacenter named: 9.1.142.86"}
NO MORE HOSTS LEFT
************************************************************* [WARNING]: Could not create retry file 'testing.retry'. [Errno
2] No such file or directory: ''
PLAY RECAP
********************************************************************* localhost : ok=1 changed=0 unreachable=0
failed=1
Why is that so? And what is the difference between a host file and an inventory file?
what is the difference between a host file and an inventory file?
They are the same. However, since you're doing everything on your local machine, it's fine that you only have localhost available.
This is your error:
TASK [vsphere_guest] *********************************************************** fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Cannot find datacenter named: 9.1.142.86"}
It's not clear to me why you're receiving this with the playbook you've provided, as it doesn't mention that IP at all and the line I suspect is causing the problem is
datacenter: 1.1.1.12
Are you sure this is the file you're running, and that you've saved any changes you've made to it?

Resources