Bitbicket -> Ansible Tower, script the tower-cli connection, help please

Hello AWX community

I am trying to CI/CD from JIRA → Bitbicket → Ansible Tower.
I think the documentation on how to pipeline from Bitbicket is out of date now tower-cli uses Oauth2.
Can anyone please guide me on how to script the setup of the tower-cli connection to tower.

There is official documentation here:
https://bitbucket.org/AnsibleByRedHat/ansible-tower-pipelines/src/master/
And it provides an example “tower.bash”

This example references the pre tower 3.3 Oauth2 login commands:
$ tower-cli config host tower.example.com
$ tower-cli config username user
$ tower-cli config password pass

To connect to the current tower version you need to use
$ tower-cli config host tower.example.com
$ tower-cli login username
Password:

I am not very experienced at this stuff but it looks to me like, In the legacy mode a variable can be used to set the password in to the ~/.tower-cli.cfg file.
I had a play with the “tower-cli login” command. I don’t know of a way to answer the prompt for the password. I think this can not be handled in the script. I think it produces the error
“/usr/local/lib/python2.7/getpass.py:83: GetPassWarning: Can not control echo on the terminal. passwd = fallback_getpass(prompt, stream)Warning: Password input may be echoed.”

Can someone with better coding than me please confirm that I am correct or not.
Please suggest a way forward.
Is there such thing as an Oath2 command in bash that can take a variable for the password?

Best Regards
Ian

Yay! I did it! W00t FTW

Hello To whom it may concern at Ansible / Red Hat / Atlassian

The following documentation does not demonstrate a working configuration and is misleading:

https://www.ansible.com/integrations/devops-tools/atlassian-bitbucket-pipelines

https://www.ansible.com/integrations/devops-tools/atlassian

https://bitbucket.org/AnsibleByRedHat/ansible-tower-pipelines/src/master/

Below is a working configuration. Please review this and update your documentation accordingly.

With this configuration when a commit happens to the Bitbucket repository, pipeline will call a job from Ansible Tower named “Tower_Test”

Set variables in Bitbucket Pipeline

ID Tower_Test

host 10.10.10.10

username tower_user

password password

Note: select the lock icon when creating the password variable so that it is hidden

In the repo create the following files

bitbucket-pipelines.yml

This is a sample build configuration for the Ansible Tower integration.

You can specify a custom docker image from Dockerhub

for your build environment.

image: python:2.7

pipelines:

default:

  • step:

script: # Modify the commands below to build your repository.

- apt-get install -y gdebi && sudo gdebi ./expect_5.45.4-2_amd64.deb -y

  • chmod 755 tower.bash

  • chmod 755 tower.exp

  • chmod 755 qtower

  • ./tower.bash

Note: repositories available to the python:2.7 image do not include expect. expect and its dependencies are sourced from an asia/pacific Ubuntu mirror. This is less than optimal, please suggest how to make this better? Like a better Docker image.

tower.bash

#!/bin/bash -ex

#configur tower-cli settings

echo “Configuring Tower Settings”

tower-cli config host $host

tower-cli config username $username

tower-cli config verify_ssl false

lets use expect to enter the pasword without echoing and get the Oath2 token from tower

./tower.exp $password

echo " current configuration settings:"

cat ~/.tower_cli.cfg

Let’s run a tower-cli job

tower-cli job launch --job-template $ID --monitor

tower.exp

#!/usr/bin/expect -f

set MYVAR [lindex $argv 0]

set send_slow {1 .1}

set timeout -1

spawn ./qtower

match_max 100000

expect -exact "Password: "

sleep .1

send – $MYVAR

send -s – “\r”

sleep .1

send -s – “\r”

expect eof

qtower

tower-cli login $username

Turn on Bitbucket Pipeline

The resulting pipeline log looks like this

Build setup

pip install ansible-tower-cli

wget http://kr.archive.ubuntu.com/ubuntu/pool/main/t/tcl8.6/libtcl8.6_8.6.8+dfsg-4_amd64.deb2s

wget http://kr.archive.ubuntu.com/ubuntu/pool/universe/e/expect/tcl-expect_5.45.4-2_amd64.deb1s

wget http://kr.archive.ubuntu.com/ubuntu/pool/universe/e/expect/expect_5.45.4-2_amd64.deb1s

apt-get install ./libtcl8.6_8.6.8+dfsg-4_amd64.deb -y9s

apt-get install ./tcl-expect_5.45.4-2_amd64.deb -y<1s

apt-get install ./expect_5.45.4-2_amd64.deb -y<1s

chmod 755 tower.bash<1s

chmod 755 tower.exp<1s

chmod 755 qtower

./tower.bash

  • ./tower.bash

  • echo ‘Configuring Tower Settings’

  • tower-cli config host 10.10.10.10

Configuring Tower Settings

Configuration updated successfully.

  • tower-cli config username tower_user

Configuration updated successfully.

  • tower-cli config verify_ssl false

Configuration updated successfully.

  • ./tower.exp $password

spawn ./qtower

Password:

{

“id”: 36,

“type”: “o_auth2_access_token”,

“url”: “/api/v2/tokens/36/”,

“created”: “2018-11-02T02:11:59.819468Z”,

“modified”: “2018-11-02T02:11:59.837189Z”,

“description”: “Tower CLI”,

“user”: 1,

“refresh_token”: null,

“application”: null,

“expires”: “3018-03-05T02:11:59.812174Z”,

“scope”: “write”

}

Configuration updated successfully.

  • echo ’ current configuration settings:’

  • cat /root/.tower_cli.cfg

current configuration settings:

[general]

host = 10.10.10.10

username = tower_user

verify_ssl = false

oauth_token = xxxxxxxxxxxxxxxxxxxxxx

  • tower-cli job launch --job-template Tower_Test --monitor

Current status: running

------Starting Standard Out Stream------

SSH password:

PLAY [all] *********************************************************************

TASK [Check the date on the server.] *******************************************

changed: [ansible]

changed: [node-1]