I need to take all related jobs for a job template. API can only return up to 200 results per page.
Has anyone handled API pagination with the URI module?
How can I simply iterate over the number of pages with a variable named page_number?
below my code:
- name: List related jobs for job template
uri:
url: http://localhost{{ api_for_job_template.json.related.jobs }}?page_size=200&page={{ page_number }}
force_basic_auth: yes
user: "{{ user }}"
password: "{{ password }}"
body_format: json
register: jobs_info
Ok, I found a way how to get all pages.
In one task I'm taking count of jobs from API and dividing it by number of records returned per page. Next in uri iterate page by page with "with_sequence". Just needed to round up it and convert to int.
- name: List related jobs for job template
uri:
url: http://localhost{{ api_for_job_template.json.related.jobs }}?page_size={{ page_size }}&page={{ item }}
force_basic_auth: yes
user: "{{ user }}"
password: "{{ password }}"
body_format: json
register: jobs_info
with_sequence: start=1 end="{{ end_at }}"
vars:
- end_at: "{{ ((jobs_count.json.count / 20) | round(0,'ceil')) | int }}"
Related
I tried to find a way to print both an API request and response to a log file. But Ansible URI module does not provide the request data.
Does anyone know any mechanism to print the request and response to file in Ansible?
example:
name: update cms
uri:
url: '{{ cms_api.msg }}'
method: POST
body_format: json
body:
api_password: "{{ api_password }}"
name: "{{ item.Name }}"
ip_address: "{{ item.IPAddress }}"
subnet_mask: "{{ item.SubnetMask }}"
router: "{{ item.Router }}"
ip_range: "{{ item.IPAddresses }}"
options: "{{ item.Options }}"
pool_name: "{{ item.PoolName}}"
validate_certs: no
return_content: yes
register: dhcp_subnet_response
loop: "{{ ip_addrs_1.list }}"
delegate_to: localhost
I need to print above request and response both to a file. Since this api calling as a loop, I need to print request|response both for each request.
Since your post contains several different questions you may have a look into the general documentation first
Registering Variables
Return Values - Common
uri module – Interacts with webservices - Return Values and their Examples
After reading and regarding
But Ansible URI module not providing request data.
you may then have a look into the following example playbook
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Get response with 'uri' module
uri:
url: "https://example.com"
method: GET
body_format: raw # default
return_content: true # default 'false'
status_code: 200
register: result
- name: Show result
debug:
var: result
resulting into the content of https://example.com, an IANA-managed Reserved Domain.
Does anyone know any mechanism to print ...
The page content itself is accessible via
- name: Show content
debug:
msg: "{{ result.content }}"
print ... to file in Ansible?
and you can Write variable to a file in Ansible or Ansible - Save registered variable to file.
I'm trying to generate random passwords for set of users, display those password and send them via email.
But after using it in the user module, and later trying to send it as email the password variable creates a new password string.
I want to either append the passwords to the user list.
This is what I have tried so far:
vars:
USER_ID_details:
- user_id: testid1
real_full_name: TESTID_1
groups: wheel
email_id: abc#abc.com
- user_id: randnme
groups: wheel
real_full_name: TESTID_2
email_id: abc#abc.com
id_pass: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}"
- name: create Linux user as per specification
user:
name: "{{ item.user_id }}"
password: "{{ id_pass | password_hash('sha512') }}"
loop: "{{ USER_ID_details }}"
when:
- os_type == "RedHat"
- name: Send the details of the user id and password to the email address
mail:
subject: "*Confidential: {{ item.user_id }} user id created on {{ affected_host }}"
body: "Hello, User id {{ item.user_id }} created on server {{ affected_host }} \n password: {{ id_pass }}"
to: "{{ item.email_id }}"
loop: "{{ USER_ID_details }}"
You are already generating a random password in your id_pass variable.
To generate a random password for every user, do this:
- set_fact:
USER_ID_details: |
{{ USER_ID_details | map('combine', { "password": "{{ lookup('password', '/dev/null chars=ascii_lowercase,digits length=15') }}"}) }}
# you need this, otherwise you will see different passwords every time you read the list
- set_fact:
USER_ID_details: "{{ USER_ID_details }}"
You will now have a field called password in every item of your list. You can use that to set the user's password and send it to them.
How can we add multiple Configuration Items (CIs) in ServiceNow incident using ansible snow_record module? I tried looping multiple CIs in snow_record-> data-> cmdb_ci but it appears to update only one CI in the ticket and not adding multiple CIs in the Affected CIs list.
- snow_record:
username: "{{ snow_user }}"
password: "{{ snow_password }}"
instance: "{{ snow_instance }}"
state: present
number: "INC0XXX"
data:
cmdb_ci: example1.com
#loop:
# - example1.com
# - example2.com
I think I found the solution, actually cmdb_ci: is for adding the primary CI in the ticket. To add additional CIs we need to use task_ci table and pass the additional CI name and the ticket number, it is working fine now. :)
- snow_record:
username: "{{ snow_user }}"
password: "{{ snow_password }}"
instance: "{{ snow_instance }}"
state: present
table: task_ci
data:
ci_item: "{{ item }}"
task: "INCXXXX"
loop:
- example1.com
- example2.com
i have a ansible-playbook that generates all elements output from remote server, and being saved into variables using set_fact.
i want to try and write each output into separate files using copy module with loop.
set_fact works to store the content but i am struggling to get them specified under copy module using loop.
- name: Playbook to get element details from server
uri:
url: https://10.10.10.19/rest/v1.1/nucli?SetType={{ item }}
body_format: raw
method: GET
headers:
Content-Type: application/xml
Accept: application/xml
Authorization: Bearer password
return_content: yes
register: fileout
with_items:
- My_var1
- My_var2
- My_var3
- My_var4
- set_fact:
My_var1: "{{ fileout.results[0].content }}"
My_var2: "{{ fileout.results[1].content }}"
My_var3: "{{ fileout.results[2].content }}"
My_var4: "{{ fileout.results[3].content }}"
- copy:
content: {{ item }}
dest: "/users/ansible/vars/{{ item }}.txt"
delegate_to: localhost
loop:
- My_var1
- My_var2
- My_var3
- My_var4
Playbook execution:
all steps executed successfully. but in copy module content: {{ item }} is not using set_fact variables, instead they are just copying string My_var1... etc.
Could anyone help me how to use loop for set_fact variable names? So that i can store the content that i have saved from set_fact (example - My_var1: "{{ fileout.results[0].content }}" )
Thanks in advance.
I have the following with_nested:
- name: Create solr schema for solr_cores
uri:
url: http://{{ cassandra_cluster_ips.split(',') | random }}:8983/solr/admin/cores?action={{ solr_core_action }}&core={{ item[1] }}.{{ item[0] }}
timeout: "{{ solr_create_timeout }}"
sudo: True
with_nested:
- "{{ solr_cores }}"
- "{{ client_names }}"
I want to change to my extra vars from:
#solr_cores: ['dom_chunk_meta', 'dom', 'tra_chunk_meta','tra','dom_difference_results','me_output']
#solr_core_action: "reload"
to:
solr_cores: [{‘dom_difference_results’:‘create’}, {‘dom’:‘reload’},
{‘tra’:‘reload’}, {‘me_output’:‘reload’}]
I looked at subelements, but don’t know how to pass it as a simple dictionary list so that I can set them into uri up to access it.
I am not 100% sure what you are trying to do but with_dict might be the one to use. If this doesn't work for you, comment and I will adjust the answer.
In your vars/inventory:
solr:
- name: core1
create_timeout: 30
action: reload
- name: core2
action: create
create_timeout: 60
In your playbook:
- name: Create solr schema for solr_cores
uri:
url: http://{{ cassandra_cluster_ips.split(',') | random }}:8983/solr/admin/cores?action={{ item.value.core_action }}&core={{ item.value.name }}.{{ item.value.other }}
timeout: "{{ item.value.create_timeout }}"
sudo: True
with_dict: "{{ solr }}"