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

**URL:** <https://forum.ansible.com/t/bitbicket-ansible-tower-script-the-tower-cli-connection-help-please/28902>\
**Category:** AWX Project\
**Tags:** awx\
**Created:** [October 30, 2018, 10:56pm UTC](https://forum.ansible.com/t/bitbicket-ansible-tower-script-the-tower-cli-connection-help-please/28902 "2018-10-30T22:56:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_Williamson](https://avatars.discourse-cdn.com/v4/letter/i/96bed5/32.png) [@Ian\_Williamson](https://forum.ansible.com/u/Ian_Williamson)\
**Post date:** [October 30, 2018, 10:56pm UTC](https://forum.ansible.com/t/bitbicket-ansible-tower-script-the-tower-cli-connection-help-please/28902/1 "2018-10-30T22:56:24Z")

</div>

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/](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](http://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](http://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

---

<div class="post-metadata">

**Author:** ![Ian\_Williamson](https://avatars.discourse-cdn.com/v4/letter/i/96bed5/32.png) [@Ian\_Williamson](https://forum.ansible.com/u/Ian_Williamson)\
**Post date:** [November 4, 2018, 9:01pm UTC](https://forum.ansible.com/t/bitbicket-ansible-tower-script-the-tower-cli-connection-help-please/28902/2 "2018-11-04T21:01:17Z")

</div>

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-bitbucket-pipelines)

[https://www.ansible.com/integrations/devops-tools/atlassian](https://www.ansible.com/integrations/devops-tools/atlassian)

[https://bitbucket.org/AnsibleByRedHat/ansible-tower-pipelines/src/master/](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.

- 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.deb](http://kr.archive.ubuntu.com/ubuntu/pool/main/t/tcl8.6/libtcl8.6_8.6.8+dfsg-4_amd64.deb)

- wget [http://kr.archive.ubuntu.com/ubuntu/pool/universe/e/expect/tcl-expect\_5.45.4-2\_amd64.deb](http://kr.archive.ubuntu.com/ubuntu/pool/universe/e/expect/tcl-expect_5.45.4-2_amd64.deb)

- wget [http://kr.archive.ubuntu.com/ubuntu/pool/universe/e/expect/expect\_5.45.4-2\_amd64.deb](http://kr.archive.ubuntu.com/ubuntu/pool/universe/e/expect/expect_5.45.4-2_amd64.deb)

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

- apt-get install ./tcl-expect\_5.45.4-2\_amd64.deb -y

- apt-get install ./expect\_5.45.4-2\_amd64.deb -y

# - 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](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](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](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]

…
