---

- name: Install pip
  yum:
    name: python2-pip
    state: present
  become: true

- name: Install git
  yum:
    name: git
    state: present
  become: true

- name: Install Tower CLI
  pip:
    name: ansible-tower-cli
  become: true

- name: Install ManageIQ module
  pip:
    name: manageiq-client
  become: true

- name: Install python2-boto
  yum:
    name: python2-boto
    state: present
  become: true

- name: Install pyOpenSSL
  yum:
    name: pyOpenSSL
    state: present
  become: true

- name: Install python-netaddr
  yum:
    name: python-netaddr
    state: present
  become: true

- name: Install python-six
  yum:
    name: python-six
    state: present
  become: true

- name: Install python2-boto3
  yum:
    name: python2-boto3
    state: present
  become: true

- name: Install python-click
  yum:
    name: python-click
    state: present
  become: true

- name: Install python-httplib2
  yum:
    name: python-httplib2
    state: present
  become: true

- name: Create tower organization
  tower_organization:
    name: "Default"
    description: "Set to Default since the trial license only allows one organization. You can change it if you have deep pockets"
    state: present
    tower_config_file: "~/.tower_cli.cfg"

- name: Add tower project for openshift-ansible-contrib
  tower_project:
    name: "openshift-ansible-contrib"
    description: "sync openshift-ansible-contrib"
    organization: "Default"
    scm_url: https://github.com/strategicdesignteam/openshift-ansible-contrib.git
    scm_type: git
    scm_branch: master
    scm_update_on_launch: true
    state: present
    tower_config_file: "~/.tower_cli.cfg"

- name: Add tower project for ansible-redhat-access-insights-client
  tower_project:
    name: "ansible-redhat-access-insights-client"
    description: "sync ansible-redhat-access-insights-client"
    organization: "Default"
    scm_url: https://github.com/strategicdesignteam/ansible-redhat-access-insights-client.git
    scm_type: git
    scm_branch: master
    scm_update_on_launch: true
    state: present
    tower_config_file: "~/.tower_cli.cfg"

- name: Add tower project for cfme-ansible
  tower_project:
    name: "cfme-ansible"
    description: "sync cfme-ansible"
    organization: "Default"
    scm_url: https://github.com/strategicdesignteam/cfme-ansible.git
    scm_type: git
    scm_branch: master
    scm_update_on_launch: true
    state: present
    tower_config_file: "~/.tower_cli.cfg"

- name: Sync project openshift-ansible-contrib
  command: tower-cli project update -n openshift-ansible-contrib

- name: Sync project ansible-redhat-access-insights-client
  command: tower-cli project update -n ansible-redhat-access-insights-client

- name: Sync project cfme-ansible
  command: tower-cli project update -n cfme-ansible

- name: Add tower credential for machine
  tower_credential:
    name: aws-privkey
    kind: ssh
    become_method: sudo
    description: aws-privkey
    organization: "Default"
    state: present
    ssh_key_data: "{{ AWS_MACHINE_SSH_KEY }}"
    username: ec2-user
    tower_config_file: "~/.tower_cli.cfg"

- name: Add tower credential for aws
  tower_credential:
    name: ec2
    kind: aws
    description: ec2
    organization: "Default"
    state: present
    username: "{{ AWS_KEY }}"
    password: "{{ AWS_SECRET }}"
    tower_config_file: "~/.tower_cli.cfg"

- name: Add tower inventory
  tower_inventory:
    name: "aws-inventory"
    description: "Tower inventory for AWS"
    organization: "Default"
    state: present
    tower_config_file: "~/.tower_cli.cfg"

- name: Add tower group
  tower_group:
    name: aws
    description: "Tower Group for AWS"
    source: ec2
    credential: ec2
    inventory: "aws-inventory"
    source_vars:
      regions: all
      regions_exclude: us-gov-west-1,cn-north-1
      vpc_destination_variable: private_ip_address
      route53: True
      rds: False
      elasticache: False
      hostname_variable: tag_Name
    state: present
    tower_config_file: "~/.tower_cli.cfg"

- name: Get Inventory Source ID
  shell: curl -s -k -u {{ TOWER_USER }}:{{ TOWER_PASSWORD }} https://{{ TOWER_HOSTNAME }}/api/v1/inventory_sources/ | python -m json.tool | grep -m 1 id |awk -F":" '{print $2}' |awk -F"," '{print $1}' |sed 's/^[ \t]*//;s/[ \t]*$//'
  register: myoutput

- name: Create aws-infrastructure job template
  become: true
  tower_job_template:
    name: aws-infrastructure
    job_type: run
    inventory: aws-inventory
    project: openshift-ansible-contrib
    playbook: "reference-architecture/aws-ansible/playbooks/infrastructure.yaml"
    machine_credential: aws-privkey
    cloud_credential: ec2
    state: present
    tower_config_file: "~/.tower_cli.cfg"

- name: Create openshift_create_httpd_file job template
  tower_job_template:
    name: create_httpd_file
    job_type: run
    inventory: aws-inventory
    project: openshift-ansible-contrib
    playbook: "reference-architecture/ansible-tower-integration/create_httpd_file/create_httpd_file.yaml"
    machine_credential: aws-privkey
    cloud_credential: ec2
    state: present
    tower_config_file: "~/.tower_cli.cfg"

- name: Create redhat-access-insights client job template
  tower_job_template:
    name: redhat-access-insights-client
    job_type: run
    inventory: aws-inventory
    become_enabled: yes
    project: ansible-redhat-access-insights-client
    playbook: "redhat-access-insights-client.yaml"
    machine_credential: aws-privkey
    cloud_credential: ec2
    state: present
    tower_config_file: "~/.tower_cli.cfg"

- name: Create aws-openshift-install job template
  tower_job_template:
    name: aws-openshift-install
    job_type: run
    inventory: aws-inventory
    become_enabled: yes
    project: openshift-ansible-contrib
    playbook: "reference-architecture/aws-ansible/playbooks/openshift-install.yaml"
    machine_credential: aws-privkey
    cloud_credential: ec2
    state: present
    tower_config_file: "~/.tower_cli.cfg"

- name: Create aws-openshift-cfme-install job template
  tower_job_template:
    name: aws-openshift-cfme-install
    job_type: run
    inventory: aws-inventory
    become_enabled: no
    project: cfme-ansible
    playbook: "cfme_ose_install.yaml"
    machine_credential: aws-privkey
    cloud_credential: ec2
    state: present
    tower_config_file: "~/.tower_cli.cfg"

- name: Create aws-openshift-cfme-ocp-provider job template
  tower_job_template:
    name: aws-openshift-cfme-ocp-provider
    job_type: run
    inventory: aws-inventory
    become_enabled: no
    project: cfme-ansible
    playbook: "cfme_ose_aws_provision.yaml"
    machine_credential: aws-privkey
    cloud_credential: ec2
    state: present
    tower_config_file: "~/.tower_cli.cfg"

- name: Create workflow-ocp-aws-install
  command: tower-cli workflow create --name="workflow-ocp-aws-install" --organization="Default" --description="A workflow for deploying OCP on AWS" -e @workflow-ocp-aws-install-extravars.yaml

- name: Replace inventory source
  lineinfile:
    path: schema.yml
    regexp: 'REPLACEME'
    line: 'aws (aws-inventory - {{ myoutput.stdout }})'

- name: Replace inventory source second time
  lineinfile:
    path: schema.yml
    regexp: 'ONEMORETIME'
    line: 'aws (aws-inventory - {{ myoutput.stdout }})'

- name: Create a schema for workflow-ocp-aws-install
  command: tower-cli workflow schema workflow-ocp-aws-install @schema.yml

- name: Reset schema file in case we run again, again
  lineinfile:
    path: schema.yml
    regexp: '^(.*)inventory_source'
    line: '            - inventory_source: ONEMORETIME'

- name: Reset schema file in case we run again
  lineinfile:
    path: schema.yml
    regexp: '^(.*)inventory_source.*(?<!\ONEMORETIME)$'
    line: '  - inventory_source: REPLACEME'
