Ansible: want to use the var in hosts - ansible

I'm using the Ansile 2.0.1.0 And Docker.
I want to connect to the container to create a Docker container in Ansbile.
Also the name of the container want to manage in the variable.
But When executing I get error message.
main.yml
- name: data container
hosts: localhost
roles:
- role: docker
tasks:
- debug: var=docker_hostname
- name: hogefuga
hosts: "{{docker_hostname}}"
connection: docker
roles:
- hogefuga
role/dockers/tasks/main.yml
- name: Create Container
shell: "docker run --name={{docker_hostname}}"
- name: host add DockerContainer
add_host: name={{ docker_hostname }} group="dockers"
group_vars/all
docker_hostname: hoge
Error Message
TASK [debug] *******************************************************************
ok: [localhost] => {
"docker_hostname": "hoge"
}
ERROR! 'docker_hostname' is undefined
I'm guessing that it for using the var to hosts.
How can I fix the error?

I found Anser.
Is it possible to define playbook-global variables in Ansible?
I did not know that can set the group_name to hosts.
- name: hogefuga
hosts: dockers
connection: docker
roles:
- hogefuga

Related

How to change the host dynamically in ansible playbook

I need to change the host dynamically in ansible playbook
Below is my sample playbook
---
- name: Deployment Playbook
hosts: “{{Servers}}”
tasks:
- name: deployment
shell: "deploy.sh {{DEPLOY_NAME}}"
In above play I need to change the server with respect of DEPLOY_NAME
Example
If {{DEPLOY_NAME}}=APP
THEN {{Servers}} = 172.17.65.17
If {{DEPLOY_NAME}}=SCRIPT
THEN {{Servers}} = 172.17.65.66
Previously we passed this as inventory from AWX. But now we need to handle this on playbook.
So please help me on this issue
---
- name: Deployment Playbook targetting Servers_1, will be skipped if DEPLOY_NAME is not APP
hosts: “{{Servers_1}}”
tasks:
- name: deployment
shell: "deploy.sh {{DEPLOY_NAME}}"
when: DEPLOY_NAME == 'APP'
- name: Deployment Playbook targetting Servers_1, will be skipped if DEPLOY_NAME is not SCRIPT
hosts: “{{Servers_2}}”
tasks:
- name: deployment
shell: "deploy.sh {{DEPLOY_NAME}}"
when: DEPLOY_NAME == 'SCRIPT'
I don't think you can do that. What I think it may work for you, is to do this instead:
---
- name: Deployment Playbook
hosts: localhost
tasks:
- name: deployment
shell: ssh root#{{ item.server }} deploy.sh {{ item.app }}
loop:
- { server: 'server1', app: 'app_1' }
- { server: 'server1', app: 'app_1' }
You could even improve this, by using that "inventory from awx", loading it as a "vars_files" which contains this list. So your final loop will just iterate over that list. Like this:
---
- name: Deployment Playbook
hosts: localhost
vars_files:
- your_list_file.yml
tasks:
- name: deployment
shell: ssh root#{{ item.server }} deploy.sh {{ item.app }}
loop: "{{ your_list_variable }}"

Running playbook in host A to get value and pass on the value to task where it execute locally

Execute a Task to get the hostname in windows machine and store in variable called HstName. Then I have to use the variable to create a folder in GCP dynamically. My GCP module works only on localhost
Ansible version : 2.7
Task 1: Target host windows, to get the hostname
Task 2: "gcp_storage" module uses localhost for creating an object inside storage bucket.
hosts: win
gather_facts: no
serial: 1
connection: winrm
become_method: runas
become_user: admin
vars:
ansible_become_password: '}C7I]8zH#5cPDE8'
gcp_object: "gcp-shareddb-bucket/emeraldpos/stage/"
tasks:
- name: hostname
win_command: hostname
register: Hstname
- set_fact:
Htname: "{{ Hstname.stdout | trim}}"
- debug:
msg: "{{Htname}}"
- hosts: localhost
gather_facts: no
tasks:
- name: Create folder under EMERALDPOS
gc_storage:
bucket: gcp-shareddb-bucket
object: "{{gcp_object}}{{Htname}}"
mode: create
region: us-west2
project: sharedBucket-228905
gs_access_key: "NAKJSAIUS231219237LOKM"
gs_secret_key: "XABSAHSKASAJS:OAKSAJNDADIAJAJD:N<MNMN"
Ansible should create gcp-shareddb-bucket/emeraldpos/stage/HOSTNAME in gcp storage object

How to add host to group in Ansible Tower inventory?

How can I add a host to a group using tower_group or tower_host modules?
The following code creates a host and a group, but they are unrelated to each other:
---
- hosts: localhost
connection: local
gather_facts: false
tasks:
- tower_inventory:
name: My Inventory
organization: Default
state: present
tower_config_file: "~/tower_cli.cfg"
- tower_host:
name: myhost
inventory: My Inventory
state: present
tower_config_file: "~/tower_cli.cfg"
- tower_group:
name: mygroup
inventory: My Inventory
state: present
tower_config_file: "~/tower_cli.cfg"
Docs mention instance_filters parameter ("Comma-separated list of filter expressions for matching hosts."), however do not provide any usage example.
Adding instance_filters: myhost to the tower_group task has no effect.
I solved it using Ansible shell module and tower-cli. I Know that create a ansible module is better than it, but to a fast solution...
- hosts: awx
vars:
tasks:
- name: Create Inventory
tower_inventory:
name: "Foo Inventory"
description: "Our Foo Cloud Servers"
organization: "Default"
state: present
- name: Create Group
tower_group:
inventory: "Foo Inventory"
name: Testes
register: fs_group
- name: Create Host
tower_host:
inventory: "Foo Inventory"
name: "host"
register: fs_host
- name: Associate host group
shell: tower-cli host associate --host "{{fs_host.id}}" --group "> {{fs_group.id}}"
This isn't natively available in the modules included with Tower, which are older and use the deprecated tower-cli package.
But it is available in the newer AWX collection, which uses the awx CLI, as long as you have a recent enough Ansible (2.9 should be fine).
In essence, install the awx collection through a requirements file, or directly like
ansible-galaxy collection install awx.awx -p ./collections
Add the awx.awx collection to your playbook
collections:
- awx.awx
and then use the hosts: option to tower_group:.
- tower_group:
name: mygroup
inventory: My Inventory
hosts:
- myhost
state: present
You can see a demo playbook here.
Be aware though that you may need preserve_existing_hosts: True if your group already contains other hosts. Unfortunately there does not seem to be an easy way to remove a single host from a group.
In terms of your example this would probably work:
---
- hosts: localhost
connection: local
gather_facts: false
collections:
- awx.awx
tasks:
- tower_inventory:
name: My Inventory
organization: Default
state: present
tower_config_file: "~/tower_cli.cfg"
- tower_host:
name: myhost
inventory: My Inventory
state: present
tower_config_file: "~/tower_cli.cfg"
- tower_group:
name: mygroup
inventory: My Inventory
state: present
tower_config_file: "~/tower_cli.cfg"
hosts:
- myhost

Ansible Playbook skip some play on multiple play playbook file

I have an ansible playbook YAML file which contains 3 plays.
The first play and the third play run on localhost but the second play runs on remote machine as you can see an example below:
- name: Play1
hosts: localhost
connection: local
gather_facts: false
tasks:
- ... task here
- name: Play2
hosts: remote_host
tasks:
- ... task here
- name: Play3
hosts: localhost
connection: local
gather_facts: false
tasks:
- ... task here
I found that, on the first run, Ansible Playbook executes Play1 and Play3 and skips Play2. Then, I try to run again, it executes all of them correctly.
What is wrong here?
The problem is that, at Play2, I use ec2 inventor like tag_Name_my_machine but this instance was not created yet, because it would be created at Play1's task.
Once Play1 finished, it will run Play2 but no host found so it silently skip this play.
The solution is to create dynamic inventor and manually register at Play1's tasks:
Playbook may look like this:
- name: Play1
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Launch new ec2 instance
register: ec2
ec2: ...
- name: create dynamic group
add_host:
name: "{{ ec2.instances[0].private_ip }}"
group: host_dynamic_lastec2_created
- name: Play2
user: ...
hosts: host_dynamic_lastec2_created
become: yes
become_method: sudo
become_user: root
tasks:
- name: do something
shell: ...
- name: Play3
hosts: localhost
connection: local
gather_facts: false
tasks:
- ... task here

Ansible Playbook not resolving variable correctly

I am using ansible and having issues with running the playbook
site.yml:
---
- name: Test Ansible Playbook
hosts: '{{ myhosts }}'
sudo: no
roles:
- myRole
And the myRole file:
---
- name: Node script
hosts: '{{ myhosts }}'
sudo: no
tasks:
- name: Start Tomcat
service: name=tomcat state=started enabled=yes
And when I try running the command: ansible-playbook "-e 'myhosts=myHostName'" site.yml
I get the following error:
ERROR: hosts is not a legal parameter in an Ansible task or handler
ERROR: hosts is not a legal parameter in an Ansible task or handler
And that is the problem. Inside a tasks file of a role you may define tasks - nothing else. Which hosts these tasks will be executed on is defined in the playbook.
Your tasks file should only contain this:
---
- name: Start Tomcat
service: name=tomcat
state=started
enabled=yes
...

Resources