Ansible: How to use examples from the documentation? - module

I'm starting to learn Ansible and for this I copy and paste examples from the documentation. For example this one
- name: Check that a page returns a status 200 and fail if the word AWESOME is not in the page contents
ansible.builtin.uri:
url: http://www.example.com
return_content: yes
register: this
failed_when: "'AWESOME' not in this.content"
which I've found in uri module documentation.
Every single time I do this, whatever the module I get:
ERROR! 'ansible.builtin.uri' is not a valid attribute for a Play
The error appears to have been in '/home/alfrerra/test2.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Check that a page returns a status 200 and fail if the word AWESOME is not in the page contents
^ here
I have only 2 playbooks that only ping successfully:
-
name: ping localhost
hosts: localhost
tasks:
- name: ping test
ping
and
---
- name: ping localhost
hosts: localhost
tasks:
- name: ping test
ping
So I adapted the example to match these 2 examples, but to no avail so far.
I'm sure it's nothing much but it's driving me crazy.

As already mentioned within the comments, the documentation are to show how to use certain parameter of certain modules within a single task. They are not mentioned to work in a copy and paste manner by itself or only.
You may have a look into the following minimal example playbook
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Check that a page returns a status 200 and fail if the word 'iana.org' is not in the page contents
uri:
url: http://www.example.com
return_content: yes
environment:
http_proxy: "localhost:3128"
https_proxy: "localhost:3128"
register: this
failed_when: "'iana.org' not in this.content"
- name: Show content
debug:
msg: "{{ this.content }}"
resulting into the output of the page content.
Please take note that the web page of the example domain is connected and delivers a valid result, but it does not contain the word AWESOME. To address this the string to lookup was changed to the page owner iana.org and to not let the task fail. Furthermore and since working behind a proxy it was necessary to address the proxy configuration and which you probably need to remove again.
Your first example is a single task and failing therefore with ERROR! ... not a valid attribute for a Play. Your second and
third examples are playbooks with a single task and therefore executing.
Documentation
Module format and documentation - EXAMPLES block
... show users how your module works with real-world examples in multi-line plain-text YAML format. The best examples are ready for the user to copy and paste into a playbook.
Ansible playbooks

failed_when must be a comparison or a boolean.
example :
- name: Check that a page returns AWESOME is not in the page contents
ansible.builtin.uri:
url: http://www.example.com
return_content: yes
register: this
failed_when: this.rc == 0;
It will execute the task only if the return value is equal to 0

Related

How to dynamically set the hosts field in Ansible playbooks with a variable generated during execution?

I am trying to test something at home with the variables mechanism Ansible offers, which I am about to implement in one of my projects at work. So, been searching for a while now, but seems I can't get it working that easily, even with others` solutions here and there.
I will represent my project logic at work now, by demonstrating with my test directory & files structure at home. Here's the case, I have the following playbooks:
main.yaml
pl1.yaml
pl2.yaml
Contents of ./main.yaml:
- import_playbook: /home/martin/ansible/pl1.yaml
- import_playbook: /home/martin/ansible/pl2.yaml
Contents of ./pl1.yaml:
- name: Test playbook 1
hosts: localhost
tasks:
- name: Discovering the secret host
shell: cat /home/martin/secret
register: whichHostAd
- debug:
msg: "{{ whichHostAd.stdout }}"
- name: Discovering my hostname
shell: hostname
register: myHostnameAd
- set_fact:
whichHost: "{{ whichHostAd.stdout }}"
myHostname: "{{ myHostnameAd.stdout }}"
cacheable: yes
- name: Test playbook 1 part 2
hosts: "{{ hostvars['localhost']['ansible_facts']['whichHost'] }}"
tasks:
- name: Structuring info
shell: hostname
register: secretHostname
- name: Showing the secret hostname
debug:
msg: "{{ secretHostname.stdout }}"
Contents of ./pl2.yaml:
- name: Test Playbook 2
hosts: "{{ whichHost }}"
tasks:
- name: Finishing up
shell: echo "And here am i again.." && hostname
- name: Showing var myHostname
debug:
msg: "{{ myHostname.stdout }}"
The whole idea is to have a working variable on the go at the hosts field between the plays. How do we do that?
The playbook does not run at all if I won't define the whichHost variable as an extra arg, and that's ok, I can do it each time, but during the execution I would like to have that variable manageable and changeable. In the test case above, I want whichHost to be used everywhere across the plays/playbooks included in main.yaml, specifically to reflect the output of the first task in pl1.yaml (or the output of the whichHostAd.stdout variable), so I can determine the host I am about to target in pl2.yaml.
According to docs, I should be able to at least access it with hostvars (as in my playbook), but this is the output I get when I try the above example:
ERROR! The field 'hosts' has an invalid value, which includes an undefined variable. The error was: 'dict object' has no attribute 'whichHost'
The error appears to have been in '/home/martin/ansible/pl1.yaml': line 22, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Test playbook 1 part 2
^ here
set_fact also does not seem to be very helpful. Any help will be appreciated!
Ok, I've actually figured it out pretty fast.
So, we definitely need to have a fact task, holding the actual data/output:
- hosts: localhost
tasks:
- name: Saving variable
set_fact:
whichHost: "{{ whichHostAd.stdout }}"
After that, when you want to invoke the var in other hosts and plays, we have to provide the host and the fact:
"{{ hostvars['localhost']['whichHost'] }}"
Like in my test above, but without ['ansible_facts']

Ansible playbook for Mapping all Routers and Switches in the network using CDP Neighbor

Hello I need help with writing a play for finding all routers and switches in the network.
Environment:
Ansible 2.8
Python 2.7
Test network in eve-ng
Router and Switches have ios
Problem Statement:
Start at the core switch and by using cdp neighbors traverse all the paths till the last switch/router inside the domain.
The depth of the network is unknown.
Output:
JSON containing a hierarchical ordering of network devices.
{
A:{A1,A2},
C:{C1,C5:{C5i:{..},C5j}
}
My Attempt:
---
- name: Backup show run (enable mode commands)
hosts: ["testrouter"]
gather_facts: false
connection: network_cli
vars:
ansible_network_os: ios
grand_parent: ["testrouter"]
tasks:
- name: CDP for "{{ inventory_hostname }}"
register: all_facts
ios_facts:
gather_subset: all
- name: filter cdp neighbors for all facts
debug:
msg: "Child of {{ inventory_hostname }} is {{ item.value[0].host }}"
loop: "{{ lookup('dict', all_facts.ansible_facts.ansible_net_neighbors) }}"
when: item.value|length == 1
- name: Remove previous grand_parent
set_fact:
root: "['{{ parent[0] }}']"
when: parent|length == 2
- name: Add the latest host as grand_parent
set_fact:
root: "{{ parent + [ inventory_hostname ] }}"
when: parent|length == 1
I have written this script in python using netmiko previously but now we have a requirement for it to be written in Ansible.
Problems:
I don't know how to modify hosts dynamically as I discovery new hosts with cdp neighbors.
Plus I need recursion to explore to unknown depth
Also since, I am learning Ansible for first time I am worried I would over complicate things and write bloated code.
Thank you for your time.
What you are doing here is a programming. You are trying to write a program using a tool which is less suited for programming than any programming language out there. May be brainfuck is worse, but I'm not sure.
There is no good answer to your question on 'how to do this complicated business logic with Ansible', like there is no good answer on question 'how to tighten a nut with a hammer'.
What you need to do (either):
Write an stand-alone application and use it in conjunction with Ansible (via rest API, inventory, stdin/out, you name it)
Write a module for Ansible. You got json at stdin, you answer with json on stdout. There are ansible heplers for Python, but you are free to use any language for module.
Write a lookup plugin for Ansible. This is more tricky and you need to keep it operational as Ansible evolves.
I advise you to go for No 1 or 2.

ansible-vault error - "Vault format unhexlify error: Odd-length string"

I am using ansible-napalm and trying to write a simple playbook to pull facts from network devices.
I want to encrypt the passwords with ansible-vault, however regardless of what I try I keep getting the error:
Vault format unhexlify error: Odd-length string
I initially was trying this in bash under the Windows subsystem for Linux and I thought this may be the issue so I recreated everything on a centos VM and still run into the same issue.
I have tried using encrypt-string to embed the encrypted pw directly into the playbook.
I have also tried encrypting the file and calling the variable. Both methods give the same error.
I found this issue: Ansible-vault errors with "Odd-length string"
And I thought the issue was to do with CRLF line terminators so I sorted that and made sure all files were ASCII text but this still gives the same errors.
My code is below, any help would be majorly appreciated because I am pulling my hair out!
---
- name: napalm_facts
hosts: all
connection: local
gather_facts: no
tasks:
- name: get facts from device
napalm_get_facts:
hostname: "{{ ansible_host }}"
username: 'admin'
password: "{{ napalm_password }}"
dev_os: 'ios'
register: result
- name: print results
debug: msg="{{ result }}"
I've tried the below methods, for reference.
ansible-vault encrypt vars/vaultpw.yml
ansible-vault encrypt_string password123 --ask-vault-pass
I managed to get this sorted by taking suggestions from this thread: Inline encrypted variable not JSON serializable
Thanks for the reply at first, it put me on the right path with getting this sorted.

Include variables generated in first play of playbook

I have a playbook which consists of two plays:
1: Create inventory file and variables file on localhost
2: Use the variables in commands on generated inventory
Example playbook:
---
- name: Generating inventory and variables
hosts: localhost
vars_files:
- variables.yml #variables file used for automating
tasks:
- name: Creating inventory template
template:
src: hosts.j2
dest: "./inventories/{{location}}/hosts"
mode: 0777
force: yes
ignore_errors: yes
run_once: true
- meta: refresh_inventory
- name: Creating predefined variables from a template
template:
src: predefined-vars.yml.j2
dest: "./variables/predefined-vars.yml"
- name: Setting vlan to network devices
remote_user: Ansible
hosts: all
vars_files:
- variables.yml #variables file used for automating.
- variables/predefined-vars.yml
tasks:
- name: configure Junos ROUTER for vlan
include_tasks: ./roles/juniper/tasks/add_vlan_rt.yml
when:
- inventory_hostname in groups['junos_routers']
- groups['junos_routers'] | length == 1
- location == inventory_name
This gives undefined variable error (for a variable created in the first play).
Is there a way to do this? I use this for generating variables like router_port_name and so on - the variables depend on location and dedicated server, which are defined in variables.yml
Any help is really appreciated.
Thanks
EDIT: However, I have noticed that this playbook:
---
- hosts: localhost
gather_facts: false
name: 1
vars_files:
- variables.yml
tasks:
- name: Creating predefined variables from a template
template:
src: predefined-vars.yml.j2
dest: "./variables/predefined-vars.yml"
- name: Generate hosts file
hosts: all
vars_files:
- variables.yml
- ./variables/predefined-vars.yml
tasks:
- name: test
debug: msg="{{ router_interface_name }}"
show the variables created in the first play.
The difference I see is that the first playbook reads all variable files (even predefined-vars.yml <- created at first play, used at the other) used in the playbook at the start of the first play (generating inventory and creating variable file) while the second playbook reads variables.yml in first play and only at the start of the second play reads the predefined-vars.yml .
Any Ideas how to make the first playbook behave the same way?
So I have found the solution to the problem, based on the documentation and suggestions from other people.
What I understood about the problem:
A playbook will read all the variables (of all plays) provided into the cache for later use, so if I include my predefined-vars.yml into vars_files, then after changing it in first play, the changes will not be used by later plays because they will use cache for that.
Thus I had to create another task in second play, which would read (load into cache) my newly generated file (for that play):
- name: Include predefined vars
include_vars: ./variables/predefined-vars.yml
run_once: true
Hope this helps you!
Still have no idea why second play shows the variables...

error while setting a fact or debug a webpage after registering this webpage in the get_uri module in Ansible

I have a problem every time while setting a fact for a registered webpage the error is :
u'redirected': False}]}: template error while templating string: unexpected char u'&' at 1238
The Playbook like:
- name: check webpage
uri:
url: http://{{ item.host }}.x.x.x
validate_certs: False
return_content: yes
status_code: 200
register: webpage3
with_items: "{{ servers }}"
when:
- lb is defined
- lb == "true"
- name: debug webpage
set_fact:
fact: "{{ webpage3 }}"
when:
- lb is defined
ignore_errors: yes
and my ansible version is ansible 2.2.1.0
So, do I have a problem with my webpage itself and is there's a solution to skip this error?
and after troubleshooting, I figured out that it fails because of a line starts with
<!-->
so how to skip this line with this character?
Thanks all, I have solved it by providing the dest in the uri module to a file then using sed to remove the unwanted characters and then registering the output to a new value to set_fact from and finally greping the needed value using a grep command. as it was hard to skip those chars in output