my yaml script is not working in raspberrypi - ansible

I have a script that is working correctly on my Mac and other linux boxes. I moved it over to a raspberrypi and I am getting a error. At first I thought it was a syntax error and again, I copied my script over and it appears to be working correctly on other devices.
I am using the latest version of ansible and raspbian. Also I have installed the pivmomi plugin.
FYI: on my script I have removed the variables for obvious reasons.
Any suggestions?
Using /etc/ansible/ansible.cfg as config file
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to have been in '/root/theScript.yaml': line 27, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: "Gather info about the vmware guest vm"
^ here
---
- hosts: localhost
#become: yes
ignore_unreachable: true
vars:
tasks:
- name: "Gather info about the vmware guest vm"
vmware_guest_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ datacenter_name }}"
validate_certs: no
name: "{{ guest_name }}"
delegate_to: localhost
register: vm_info

It really looks like an indentation error to me. How did you copy and paste this in. Could there be tabs interpreted here. Try removing lines 3,4 5 6 and 8.

Apparently the raspberry repo does not have the latest version of ansible. Once I updated it my script worked.
I used the link below to update ansible.
https://www.theurbanpenguin.com/installing-ansible-on-the-raspberry-pi/

Related

Error handling for updating IOS via Ansible

beginner to using Ansible. More of a network engineer, less of a scripter / programmer, but trying to learn a new skill.
Attempting to write a playbook to automate updating of our fleet of Cisco switch stacks but I think I am both lost in syntax and if this is the 'right' way to go about what I am doing.
---
- name: Update Cisco switch stack
hosts: Cisco2960
vars:
upgrade_ios_version: "15.2(7)E5"
tasks:
name: Check current IOS version / Determine if update is needed...
ios_facts:
debug:
msg:
- "Current image is {{ ansible_net_version }}"
- "Current compliant image is {{ upgrade_ios_version }}"
name: Fail if versions match.
ansible.builtin.fail: msg="IOS versions match. Stopping update."
when: "{{ ansible_net_version }} = {{ upgrade_ios_version }}"
At first I thought each variable needed its own quotation, but that appears to be incorrect syntax as well, as below.
when: "{{ ansible_net_version }}" = "{{ upgrade_ios_version }}"
Couple questions:
Is there an easier way with a plain-English way of describing the type of error handling I am looking for? Ansible documentation is great on options, but light on practical applications / examples.
Why am I receiving this specific syntax error in this case?
You can use the playbook below.
Ansible Playbook to upgrade Cisco IOS
- name: Upgrade CISCO IOS
hosts: SWITCHES
vars:
upgrade_ios_version: 15.2(7)E5
tasks:
- name: CHECK CURRENT VERSION
ios_facts:
- debug:
msg:
- "Current version is {{ ansible_net_version }}"
- "Current compliant image is {{ upgrade_ios_version }}"
- debug:
msg:
- "Image is not compliant and will be upgraded"
when: ansible_net_version != upgrade_ios_version
Create backup folder for today
- hosts: localhost
tasks:
- name: Get ansible date/time facts
setup:
filter: "ansible_date_time"
gather_subset: "!all"
- name: Store DTG as fact
set_fact:
DTG: "{{ ansible_date_time.date }}"
- name: Create Directory {{hostvars.localhost.DTG}}
file:
path: ~/network-programmability/backups/{{hostvars.localhost.DTG}}
state: directory
run_once: true
Backup Running Config
- hosts: SWITCHES
tasks:
- name: Backup Running Config
ios_command:
commands: show run
register: config
- name: Save output to ~/network-programmability/backups/
copy:
content: "{{config.stdout[0]}}"
dest: "~/network-programmability/backups/{{hostvars.localhost.DTG}}/{{ inventory_hostname }}-{{hostvars.localhost.DTG}}-config.txt"
SAVE the Running Config
- name: Save running config
ios_config:
save_when: always
Copy software to target device
- name: Copy Image // This could take up to 4 minutes
net_put:
src: "~/network-programmability/images/c2960l-universalk9-mz.152-7.E5.bin"
dest: "flash:/c2960l-universalk9-mz.152-7.E5.bin"
vars:
ansible_command_timeout: 600
Change the Boot Variable to the new image
- name: Change Boot Variable to new image
ios_config:
commands:
- "boot system flash:c2960l-universalk9-mz.152-7.E5.bin"
save_when: always
Reload the device
- name: Reload the Device
cli_command:
command: reload
prompt:
- confirm
answer:
- 'y'
Wait for Reachability to the device
- name: Wait for device to come back online
wait_for:
host: "{{ inventory_hostname }}"
port: 22
delay: 90
delegate_to: localhost
Check current image
- name: Check Image Version
ios_facts:
- debug:
msg:
- "Current version is {{ ansible_net_version }}"
- name: ASSERT THAT THE IOS VERSION IS CORRECT
vars:
upgrade_ios_version: 15.2(7)E5
assert:
that:
- upgrade_ios_version == ansible_net_version
- debug:
msg:
- "Software Upgrade has been completed"

Anyone know how tog get passed the following error?

So I have this error that I am getting, which is mind boggling.
ERROR! couldn't resolve module/action 'community.digitalocean.digital_ocean'. This often indicates a misspelling, missing collection, or incorrect module path.
The error appears to be in '/etc/ansible/playbook.yml': line 19, column 6, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Create DigitalOcean SSH key
^ here
Here is my playbook:
- hosts: localhost
connection: local
vars:
digital_ocean_token:
droplets:
- ****
- ****
tasks:
- name: Create SSH key
user:
name: "{{ansible_user_id}}"
generate_ssh_key: yes
ssh_key_type: rsa
ssh_key_bits: 4096
ssh_key_file: .ssh/id_rsa
- name: Create DigitalOcean SSH key
community.digitalocean.digital_ocean:
state: present
command: droplet
name: "{{ item }}"
unique_name: yes
size_id: s-1vcpu-1gb
region_id: nyc1
image_id: centos-7-64x
ssh_key_ids: "{{ my_ssh.ssh_key.id }}"
api_taken: "{{ digital_ocean_token }}"
You need to install the collection before running the playbook.
ansible-galaxy collection install community.digitalocean
Reference

Ansible Module vmware_host_facts only returning one Host

I'm playing around with Ansible VMWare Modules and tried to get all the Information from ESXi Hosts from a vCenter.
With the Module vmware_host_facts it should be possible.
But when I run a Playbook with the following configuration, I only get the Information of one Host back - and not all. In this vCenter there are about 20 Hosts.
Playbook:
- name: Gather vmware host facts
vmware_host_facts:
hostname: vCenter_IP
username: username
password: password
register: host_facts
delegate_to: localhost
In the Documentation it tells me, that the hostname can also be a vCenter IP.
Resource:
http://docs.ansible.com/ansible/latest/modules/vmware_host_facts_module.html#vmware-host-facts
Is that module not the correct one to gather all host information from a vCenter? Or is there a "hidden trick", which I am missing?
Thanks a lot!
Kind regards,
M
I got an answer on another resource.
https://github.com/ansible/ansible/issues/43187
Basically you have to add the Hostnames as a list to the Task.
Example:
- name: Gather vmware host facts
vmware_host_facts:
hostname: "{{ item.esxi_hostname }}"
username: "{{ item.esxi_user }}"
password: "{{ item.esxi_pass }}"
validate_certs: no
register: host_facts
delegate_to: localhost
with_items:
- {esxi_hostname: hostname_1, esxi_user: username_host_1, esxi_pass: pass_host_1}
- {esxi_hostname: hostname_2, esxi_user: username_host_2, esxi_pass: pass_host_2}
These Hostnames you can gather with another module - vmware_vm_facts. Here you can get the Hostnames from. I will update this with an example playbook in the near future.
I use this module with these options, so far I have one issue if I put vcenter address it only give me first Esxi output.
- name: Somethign.
vmware_host_facts:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_user }}'
password: '{{ vcenter_pass }}'
validate_certs: no
register: all_cluster_hosts_facts
delegate_to: localhost
- debug: var=all_cluster_hosts_facts

Ansible Playbook with Variables?

Environment:
Ubuntu 16.04 in Azure vm
ansible 2.4.3.0
python version = 2.7.12 (default, Nov 20 2017, 18:23:56) [GCC 5.4.0 20160609]
azure-cli (2.0.27)
I have successfully created my first vm in azure using an Ansible Playbook.
Now, my question is this: Can I use variables in the playbook for things like admin-password, vmname etc, so that I could, for instance, put a list of 10 machines I want to create in a file with the different names and their passwords, and then loop through and feed that info through the playbook command to the playbook.yml file?
I'm open to any way of doing this, and I'm aware that Ansible may already have an easier/normal way of doing it, I just don't know enough to google what I need.
EDIT:
Evidently my question is too broad. I'll try and be more specific. I looked at the link that Konstantin Suvorov left. As I understand it, the method described does the inverse of what I'm after, by letting you assign variables to groups. And in the examples, to existing machines.
What I need is to be able to create vms, in the normal YAML style, but be able to have the process loop in some way so that it will create machines 1 thru 10 with all the same info, save for the machine name and the password. The examples above seem to use the variables to assign the same info to multiple machines.
How Can I do this? I'm mostly a bash scripter, so what I'd do is put name:password for each machine on multiple lines of a file, loop thru it and run the ansible-playbook command once per loop to create each machine, feeding in the variables somehow. However, I have a feeling that there's a more efficient way, such that I run the command once and the command will create each machine with the variables/info that are coded (somehow) into the yaml file. Is this specific enough?
FURTHER EDIT:
Thanks for putting me on the right track. I have adapted the script i used to create one vm to try to make it create two. I am getting assorted errors, none of which seem to actually point to offending code. Here follows what I'm trying:
- name: Create Azure VM
hosts: localhost
connection: local
with_items:
- { vmname: 'testvmfordb', userpassword: 'my password' }
- { vmname: 'testvmforfe', userpassword: 'my other password' }
tasks:
- name: Create virtual network inteface card
azure_rm_networkinterface:
resource_group: my_rg
name: "{{ item.vmname }}NIC"
virtual_network: my_rg
subnet: default
public_ip_name: "{{ item.vmname }}IP"
security_group: my_firewall_rules
- name: Create VM
azure_rm_virtualmachine:
resource_group: my_rg
name: "{{ item.vmname }}"
vm_size: Standard_DS1_v2
location: EastUS
admin_username: myusername
admin_password: "{{ item.userpassword }}"
ssh_password_enabled: true
storage_container: vhds
storage_blob: "{{ item.vmname }}osdisk.vhd"
network_interfaces: "{{ item.vmname }}NIC"
image:
offer: UbuntuServer
publisher: Canonical
sku: '16.04-LTS'
version: latest
os_type: Linux
Thanks in advance!
I followed #KonstantinSuvorov suggestion and separated the creation tasks in a separate file. I got this message:
TASK [include_tasks] *******************************************************************************************************************************
task path: /home/user/ansible_playbooks/azure_create_many_vms.yml:5
fatal: [localhost]: FAILED! => {
"reason": "included task files must contain a list of tasks"
}
fatal: [localhost]: FAILED! => {
"reason": "included task files must contain a list of tasks"
}
Here's the files/code:
azure_create_many_vms.yml
- name: Create Azure VM
hosts: localhost
connection: local
tasks:
- include_tasks: azure_create_many_vms_tasks.yml
with_items:
- { vmname: 'testvmforelastic', userpassword: 'my user password' }
- { vmname: 'testvmforfe', userpassword: 'my user password' }
azure_create_many_vms_tasks.yml
tasks:
- name: Create virtual network inteface card
azure_rm_networkinterface:
resource_group: my_core_rg
name: "{{ item.vmname }}NIC"
virtual_network: my_core_rg
subnet: default
public_ip_name: "{{ item.vmname }}IP"
security_group: my_core_firewall_rules
- name: Create VM
azure_rm_virtualmachine:
resource_group: my_core_rg
name: "{{ item.vmname }}"
vm_size: Standard_DS1_v2
location: EastUS
admin_username: adminuser
admin_password: "{{ item.userpassword }}"
ssh_password_enabled: true
storage_container: vhds
storage_blob: "{{ item.vmname }}osdisk.vhd"
network_interfaces: "{{ item.vmname }}NIC"
image:
offer: UbuntuServer
publisher: Canonical
sku: '16.04-LTS'
version: latest
os_type: Linux
You should remove tasks keyword from included file, leave just a plain list of tasks like this:
- name: Create virtual network inteface card
azure_rm_networkinterface:
...
- name: Create VM
azure_rm_virtualmachine:
...

Ansible - Unable to run certain JUNOS modules

I'm trying to run the Ansible modules junos_cli and junos_rollback and I get the following error:
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to have been in '/home/quake/network-ansible/roles/junos-rollback/tasks/main.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- name: I've made a huge mistake
^ here
This is the role in question:
---
- name: I've made a huge mistake
junos_rollback:
host={{ inventory_hostname }}
user=ansible
comment={{ comment }}
confirm={{ confirm }}
rollback={{ rollback }}
logfile={{ playbook_dir }}/library/logs/rollback.log
diffs_file={{ playbook_dir }}/configs/{{ inventory_hostname }}
Here is the Juniper page:
http://junos-ansible-modules.readthedocs.io/en/1.3.1/junos_rollback.html
Their example's syntax is a little odd. host uses a colon while the rest uses = signs. I've tried mixing both and only using one or the other. I keep getting errors.
I also confirmed that my junos-eznc version is higher than 1.2.2 (I have 2.0.1)
I've been able to use junos_cli before, I don't know if a version mismatch happened. On the official Ansible documentation, there is no mention of junos_cli or junos_rollback. Perhaps they're not supported anymore?
http://docs.ansible.com/ansible/list_of_network_modules.html#junos
Thanks,
junos_cli & junos_rollback are part of Galaxy and not core modules. You can find them at
https://galaxy.ansible.com/Juniper/junos/
Is the content posted here has whole content of your playbook? if yes, You need to define other items too in your playbook such as roles, connection, local. For example
refer https://github.com/Juniper/ansible-junos-stdlib#example-playbook
```
---
- name: rollback example
hosts: all
roles:
- Juniper.junos
connection: local
gather_facts: no
tasks:
- name: I've made a huge mistake
junos_rollback:
host = {{inventory_hostname}}
----
----
```
Where have you saved the content of juniper.junos modules?. Can you post the content of your playbook and the output of the tree command to see your file structure? That could help.
I had a similar problem where Ansible was not finding my modules and what I did was to copy the juniper.junos folder to my roles folder and then added a tasks folder within it to execute the main.yaml from there.
Something like this:
/Users/macuared/Ansible_projects/roles/Juniper.junos/tasks
---
- name: "TEST 1 - Gather Facts"
junos_get_facts:
host: "{{ inventory_hostname}}"
user: "uuuuu"
passwd: "yyyyyy"
savedir: "/Users/macuared/Ansible_projects/Ouput/Facts"
ignore_errors: True
register: junos
- name: Checking Device Version
debug: msg="{{ junos.facts.serialnumber }}"
Additionally, I would add "" to the string values in your YAML. Something like this:
---
- name: I've made a huge mistake
junos_rollback:
host="{{ inventory_hostname }}"
user=ansible
comment="{{ comment }}"
confirm={{ confirm }}
rollback={{ rollback }}
logfile="{{ playbook_dir }}/library/logs/rollback.log"
diffs_file="{{ playbook_dir }}/configs/{{ inventory_hostname }}"
Regarding this "I've tried mixing both and only using one or the other. I keep getting errors."
I've used just colon and mine works fine even when in the documentation suggests = signs. See junos_get_facts

Resources