Ansible. How to pass argument from local to host machine - ansible

I'm new to Ansible.
Currently I am working with a remote host that use Capistrano as package management agent.
When I run deploy script as follow:
- name: build source
shell: |
echo "bundle exec cap branch=staging stg deploy"
tags:
- build_source
Conifg capistrano is here
set :branch, lambda {
branch = Capistrano::CLI.ui.ask("[cap] Branch or Tag (default `master`): ")
branch.empty? ? "master" : branch
}
Therefore, the Ansible script will be stuck at ask step.
I wonder if there is any way to passing argument from an Ansible on local to capistrano on host machine
Many thanks in advance

Thank you.
I was looking for expect module as #Zeitounator suggested

Related

How do I make .gitlab-ci.yml execute different ansible playbooks in different folders/subfolders?

I just started learning about NetDevOps.
All the examples containing demos of GitLAb + Ansible show how to execute "deploy.yml" from Ansible from the .gitlab-ci.yml.
However, when I see a general network equipment based Ansible tutorial the author executes different Ansible cookbook .yml files, for example sites.yml from the root, deploy.yml from another subfolder, interfaces.yml from another subfolder.
Can someone give me an example of how I would execute the different Ansible playbook .yml files on demand?
I.e. - When it detects changes in .yml in a folder, run that .yml file under that folder?
lets say you have an ansible repo and a .gitlab-ci.yml in the root of it.
.gitlab-ci.yml
ansible/cookbook1
ansible/cookbook2
For each cookbook you could create a Gitlab job
execute_cookbook1:
script:
- ...ANSIBLE COMMANDS...
...
only:
changes:
- ansible/cookbook1/**/*.yml
execute_cookbook2:
script:
- ...ANSIBLE COMMANDS...
...
only:
changes:
- ansible/cookbook2/**/*.yml
This way when you push your code to ansible repo, Gitlab will detect which cookbook changed. And run the according job
You can use in your GitLab job complex rules like if, changes, and exists, in the same rule.
The rule evaluates to true only when all included keywords evaluate to true.
For example:
docker build:
script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
rules:
- if: '$VAR == "string value"'
changes: # Include the job and set to when:manual if any of the follow paths match a modified file.
- Dockerfile
- docker/scripts/*
when: manual
allow_failure: true
In your case, keep when: manual and changes: *.yml in order to trigger the job manually, if yml Ansible playbook are changed.

In jenkins what will be my Playbook path ? how to set up Inventory there are 3 option for the same

I have installed the Jenkins and ansible on my AWS Instance centos
and am trying to call my ansible playbook from jenkins ?
I dont know how to set up : what will be my Playbook path ?
how to set up Inventory there are 3 option for the same (
1. Do not specify Inventory
2. File or host list
3.Inline content)
Help me out so that i call my ansible to run from jenkins
I am giving you the simple example, please ask in the comments if you need some specific help. Let's say, you job name is "stackoverflow" then you can put the ansible-scripts directory inside the /var/lib/jenkins/jobs/stackoverflow/workspace and use it in your jenkins job like this:
After that you can use the shell environment:
Once you will configure this job then you can use it as you are normally using the ansible from the command line:
Please adjust the settings as per your environment.Hope this will help you

Execute shell using arguments file from git

I've managed to set up a minimal Ansible playbook to execute some scripts on my machines:
- name: Execute CLI on remote servers
hosts: webserver
tasks:
- name: Get metrics
shell: /home/user1/bin/cli.sh --file=script.cli
The only issue is that this relies on the filesystem to store the scripts. I'd like to store my script in a repository (such as git) and pass a reference to it as argument to the shell.Something like:
shell: /home/user1/bin/cli.sh --file=ssh://git#github.com/mylogin/script.cli
Any suggestion is highly appreciated!
Not a very elegant solution, but you can use the Ansible git module (http://docs.ansible.com/ansible/git_module.html) to first clone the repository that contains your scripts on your target machine(s) (webserver) and then reference those files from the shell module.

Running playbooks automatically

I am learning ansible recently and I am a hard time figuring out, how to configure ansible to run the playbooks on its own after a certain interval. ? Just like puppet does.
Ansible works in a different way compared to Puppet.
Puppet PULLS for configuration changes from a central place and applies changes on the remote host that asked for it.
Ansible by design works different. You PUSH the changes (from any control machine that has SSH access to remote hosts - usually your own computer) to remote hosts.
You can make Ansible work in pull mode also but it's not how Ansible was designed to be used.
You can see this answer for more information: Can't run Ansible in daemon-mode
If you would like the host to automatically run playbooks on itself (localhost) you would basically use ansible-pull script + crontab.
If you want to run the playbooks once after a certain interval, you can use the at command.
Example
# Schedule a command to execute in 20 minutes as root.
- at: command="ls -d / > /dev/null" count=20 units="minutes"
Further information available on ansible official site.
This is what Ansible Tower is for. It'll run after being pinged on its API, by schedule, manually, and so on.

best way to pull ansible playbook down and run it

What is the best way to pull a playbook down from bitbucket and execute it using ansible?
description:
I wrote a playbook and have it checked into bitbucket. I have docker which is spinning up ansible(image) and I need it to pull down the playbook and run a command against it. Anyone have experience with this?? Any help would be appreciated.
You can use ansible-pull http://docs.ansible.com/ansible/playbooks_intro.html or just do a regular git checkout and then run the code. Both work well with a public repo but will require git credentials if the repo is private. If you want to do this with a private repo you will want some form of secrets management for storing a token or ssh key to authenticate with.
Alternatively, at build time you can copy the files you need into the Docker image from your dev machine or CI tool and run the playbook, instead of doing that at runtime. This will decrease the boot time of your docker image as well.
You can simply do:
# clone repo
git clone https://bitbucket.org/user/repo.git && cd repo
docker run --rm=true -v `pwd`:/repo:rw ansible/ubuntu14.04-ansible /bin/bash -c "ansible-playbook -i /repo/hosts /repo/main.yml"
PS:
I am using with Travis CI such a way: https://github.com/weldpua2008/ansible-pycharm/blob/master/.travis.yml

Resources