JSON - File Evaluations - Scenarios - Ansible Converted
===========================================================================
===========================================================================
====Another example of fetching the information
package play
default let_do := false
let_do = true {
resource := input[_]
resource.hosts == "databases"
}
Result :
{
"let_do": true
}
============
INPUT FILE
[
{
"name": "Update web servers",
"hosts": "webservers",
"remote_user": "root",
"tasks": [
{
"name": "Ensure apache is at the latest version",
"ansible.builtin.yum": {
"name": "httpd",
"state": "latest"
}
},
{
"name": "Write the apache config file",
"ansible.builtin.template": {
"src": "/srv/httpd.j2",
"dest": "/etc/httpd.conf"
}
}
]
},
{
"name": "Update db servers",
"hosts": "databases",
"remote_user": "root",
"tasks": [
{
"name": "Ensure postgresql is at the latest version",
"ansible.builtin.yum": {
"name": "postgresql",
"state": "latest"
}
},
{
"name": "Ensure that postgresql is started",
"ansible.builtin.service": {
"name": "postgresql",
"state": "started"
}
}
]
}
]
===
== Works
package play
default let_do := false
let_do {
info := input[_]
info.name == "Update web servers"
}
===
package play
default resource_name := false
resource_name {
resource := input[_]
info := resource.tasks[_]
info.name =="Ensure apache is at the latest version"
}
=====
[
{
"name": "Update web servers",
"hosts": "webservers",
"remote_user": "root",
"tasks": [
{
"name": "Ensure apache is at the latest version",
"ansible.builtin.yum": {
"name": "httpd",
"state": "latest"
}
},
{
"name": "Write the apache config file",
"ansible.builtin.template": {
"src": "/srv/httpd.j2",
"dest": "/etc/httpd.conf"
}
}
]
},
+++++++++++++Another scenarios
package play
default resource_name := false
resource_name {
resource := input[_]
info := resource.tasks[_]
info_2 := info["ansible.builtin.yum"]
info_2.name == "httpd"
}
INPUT file
[
{
"name": "Update web servers",
"hosts": "webservers",
"remote_user": "root",
"tasks": [
{
"name": "Ensure apache is at the latest version",
"ansible.builtin.yum": {
"name": "httpd",
"state": "latest"
}
},
{
"name": "Write the apache config file",
"ansible.builtin.template": {
"src": "/srv/httpd.j2",
"dest": "/etc/httpd.conf"
}
}
]
},
{
"name": "Update db servers",
"hosts": "databases",
"remote_user": "root",
"tasks": [
{
"name": "Ensure postgresql is at the latest version",
"ansible.builtin.yum": {
"name": "postgresql",
"state": "latest"
}
},
{
"name": "Ensure that postgresql is started",
"ansible.builtin.service": {
"name": "postgresql",
"state": "started"
}
}
]
}
]
====Another Scenario : https://play.openpolicyagent.org/p/W2IPPgEXqo
Policy file ..
package play
default allow_update := false
default allow_update_1 := false
default allow_update_2 := false
default allow_update_3 := false
default allow_update_4 := false
default allow_update_5 := false
allow_update = true {
resource := input[_]
resource.name == "Add new.foo.com as an A record with 3 IPs"
}
allow_update_1 = true {
resource := input[_]
dns_update := resource["community.dns.hosttech_dns_record_set"]
dns_update.ttl == 7200
}
allow_update_2 = true {
resource := input[_]
resource.name == "Add new.foo.com as an A record with 3 IPs"
dns_update := resource["community.dns.hosttech_dns_record_set"]
dns_update.ttl == 7200
}
allow_update_3 = true {
resource := input[_]
resource.name == "Update new.foo.com as an A record with a list of 3 IPs"
dns_update := resource["community.dns.hosttech_dns_record_set"]
dns_update.record == "new.foo.com"
}
allow_update_4 = true {
resource := input[_]
resource.name == "Update new.foo.com as an A record with a list of 3 IPs"
dns_update := resource["community.dns.hosttech_dns_record_set"]
dns_update.record == "new.foo.com"
dns_update.value[0] == "1.1.1.1"
dns_update.value[1] == "2.2.2.2"
dns_update.value[2] == "3.3.3.3"
}
# Iteration
allow_update_5 = true {
resource := input[_]
resource.name == "Update new.foo.com as an A record with a list of 3 IPs"
dns_update := resource["community.dns.hosttech_dns_record_set"]
dns_update.record == "new.foo.com"
some i
dns_update.value[i] == "3.3.3.3"
}
====INPUT FILE
[
{
"name": "Add new.foo.com as an A record with 3 IPs",
"community.dns.hosttech_dns_record_set": {
"state": "present",
"zone_name": "foo.com",
"record": "new.foo.com",
"type": "A",
"ttl": 7200,
"value": "1.1.1.1,2.2.2.2,3.3.3.3",
"hosttech_token": "access_token"
}
},
{
"name": "Update new.foo.com as an A record with a list of 3 IPs",
"community.dns.hosttech_dns_record_set": {
"state": "present",
"zone_name": "foo.com",
"record": "new.foo.com",
"type": "A",
"ttl": 7200,
"value": [
"1.1.1.1",
"2.2.2.2",
"3.3.3.3"
],
"hosttech_token": "access_token"
}
},
{
"name": "Retrieve the details for new.foo.com",
"community.dns.hosttech_dns_record_set_info": {
"zone_name": "foo.com",
"record": "new.foo.com",
"type": "A",
"hosttech_username": "foo",
"hosttech_password": "bar"
},
"register": "rec"
},
{
"name": "Delete new.foo.com A record using the results from the facts retrieval command",
"community.dns.hosttech_dns_record_set": {
"state": "absent",
"zone_name": "foo.com",
"record": "{{ rec.set.record }}",
"ttl": "{{ rec.set.ttl }}",
"type": "{{ rec.set.type }}",
"value": "{{ rec.set.value }}",
"hosttech_username": "foo",
"hosttech_password": "bar"
}
},
{
"name": "Add an AAAA record",
"community.dns.hosttech_dns_record_set": {
"state": "present",
"zone_name": "foo.com",
"record": "localhost.foo.com",
"type": "AAAA",
"ttl": 7200,
"value": "::1",
"hosttech_token": "access_token"
}
},
{
"name": "Add a TXT record",
"community.dns.hosttech_dns_record_set": {
"state": "present",
"zone_name": "foo.com",
"record": "localhost.foo.com",
"type": "TXT",
"ttl": 7200,
"value": "bar",
"hosttech_username": "foo",
"hosttech_password": "bar"
}
},
{
"name": "Remove the TXT record",
"community.dns.hosttech_dns_record_set": {
"state": "absent",
"zone_name": "foo.com",
"record": "localhost.foo.com",
"type": "TXT",
"ttl": 7200,
"value": "bar",
"hosttech_username": "foo",
"hosttech_password": "bar"
}
},
{
"name": "Add a CAA record",
"community.dns.hosttech_dns_record_set": {
"state": "present",
"zone_name": "foo.com",
"record": "foo.com",
"type": "CAA",
"ttl": 3600,
"value": [
"128 issue letsencrypt.org",
"128 iodef mailto:webmaster@foo.com"
],
"hosttech_token": "access_token"
}
},
{
"name": "Add an MX record",
"community.dns.hosttech_dns_record_set": {
"state": "present",
"zone_name": "foo.com",
"record": "foo.com",
"type": "MX",
"ttl": 3600,
"value": [
"10 mail.foo.com"
],
"hosttech_token": "access_token"
}
},
{
"name": "Add a CNAME record",
"community.dns.hosttech_dns_record_set": {
"state": "present",
"zone_name": "bla.foo.com",
"record": "foo.com",
"type": "CNAME",
"ttl": 3600,
"value": [
"foo.foo.com"
],
"hosttech_username": "foo",
"hosttech_password": "bar"
}
},
{
"name": "Add a PTR record",
"community.dns.hosttech_dns_record_set": {
"state": "present",
"zone_name": "foo.foo.com",
"record": "foo.com",
"type": "PTR",
"ttl": 3600,
"value": [
"foo.foo.com"
],
"hosttech_token": "access_token"
}
},
{
"name": "Add an SPF record",
"community.dns.hosttech_dns_record_set": {
"state": "present",
"zone_name": "foo.com",
"record": "foo.com",
"type": "SPF",
"ttl": 3600,
"value": [
"v=spf1 a mx ~all"
],
"hosttech_username": "foo",
"hosttech_password": "bar"
}
},
{
"name": "Add a PTR record",
"community.dns.hosttech_dns_record_set": {
"state": "present",
"zone_name": "foo.com",
"record": "foo.com",
"type": "PTR",
"ttl": 3600,
"value": [
"10 100 3333 service.foo.com"
],
"hosttech_token": "access_token"
}
}
]
Comments
Post a Comment