Skip to content

Commit 407a883

Browse files
Add '--run-ansible-roles' flag
1 parent c7020ca commit 407a883

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

bootstrap.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,36 @@ def install_ssh_key_from_string(foreman_ssh_key):
718718
output.close()
719719

720720

721+
def run_ansible_roles(foreman_fqdn, fqdn, job_invocation_name='Ansible Roles - Ansible Default'):
722+
"""
723+
Call 'Ansible Roles - Ansible Default' remote job API in order to trigger the execution of ansible roles.
724+
"""
725+
print_generic("Calling run_ansible_roles")
726+
jsondata = {"search": "name = \"%s\"" % job_invocation_name}
727+
myurl = "https://" + foreman_fqdn + ":" + API_PORT + "/api/job_templates/"
728+
print_running("Calling Foreman API to retrieve job template id of '%s'" % job_invocation_name)
729+
return_values = get_json(myurl, jsondata)
730+
731+
if 'subtotal' in return_values.keys() and \
732+
'results' in return_values.keys() and \
733+
return_values['subtotal'] == 1 and \
734+
return_values['results'][0]['name'] == job_invocation_name:
735+
job_template_id = return_values['results'][0]['id']
736+
737+
jsondata = {"job_invocation": {"job_template_id": job_template_id, "search_query": "name = %s" % fqdn, "targeting_type": "static_query"}}
738+
myurl = "https://" + foreman_fqdn + ":" + API_PORT + "/api/job_invocations/"
739+
print_running("Calling Foreman API to execute '%s' job" % job_invocation_name)
740+
return_values = post_json(myurl, jsondata)
741+
if 'id' in return_values.keys():
742+
print_success("Job template '%s' executed. Job id: %d" % (job_invocation_name, return_values['id']))
743+
else:
744+
print_error("Error during the execution of job template '%s'" % job_invocation_name)
745+
sys.exit(1)
746+
else:
747+
print_error("Job template '%s' not found" % job_invocation_name)
748+
sys.exit(1)
749+
750+
721751
class BetterHTTPErrorProcessor(urllib_basehandler):
722752
"""
723753
A substitute/supplement class to HTTPErrorProcessor
@@ -788,9 +818,9 @@ def call_api(url, data=None, method='GET'):
788818
sys.exit(2)
789819

790820

791-
def get_json(url):
821+
def get_json(url, jdata=None):
792822
"""Use `call_api` to place a "GET" REST API call."""
793-
return call_api(url)
823+
return call_api(url, data=jdata)
794824

795825

796826
def post_json(url, jdata):
@@ -1214,6 +1244,7 @@ def exec_service(service, command, failonerror=True):
12141244
parser.add_option("--rex-urlkeyfile", dest="remote_exec_url", help="HTTP/S location of a file containing one or more Foreman's SSH keys for remote execution.")
12151245
parser.add_option("--rex-apikeys", dest="remote_exec_apikeys", action="store_true", help="Fetch Foreman's SSH keys from the API.")
12161246
parser.add_option("--rex-authpath", dest="remote_exec_authpath", help="Full path to local authorized_keys file in order to install Foreman's SSH keys for remote execution. Default ~/.ssh/authorized_keys")
1247+
parser.add_option("--run-ansible-roles", dest="run_ansible_roles", action="store_true", help="Run ansible roles after registration.", default=False)
12171248
parser.add_option("--enablerepos", dest="enablerepos", help="Repositories to be enabled via subscription-manager - comma separated", metavar="enablerepos")
12181249
parser.add_option("--skip", dest="skip", action="append", help="Skip the listed steps (choices: %s)" % SKIP_STEPS, choices=SKIP_STEPS, default=[])
12191250
parser.add_option("--ip", dest="ip", help="IPv4 address of the primary interface in Foreman (defaults to the address used to make request to Foreman)")
@@ -1564,3 +1595,6 @@ def exec_service(service, command, failonerror=True):
15641595
else:
15651596
remote_exec_url = "https://" + str(options.foreman_fqdn) + ":9090/ssh/pubkey"
15661597
install_ssh_key_from_url(remote_exec_url)
1598+
1599+
if options.run_ansible_roles:
1600+
run_ansible_roles(options.foreman_fqdn, FQDN)

0 commit comments

Comments
 (0)