Error trying to create a new vm in ansible - 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?

Related

How to fix "Infoblox IPAM is misconfigured?"

I'm calling infoblox from ansible using the following playbook:
- hosts: localhost
gather_facts: false
tasks:
- name: Include infoblox_vault
include_vars:
file: 'infoblox_vault.yml'
- name: Install infoblox-client for DDI
pip:
name: infoblox-client
environment:
HTTP_PROXY: http://our_internal_proxy.net:8080
HTTPS_PROXY: http://our_internal_proxy.net:8080
delegate_to: localhost
- debug:
msg: can I decrypt username?--> "{{ vault_infoblox_username }}"
- name: Check if DNS Record exists
set_fact:
miqCreateVM_ddiRecord: "{{ lookup('nios', 'record:a', filter={'name': 'infoblox-devtest.net' }, provider={'host': 'ddi-qa.net', 'username': vault_infoblox_username, 'password': vault_infoblox_password }) }}"
- debug:
msg: check var miqCreateVM_ddiRecord "{{ miqCreateVM_ddiRecord }}"
- debug:
msg: test to see amazing vm_name! "{{ vm_name }}"
... code snipped
When the job runs, I get:
Vault password:
PLAY [localhost] ***************************************************************
TASK [Include infoblox_vault] **************************************************
ok: [127.0.0.1]
TASK [Install infoblox-client for DDI] *****************************************
ok: [127.0.0.1 -> localhost]
TASK [debug] *******************************************************************
ok: [127.0.0.1] => {
"msg": "can I decrypt username?--> \"manageiq-ddi\""
}
TASK [Check if DNS Record exists] **********************************************
fatal: [127.0.0.1]: FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'nios'. Error was a <type 'exceptions.Exception'>, original message: Infoblox IPAM is misconfigured: infoblox_username and infoblox_password are incorrect."}
PLAY RECAP *********************************************************************
127.0.0.1 : ok=3 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Here's the main part: "An unhandled exception occurred while running the lookup plugin 'nios'. Error was a <type 'exceptions.Exception'>, original message: Infoblox IPAM is misconfigured: infoblox_username and infoblox_password are incorrect."
This playbook used to work in the past. I haven't worked on it for a few monhths. Not sure why it's broken.
I confirmed that I can log into infoblox client manually using the credentials. I also tried manually logging the username to ensure it's decrypting the creds from the ansible-vault file. That worked fine. So it's not the credentials, not the vault decryption. It's something else.
I found the following three related topics online, but none of them seem to resolve the problem:
This one (which references adding certs to the request. Anyone know how to do this? I can't find instructions)
This one (which mentions problems from upgrading. I showed the versions mentioned in that post to our networking folks and they said the version numbers didn't correlate at all with what we have in our environment, so it's hard to evaluate whether that's relevant.)
Last one (which calls for using a property 'http_request_timeout' : None that doesn't strike me as being the problem as I can't get it to work at all.)
Any theories? Thanks!
This might not solve it for others, but this solved it for me:
Got a new password for Ansible to use to log into Infoblox.
Create a new ansible vault file containing the new infoblox password. I made a new password for the vault file encryption also.
I created a new credential object in ansible to enable ansible to be able to read the new vault file.
I updated the playbook to use the new vault.
It works now. Something was wrong with the encryption.

How to authenticate hosts with Ansible?

My hosts file
[all]
192.168.77.10
192.168.77.11
192.1680.77.12
And here is my playbook.yml
---
- hosts: all
tasks:
- name: Add the Google signing key
apt_key : url=https://packages.cloud.google.com/apt/doc/apt-key.gpg state=present
- name: Add the k8s APT repo
apt_repository: repo='deb http://apt.kubernetes.io/ kubernetes-xenial main' state=present
- name: Install packages
apt :
name: "{{ packages }}"
vars:
packages:
- vim
- htop
- tmux
- docker.io
- kubelet
- kubeadm
- kubectl
- kubernetes-cni
When I run
ansible-playbook -i hosts playbook.yml
unexpected authentication problem occurs.
The authenticity of host '192.168.77.11 (192.168.77.11)' can't be established.
ECDSA key fingerprint is SHA256:mgX/oadP2cL6g33u7xzrEblvga9CGfpW13K2YUdeKsE.
Are you sure you want to continue connecting (yes/no)? The authenticity of host '192.168.77.10 (192.168.77.10)' can't be established.
ECDSA key fingerprint is SHA256:ayWHzp/yquIuQxw7MKGR0+NbtrzHY86Z8PdIPv7r6og.
Are you sure you want to continue connecting (yes/no)? fatal: [192.1680.77.12]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname 192.1680.77.12: Name or service not known\r\n", "unreachable": true}
^C [ERROR]: User interrupted execution
I am following the example from DevOps book,I reproduced the original code. MY OS is Ubuntu 18.04.
telnet hosts
telnet: could not resolve hosts/telnet: Temporary failure in name resolution
VM ls output
vagrant#ubuntu-bionic:~$ ls
hosts playbook.retry playbook.yml
I edited /etc/ansible/ansible.cfg by ading False option.
Anyway it does not work again
fatal: [192.1680.77.12]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname 192.1680.77.12: Name or service not known\r\n", "unreachable": true}
fatal: [192.168.77.10]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.77.10' (ECDSA) to the list of known hosts.\r\nvagrant#192.168.77.10: Permission denied (publickey).\r\n", "unreachable": true}
fatal: [192.168.77.11]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.77.11' (ECDSA) to the list of known hosts.\r\nvagrant#192.168.77.11: Permission denied (publickey).\r\n", "unreachable": true}
to retry, use: --limit #/home/vagrant/playbook.retry
PLAY RECAP *************************************************************************************************************************************************************************************************
192.168.77.10 : ok=0 changed=0 unreachable=1 failed=0
192.168.77.11 : ok=0 changed=0 unreachable=1 failed=0
192.1680.77.12 : ok=0 changed=0 unreachable=1 failed=0
How to resolve this issue?
You have several options. One is of course to SSH to the hosts and add them to the known hosts files of your Ansible servers. Another option is to set the environment variable ANSIBLE_HOST_KEY_CHECKING to false. A third option is to use the ansible.cfg config file:
[defaults]
host_key_checking = False
See the official documentation.

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.

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