I have my directory structure as this
└── digitalocean ├── README.md ├── play.yml └── roles ├── bootstrap_server │ └── tasks │ └── main.yml ├── create_new_user │ └── tasks │ └── main.yml ├── update │ └── tasks │ └── main.yml └── vimserver ├── files │ └── vimrc_server └── tasks └── main.yml
When I am creating a user under the role create_new_user
, I was hard coding the user name as
--- - name: Creating a user named username on the specified web server. user: name: username state: present shell: /bin/bash groups: admin generate_ssh_key: yes ssh_key_bits: 2048 ssh_key_file: .ssh/id_rsa - name: Copy .ssh/id_rsa from host box to the remote box for user username become: true copy: src: ~/.ssh/id_rsa.pub dest: /home/usernmame/.ssh/authorized_keys mode: 0600 owner: username group: username
One way of solving this may be to create a var/main.yml
and put the username there. But I wanted something through which I can specify the username at play.yml
level. As I am also using the username in the role vimrcserver
.
I am calling the roles using play.yml
--- - hosts: testdroplets roles: - update - bootstrap_server - create_new_user - vimserver
Would a template work here in this case? Couldn't find much from these SO questions
The easiest way to pass Pass Variables value to Ansible Playbook in the command line is using the extra variables parameter of the “ansible-playbook” command. This is very useful to combine your Ansible Playbook with some pre-existent automation or script.
To solve this issue, we can use the Ansible extra vars feature. We can define a variable representing the hosts' group and specify its value when running the playbook. Now that we have an example playbook as above, we can pass the value to the “group” variable using the –extra-vars option while running the playbook.
I got it working by doing a
--- - hosts: testdroplets roles: - update - bootstrap_server - role: create_new_user username: username - role: vimserver username: username
on play.yml
Although would love to see a different approach then this
Docs: http://docs.ansible.com/ansible/playbooks_roles.html#roles
EDIT
I finally settled with a directory structure like
$ tree . ├── README.md ├── ansible.cfg ├── play.yml └── roles ├── bootstrap_server │ └── tasks │ └── main.yml ├── create_new_user │ ├── defaults │ │ └── main.yml │ └── tasks │ └── main.yml ├── update │ └── tasks │ └── main.yml └── vimserver ├── defaults │ └── main.yml ├── files │ └── vimrc_server └── tasks └── main.yml
Where I am creating a defaults/main.yml
file inside the roles where I need the usage of {{username}}
If someone is interested in the code,
You should be able to put username
in a vars
entry in play.yml.
Variables can also be split out into separate files.
Here is an example which shows both options:
- hosts: all vars: favcolor: blue vars_files: - /vars/external_vars.yml tasks: - name: this is just a placeholder command: /bin/echo foo
https://docs.ansible.com/ansible/playbooks_variables.html#variable-file-separation
Ansible seems to delight in having different ways to do the same thing, without having either a nice comprehensive reference, or a rationale discussing the full implications of each different approach :). If you didn't remember the above was possible (I'd completely forgotten vars_files
), the easiest option to find from the documentation might have been a third way, which is the most sophisticated one.
There's a prominent recommendation for ansible-examples. You can see a group_vars
directory, with files which automatically provide values for hosts according to their groups, including the magic all
group. The group_vars directory can be placed in the same directory as the playbook.
https://github.com/ansible/ansible-examples/tree/master/lamp_simple
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With