Reputation: 12141
I'm building a machine on ec2 using Ansible. But I also have vagrant to test the script locally.
Here's my playbook
- name: Provision ec2
hosts: all
user: vagrant
sudo: yes
gather_facts: true
vars:
gsutil_user_home: "vagrant"
If I want to use this playbook for EC2 I have to change the user
and gsutil_user_home
to be ubuntu
to get it to work. Is there anyway I can make that as a variable and tell Ansible which user it needs to run based on some argument?
Upvotes: 0
Views: 855
Reputation: 35129
I would suggest you to create folders say develop
, production
, staging
. Set hosts
files, group_vars
, hosts_vars
in there with correct variables and use them.
For example:
.
├── develop
│ └── hosts
├── production
│ └── hosts
├── test.yml
---
- hosts: localhost
gather_facts: no
connection: local
tasks:
- debug: var=ansible_ssh_user
[localhost]
127.0.0.1 ansible_ssh_user=vagrant
[localhost]
127.0.0.1 ansible_ssh_user=ec2-user
$ ansible-playbook -i develop/ test.yml
PLAY [localhost] **************************************************************
TASK: [debug var=ansible_ssh_user] ********************************************
ok: [127.0.0.1] => {
"var": {
"ansible_ssh_user": "vagrant"
}
}
PLAY RECAP ********************************************************************
127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0
$ ansible-playbook -i production/ test.yml
PLAY [localhost] **************************************************************
TASK: [debug var=ansible_ssh_user] ********************************************
ok: [127.0.0.1] => {
"var": {
"ansible_ssh_user": "ec2-user"
}
}
PLAY RECAP ********************************************************************
127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0
Upvotes: 2
Reputation: 13940
If these values are generally consistent for the duration of your playbooks, you'd usually set them as group/host vars in inventory, and have different inventories or groups for different environments. But if you really want to do it with vars and pass them in with -e
, that's fine too:
- name: Provision ec2
hosts: all
user: {{ my_ec2_user }}
sudo: yes
gather_facts: true
vars:
gsutil_user_home: {{ gsutil_user_home }}
Upvotes: 2