snippetMinor
Ansible: How to run ad-hoc command with multiple environnements?
Viewed 0 times
environnementswithmultiplehowcommandansiblerunhoc
Problem
Given the following architecture:
The content of ansible.cfg is:
The content of the hosts/production and hosts/staging file is the same:
staging/group_vars/all.yml, mygroup.yml, mygroup2.yml contains all:
staging/host_vars/mhost1.yml and mhost2.yml contains both (with their respective ip):
staging_playbook.yml contains:
In the production environment, the production_playbook.yml is similar:
The only differences are in production/host_vars where I have different IP addresses.
If I run:
ansible-playbook staging/staging_playbook.yml
or
ansible-playbook production/production_playbook.yml
it all works fine so I guess the architecture is correct.
Now, my question is: how can I target a specific host in a specific environment with an Ansible ad-hoc command?
for example:
which is not working and gives the output:
```
mhost1 | UNREACHABLE! => {
"chan
├── ansible.cfg
├── hosts
│ ├── production
│ └── staging
├── production
│ ├── group_vars
│ │ ├── all.yml
│ │ ├── mygroup.yml
│ │ └── mygroup2.yml
│ ├── host_vars
│ │ ├── mhost1.yml
│ │ └── mhost2.yml
│ └── production_playbook.yml
└── staging
├── group_vars
│ ├── all.yml
│ ├── mygroup.yml
│ └── mygroup2.yml
├── host_vars
│ ├── mhost1.yml
│ └── mhost2.yml
└── staging_playbook.ymlThe content of ansible.cfg is:
[defaults]
inventory=hostsThe content of the hosts/production and hosts/staging file is the same:
[all]
[mygroup]
mhost1
[mygroup2]
mhost2staging/group_vars/all.yml, mygroup.yml, mygroup2.yml contains all:
ansible_user: rootstaging/host_vars/mhost1.yml and mhost2.yml contains both (with their respective ip):
ansible_host: xxx.xxx.xxx.xxstaging_playbook.yml contains:
---
- hosts: all
tasks:
- name: ping all in staging
ping:
- hosts: mhost1
tasks:
- name: ping mhost1 in staging
ping:
- hosts: mhost2
tasks:
- name: ping mhost2 in staging
ping:In the production environment, the production_playbook.yml is similar:
---
- hosts: all
tasks:
- name: ping all in production
ping:
- hosts: mhost1
tasks:
- name: ping mhost1 in production
ping:
- hosts: mhost2
tasks:
- name: ping mhost2 in production
ping:The only differences are in production/host_vars where I have different IP addresses.
If I run:
ansible-playbook staging/staging_playbook.yml
or
ansible-playbook production/production_playbook.yml
it all works fine so I guess the architecture is correct.
Now, my question is: how can I target a specific host in a specific environment with an Ansible ad-hoc command?
for example:
ansible mhost1 -i hosts/staging -m pingwhich is not working and gives the output:
```
mhost1 | UNREACHABLE! => {
"chan
Solution
One of the options would be to change the current directory. This simplifies the structure and lets you keep common configuration and inventory for production and staging. For example, to run the commands,
Make the changes below
-
Link ansible.cfg to both production/ansible.cfg and staging/ansible.cfg
-
Link hosts to both production/hosts and staging/hosts
-
You can rename both production_playbook.yml and staging_playbook.yml to playbook.yml
WARNING
In a critical environment separate production and staging physically.
- either change the working directory for all commands
shell> cd staging
shell> ansible-playbook playbook.yml
shell> ansible mhost1 -m ping- , or change the working directory for each command
shell> (cd staging; ansible-playbook playbook.yml)
shell> (cd staging; ansible mhost1 -m ping)Make the changes below
- Use inventory file from the current directory. Put into the ansible.cfg
inventory=$PWD/hosts-
Link ansible.cfg to both production/ansible.cfg and staging/ansible.cfg
-
Link hosts to both production/hosts and staging/hosts
-
You can rename both production_playbook.yml and staging_playbook.yml to playbook.yml
├── ansible.cfg
├── hosts
├── production
│ ├── group_vars
│ │ ├── all.yml
│ │ ├── mygroup.yml
│ │ └── mygroup2.yml
│ ├── host_vars
│ │ ├── mhost1.yml
│ │ └── mhost2.yml
| ├── ansible.cfg -> ../ansible.cfg
│ ├── hosts -> ../hosts
│ └── playbook.yml
└── staging
├── group_vars
│ ├── all.yml
│ ├── mygroup.yml
│ └── mygroup2.yml
├── host_vars
│ ├── mhost1.yml
│ └── mhost2.yml
├── ansible.cfg -> ../ansible.cfg
├── hosts -> ../hosts
└── playbook.ymlWARNING
In a critical environment separate production and staging physically.
Code Snippets
shell> cd staging
shell> ansible-playbook playbook.yml
shell> ansible mhost1 -m pingshell> (cd staging; ansible-playbook playbook.yml)
shell> (cd staging; ansible mhost1 -m ping)inventory=$PWD/hosts├── ansible.cfg
├── hosts
├── production
│ ├── group_vars
│ │ ├── all.yml
│ │ ├── mygroup.yml
│ │ └── mygroup2.yml
│ ├── host_vars
│ │ ├── mhost1.yml
│ │ └── mhost2.yml
| ├── ansible.cfg -> ../ansible.cfg
│ ├── hosts -> ../hosts
│ └── playbook.yml
└── staging
├── group_vars
│ ├── all.yml
│ ├── mygroup.yml
│ └── mygroup2.yml
├── host_vars
│ ├── mhost1.yml
│ └── mhost2.yml
├── ansible.cfg -> ../ansible.cfg
├── hosts -> ../hosts
└── playbook.ymlContext
StackExchange DevOps Q#16142, answer score: 2
Revisions (0)
No revisions yet.