Anyone know how tog get passed the following error? - ansible

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

Related

my yaml script is not working in raspberrypi

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/

not found what is the error actually means. Error: "(<unknown>): mapping values are not allowed in this context at line 3 column 16 "

- hosts: local_host
remote_user: ansible
become: yes
become_method: sudo
connection: ssh
gather_fact: yes
tasks:
name: installing MariaDB
yum:
name: mariadb-server
state: latest
notify: startservice
handlers:
name: startservice
service:
name: mariadb
state: restarted
The error is in the first two lines:
- hosts: local_host
remote_user: ansible
host cannot have both a scalar value (local_host) and a mapping value (starting at remote_user:). Chances are that you want remote_user to be on the level of hosts, making it a sibling key:
- host: local_host
remote_user: ansible
# and so on

Trying to include a list of tasks from an playbook in Ansible

My folder structure:
First I'll give you this so you can see how this is laid out and reference it when reading below:
/environments
/development
hosts // Inventory file
/group_vars
proxies.yml
/custom_tasks
firewall_rules.yml // File I'm trying to bring in
playbook.yml // Root playbook, just brings in the plays
rev-proxy.yml // Reverse-proxy playbook, included by playbook.yml
playbook.yml:
---
- include: webserver.yml
- include: rev-proxy.yml
proxies.yml just contains firewall_custom_include_file: custom_tasks/firewall_rules.yml
firewall_rules.yml:
tasks:
- name: "Allowing traffic from webservers on 80"
ufw: src=10.10.10.3, port=80, direction=in, rule=allow
- name: "Allowing traffic all on 443"
ufw: port=443, rule=allow
and finally rev-proxy.yml play:
---
- hosts: proxies
become: yes
roles:
- { role: firewall }
- { role: geerlingguy.nginx }
pre_tasks:
# jessie-backports for nginx-extras 1.10
- name: "Adding jessie-backports repo"
copy: content="deb http://ftp.debian.org/debian jessie-backports main" dest="/etc/apt/sources.list.d/jessie-backports.list"
- name: Updating apt-cache.
apt: update_cache="yes"
- name: "Installing htop"
apt:
name: htop
state: present
- name: "Coopying SSL certificates"
copy: src=/vagrant/ansible/files/ssl/ dest=/etc/ssl/certs force=no
tasks:
- name: "Including custom firewall rules."
include: "{{ inventory_dir }}/{{ firewall_custom_include_file }}.yml"
when: firewall_custom_include_file is defined
vars_files:
- ./vars/nginx/common.yml
- ./vars/nginx/proxy.yml
What I'm trying to do:
Using Ansible 2.2.1.0
I'm trying to include a list of tasks that will be run if a variable firewall_custom_include_file is set. The list is included relative to the inventory directory by doing "{{ inventory_dir }}/{{ firewall_custom_include_file }}.yml" - in this case that works out to /vagrant/ansible/environments/development/custom_tasks/firewall_rules.yml
Essentially the idea here is that I need to have different firewall rules be executed based on what environment I'm in, and what hosts are being provisioned.
To give a simple example: I might want to whitelist a database server IP on the production webserver, but not on the reverse proxy, and also not on my development box.
The problem:
Whenever I include firewall_rules.yml like above, it tells me:
TASK [Including custom firewall rules.] ****************************************
fatal: [proxy-1]: FAILED! => {"failed": true, "reason": "included task files must contain a list of tasks"}
I'm not sure what it's expecting, I tried taking out the tasks: at the beginning of the file, making it:
- name: "Allowing traffic from webservers on 80"
ufw: src=10.10.10.3, port=80, direction=in, rule=allow
- name: "Allowing traffic all on 443"
ufw: port=443, rule=allow
But then it gives me the error:
root#ansible-control:/vagrant/ansible# ansible-playbook -i environments/development playbook.yml
ERROR! Attempted to execute "/vagrant/ansible/environments/development/custom_tasks/firewall_rules.yml" as inventory script: problem running /vagrant/ansible/environments/development/custom_tasks/firewall_rules.yml --list ([Errno 8] Exec format error)
Attempted to read "/vagrant/ansible/environments/development/custom_tasks/firewall_rules.yml" as YAML: 'AnsibleSequence' object has no attribute 'keys'
Attempted to read "/vagrant/ansible/environments/development/custom_tasks/firewall_rules.yml" as ini file: /vagrant/ansible/environments/development/custom_tasks/firewall_rules.yml:2: Expected key=value host variable assignment, got: name:
At this point I'm not really sure what it's looking for in the included file, and I can't seem to really find clear documentation on this, or other people having this issue.
Try to execute with -i environments/development/hosts instead of directory.
But I bet that storing tasks file inside inventory is far from best practices.
You may want to define list of custom rules as inventory variable, e.g.:
custom_rules:
- src: 10.10.10.3
port: 80
direction: in
rule: allow
- port: 443
rule: allow
And instead of include task, make something like this:
- ufw:
port: "{{ item.port | default(omit) }}"
rule: "{{ item.rule | default(omit) }}"
direction: "{{ item.direction | default(omit) }}"
src: "{{ item.src | default(omit) }}"
with_items: "{{ custom_rules }}"

Ansible playbook syntax error with tasks:

I just started experimenting with ansible and I am trying to write my first simple playbook.
But I am getting a syntax error with the task keywork,
---
name: add ansible user
hosts: all
become: true
become_method: sudo
become_user:root
tasks:
- user:
name: ansible
groups: ansible
When I run this get the following:
utility:~/scripts/ansible# ansible-playbook --check add-ansible-user.yml
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/root/scripts/ansible/add-ansible-user.yml': line 8, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
^ here
From searching I belive the best bet is that I have an indent problem, but nomatter how I have tried to change it up, I cant get it too work.
- name: add ansible user
hosts: all
become: true
become_method: sudo
become_user: root
tasks:
- user:
name: ansible
groups: ansible
It's indeed the indentation problem. Please try the code written above.
Facing same issue, by making correct Indent spacing able to resolve it
As ref below
---
- name: my ansible
hosts: webserver
remote_user: root
become: true
tasks:
- name: intall httpd
yum:
name: httpd
state: latest
- name: run httpd
service:
name: httpd
state: started
- name: create content
copy:
content: “Congratulation on installing ansible”
dest: /var/www/html/index.html
The problem is here:
become_user:root
You need a space between : and root
become_user: root

ERROR: local_action is not a legal parameter at this level in an Ansible Playbook

Trying to use ansible to spin up instances on linode. I've pip installed linode-python according to http://docs.ansible.com/linode_module.html
I've also made extra adjustments per http://softwareas.com/ansible-and-linode-what-i-learned-about-controlling-linodes-from-ansible/
The command line:
ansible localhost -m linode -a "api_key=xxx name=test plan=1 distribution=124 datacenter=3 password=xxx state=present"
works. Why does this playbook not work?
---
- local_action:
module: linode
api_key: 'xxx'
name: quickpic
plan: 1
datacenter: 3
distribution: 124
password: 'xxx'
wait: yes
wait_timeout: 600
state: present
$ ansible-playbook test.yml
ERROR: local_action is not a legal parameter at this level in an Ansible Playbook
You are missing the connection/host part of the playbook. See Local Playbooks in the documentation.
- hosts: 127.0.0.1
connection: local
tasks:
- name: Create a linode machine
linode:
api_key: 'longStringFromLinodeApi'
name: linode-test1
plan: 1
...etc
Hah! Thanks Josh! just played with the indents and this works:
---
- hosts: 127.0.0.1
connection: local
tasks:
- name: Create linode machine
linode:
api_key: 'xxx'
name: test
plan: 1
datacenter: 3
distribution: 124
password: 'xxx'
wait: yes
wait_timeout: 600
state: present

Resources