1

This is my setup. I have created a scm type git project and have my code there. My playbook is on that repository as well and it contains docker build and run command. In order to build my docker I should execute my build command where my docker file is located (in this case where ansible project clones /var/lib/awx/project). I want to get that path to my ansible playbook.

My playbook looks like this:

---
- hosts: all
  sudo: yes
  remote_user: ubuntu
  gather_facts: no

  tasks:
    - name : build docker
      become: yes
      become_user: root
      command : docker build -t "test-api" .
    - name: run docker
      become: yes
      become_user: root
      command : docker run -it -p 80:9001 --name api test-api

How can i achieve this?

David
  • 271
  • 1
  • 15
Frodo
  • 555
  • 1
  • 5
  • 15

2 Answers2

0

You can make use of vars for defining the variables:

---
- hosts: all
  sudo: yes
  remote_user: ubuntu
  gather_facts: no
  vars:
    file_path: "<your file path>" 

  tasks:
    - name : build docker
      become: yes
      become_user: root
      command : docker build -t "test-api" .
    - name: run docker
      become: yes
      become_user: root
      command : docker run -it -p 80:9001 --name api test-api

And In your command access that variable like "{{ file_path }}"

  • @Dasun Hettiarachchi: Please let me know if you need more information ? –  May 20 '18 at 11:26
  • yes. so i have to define the path in order to achieve this? is there any way i can get the path from ansbile variable without define it? for example it clone to path like this var/lib/awx/projects/_16__docker i have to check on the server every time i add a project – Frodo May 20 '18 at 11:31
  • In order to access the ansible variable you need to define either in playbook file. or if you use `roles` then under `vars`->`main.yaml`. –  May 20 '18 at 11:33
  • You can define the clone path in a variable and use that variable for cloning step and also build command –  May 20 '18 at 11:36
  • okay. im not familiar with roles. il test with variables in playbook – Frodo May 20 '18 at 11:38
  • @here i have different question, when i run update on scm on project i ran into error saying Fatal: Failed to resolve 'origin/04753df8240a4a186e6f0f6e177319fec1c3d242' as a valid revision. – Frodo May 20 '18 at 11:48
  • This is something related to error while `cloning` of your project –  May 20 '18 at 11:52
0

You can send the variable to the playbook when you execute the ansible-playbook command. This is what you could do:

 ansible-playbook my-playbook.yml -e"path='/var/lib/awx/project'"

Then just use it in the playbook as a normal variable: {{ path }}

This is useful if you decide to change the path. If you have any question about this, feel free to ask in the comments

David
  • 271
  • 1
  • 15
  • im using ansible tower, can i use the same? – Frodo May 21 '18 at 09:55
  • 1
    I don't know Ansible Tower, but I think it might be the same. [Go to this website for some examples](http://docs.ansible.com/ansible-tower/latest/html/towerapi/tower_cli.html#specify-extra-variables). I think the option "--extra-vars" is the same as the "-e" I used – David May 21 '18 at 13:56
  • yes, i used extra variables to map with playbook. it void me defining them in the playbook. great way. – Frodo May 22 '18 at 04:01
  • 1
    @DasunHettiarachchi if you have a big amount of variables, you can define them in a file. This is a good solution if you work with big variables or encrypted Ansible passwords – David May 22 '18 at 08:31
  • yes i'm working with lot of vars, can you encrypt vars and store in a file and will ansible able to decrepit and use? – Frodo May 22 '18 at 10:43
  • Yes you can. You can encrypt individual variables or even the hole file. All this done with [Ansible Vault](https://docs.ansible.com/ansible/2.4/vault.html#what-can-be-encrypted-with-vault). Also, if you save variables in a file (it must be in YAML syntax), you can include that file with `-e"@filename.yml"` – David May 22 '18 at 11:07