In my playbooks, I SSH in as a non-root user, then use become to become root.
What is the Ansible variable name that stores the user that originally SSH'd into the box?
In Ansible < 2.0, I could use {{ ansible_ssh_user }} to access the username SSH'ing into the box.
Just tried with Ansible 2.0.2 and that returns null. I tried ansible_user as suggested by the FAQ, but that also returns null. I also tried ansible_user_id, but that returns the result of become, not the original user.
You can accesss this via ansible_env.SUDO_USER.
I tried a number of other variables, and almost all of them changed their values as soon as I used become on the remote node.
{{ ansible_user }} does actually work fine for me with Ansible 2.5:
with_items:
- root
- "{{ ansible_user }}"
Related
I am having issues resolving the dot (.) notation of pebble templating in Gatling to retrieve attributes of variables, such as Map key values.
Here's an example of a session attribute that I've defined:
session => session.set("location", Map("text" -> "Alabama"))
I'm trying to replace {{ location.text }} in the following json template file with the value "Alabama":
...
"text": "{{ location.text }}",
...
However, it looks like Gatling is unable to resolve the attribute, and therefore, the result is an empty string.
I am using version 3.2.1 of Gatling.
Any help would be appreciated, thanks!
Answered here: https://groups.google.com/forum/#!topic/gatling/23CuH88x1Vo
Since Pebble is a Java library...
after converting the map to a java.util.Map datatype using scala.collection.JavaConverters, I was able to successfully use Pebble dot (.) notation for traversing the map.
I'm currently making the transition from Puppet to Ansible and so far so good. Yet I want to automatize as much as possible.
I'm trying to use a with_items loop inside vars_files to load variable files based on a given list of items. Ansible complains about the syntax and I can't seem to find an example of a similar solution, only examples that use with_items inside tasks and roles.
For example:
vars_files:
- ["vars/{{ item }}-{{ ansible_fqdn }}.yml", "vars/{{ item }}-{{ system_environment }}.yml", "vars/{{ item }}.yml"]
with_items:
- php
- nginx
The goal here is to loop the second line for as long as there are items in with_items using an array to fallback on the next item if it can't find the given file (which works).
Not sure if this is at all possible, but I wanted to ask before taking another direction.
with_items, or in general all loops, are a feature of tasks. vars_files though is no task. So it won't work the way you tried it and the short answer would be: It is not possible.
I don't know of a clean way to solve your exact problem. A custom vars plugin might be an option. But vars plugin work on a global level while your vars seem to be used in a role.
A custom lookup plugin might be a solution if solving this on task level is an option for you. The lookup plugin takes your input, checks for presence of the files and returns an array of the files which need to be include. This then can be used with the include_vars module.
- include_vars: "{{ item }}"
with_my_custom_plugin:
- php
- nginx
An ugly solution would be to combine the with_items loop with a with_first_found loop. Though, since you cannot directly nest loops, you need to work with an include.
- include: include_vars.yml
with_items:
- php
- nginx
And inside include_vars.yml you then can use with_first_found with the include_vars module.
- include_vars: "{{ item }}"
with_first_found:
- vars/{{ item }}-{{ ansible_fqdn }}.yml
- vars/{{ item }}-{{ system_environment }}.yml
- vars/{{ item }}.yml
Putting this in a separate answer to expand on the group and host variables solution I eventually came up with (cc #udondan).
Basically I group all my hosts in my inventory file under several sub and parent groups no matter what. Then I create files for group vars whenever applicable so it follows a certain order of precedence (first is highest and overrides all others, last applies to all hosts and can be overridden down the chain):
task vars > playbook vars > host_vars > web/database-local > local > web/database > all
That way I can define variables for all hosts to use (all), just web/database (mostly production values), all local servers (local group), all local web/database servers, et cetera, or per-host (the standard host_vars). Of course playbook and task vars override these further. All of this following the Ansible guidelines.
An example of a local inventory (replace default with your hostname or IP, add as many as you like per group, x-local can be omitted if this would be a production inventory):
[web-local]
default
[database-local]
default
[local:children]
web-local
database-local
[web:children]
web-local
[database:children]
database-local
Then my group_vars folder with directories for each inventory group and variables split into files to keep it structured (could just have one database-local.yaml file for the database-local group for instance instead of folders and split YAML-files):
group_vars/
all/
always_applied_variables.yaml
swap.yaml
web/
database/
database_only_variables.yaml
database-production/
production_database_variables.yaml
production/
random_production_only_variables.yaml
local/
users.yaml
web-local/
database-local/
local_database_variables.yaml
host_vars/
default/
php.yaml
mysql.yaml
other_specific_host_variables.yaml
Hope this is somewhat clear. I'd be happy to answer any questions.
I'm checking out GAE Managed VMs using app.yaml, as described here https://cloud.google.com/appengine/docs/managed-vms/java/configuring-your-app-with-app-yaml
When I do
env_variables:
java.util.logging.config.file: 'WEB-INF/logging.properties'
I get exception
google.appengine.api.yaml_errors.EventError: Value 'java.util.logging.config.file' for key in EnvironmentVariables does not match expression '^(?:[a-zA-Z_][a-zA-Z0-9_]*)$'
Is there any way to specify custom logging.properties through app.yaml?
Try this:
env_variables:
JAVA_USER_OPTS: -Djava.util.logging.config.file=webapps/root/WEB-INF/logging.properties
The env_variables section in app.yaml is for setting environment variables. Dots are not allowed in their names, so the exception makes sense.
You are trying to set java.util.logging.config.file, which is a system property, not an environment variable. To set it you need to provide -Djava.util.logging.config.file=<value> argument when starting Java. GAE Flexible image provides JAVA_USER_OPTS environment variable to customise Java command line, so you can use it to customise JUL settings (as is now also recommended in the image readme).
Also, WEB-INF/logging.properties value didn't work for me as the current dir is $JETTY_BASE, not $JETTY_BASE/webapps/root.
The other answer is no longer correct. The property name is now named JAVA_OPTS
Full 'secret' variable names here
https://github.com/GoogleCloudPlatform/jetty-runtime#providing-loggingproperties-via-the-web-application
This is the correct setting now:
env_variables:
JAVA_OPTS: -Djava.util.logging.config.file=webapps/root/WEB-INF/logging.properties
For the Generally Available Flexible Environment, use this format.
env_variables:
JETTY_ARGS: -Djava.util.logging.config.file=WEB-INF/logging.properties
See here .
I am writing a playbook that creates a network with a name I assign. Later on in the playbook I need to access the IP that is assigned by this network task, so I get it from the hostvars. So for example it if I was calling the network 'my_website' the value I would be targeting in hostvars would be
server: "{{hostvars.localhost.rax_nfs.results[0].success[0].rax_addresses.my_website[0].addr}}"
This is fine, but I want to name the network based on the contents of a variable passed in by a var file to make it reusable across multiple setups, and then still be able to get that IP back, so
network_label: "{{ my_website }}"
server: "{{hostvars.localhost.rax_nfs.results[0].success[0].rax_addresses.network_label[0].addr}}"
Obviously this doesn't work as it just assigns a string. How do I use network_label as the key inside another variable? Like in php something like
$array[$variable], or $object->$variable
Is this possible?
Not sure what the playbook looks like but does accessing the IP based on the gathered facts not work in your context? Example: {{ ansible_eth0.ipv4.address }}
Alternatively on the task that creates the network you could register that as a variable for later access using that variable name.
You can also access elements of an array in a variable: {{ var_name[0] }}
I've created an Ansible playbook that creates a cloud instance and then installs some programs on the instance. I want to run this playbook multiple times (without using a bash script). Is it possible to use a loop to loop over those two tasks together (I.E. One loop for two tasks?). All I've been able to find so far is one loop for each individual task
An update:
In 2.0 you are able to use with_ loops and task includes (but not playbook includes), this adds the ability to loop over the set of tasks in one shot. There are a couple of things that you need to keep in mind, a included task that has it’s own with_ loop will overwrite the value of the special item variable. So if you want access to both the include’s item and the current task’s item you should use set_fact to create a alias to the outer one.:
- include_tasks: test.yml
with_items:
- 1
- 2
- 3
in test.yml:
- set_fact: outer_loop="{{item}}"
- debug: msg="outer item={{outer_loop}} inner item={{item}}"
with_items:
- a
- b
- c
Source: Ansible Docs
No that's currently not possible. with_items used to work with the include statement in previous versions of Ansible but was unfortunately dropped.
Though it will be brought back in Ansible 2.0, see slide 14/15 of What's New in v2 - AnsibleFest London 2015
You could try to work with the v2 branch from github, the feature should be available in there.
What you can do with 1.9.1 is to move your tasks into a role and reference this role multiple times in your playbook.
I managed to do this by recursively including the same yaml file based on a condition. Here is the gist: https://gist.github.com/ParagDoke/5ddfc3d5647ce9b0110d1b9790090092. Effectively, in your playbook, include a file with some vars:
- name: Invoke poller
vars:
some_condition: '"failed" not in response.content and response.json.status=="running"'
include_tasks: status-poller.yml
Then in status-poller.yml, include itself:
- include_tasks: includes/status-poller.yml
when: some_condition