How to get the first element of a list from the output of setup module in Ansible?
To get first item of the list: – debug: msg: “First item: {{ ansible_processor[0] }}” Or: – debug: msg: “First item: {{ ansible_processor | first }}”
To get first item of the list: – debug: msg: “First item: {{ ansible_processor[0] }}” Or: – debug: msg: “First item: {{ ansible_processor | first }}”
Hows this – hosts: localhost vars: war_files: server1: – file1.war – file2.war server2: – file1.war – file2.war – file3.war tasks: – name: Loop over subelements of the dictionary debug: msg: “Key={{ item.0.key }} value={{ item.1 }}” loop: “{{ war_files | dict2items | subelements(‘value’) }}” dict2items, subelements filters are coming in Ansible 2.6. FYI, if a … Read more
Use check_mode, register and failed_when in concert. This fails the task if the lineinfile module would make any changes to the file being checked. Check_mode ensures nothing will change even if it otherwise would. – name: “Ensure /tmp/my.conf contains ‘127.0.0.1’” lineinfile: name: /tmp/my.conf line: “127.0.0.1” state: present check_mode: yes register: conf failed_when: (conf is changed) … Read more
I stumbled upon this error running ansible on Ubuntu 15.10 server, because it ships with Python 3.4.3 and ansible requires Python 2. This is how my provision.yml looks now: – hosts: my_app sudo: yes remote_user: root gather_facts: no pre_tasks: – name: ‘install python2’ raw: sudo apt-get -y install python tasks: – name: ‘ensure user {{ … Read more
I think I got there in the end. The task is like this: – name: Populate genders set_fact: genders: “{{ genders|default({}) | combine( {item.item.name: item.stdout} ) }}” with_items: “{{ people.results }}” It loops through each of the dicts (item) in the people.results array, each time creating a new dict like {Bob: “male”}, and combine()s that … Read more
There are multiple ways to do this and from your question it’s nor clear what you need. 1. If you need environment variable to be defined PER TASK ONLY, you do this: – hosts: dev tasks: – name: Echo my_env_var shell: “echo $MY_ENV_VARIABLE” environment: MY_ENV_VARIABLE: whatever_value – name: Echo my_env_var again shell: “echo $MY_ENV_VARIABLE” Note … Read more
There is a workaround which may help. You may “register” results for each set_fact iteration and then map that results to list: — – hosts: localhost tasks: – name: set fact set_fact: foo_item=”{{ item }}” with_items: – four – five – six register: foo_result – name: make a list set_fact: foo=”{{ foo_result.results | map(attribute=”ansible_facts.foo_item”) | … Read more
As ant31 already pointed out you can use the synchronize module to this. By default, the module transfers files between the control machine and the current remote host (inventory_host), however that can be changed using the task’s delegate_to parameter (it’s important to note that this is a parameter of the task, not of the module). … Read more
So how can I properly save the results of the operations in variable names based on the list I iterate over? You don’t need to. Variables registered for a task that has with_items have different format, they contain results for all items. – hosts: localhost gather_facts: no vars: images: – foo – bar tasks: – … Read more
Per Ansible FAQ: Another rule is ‘moustaches don’t stack’. We often see this: {{ somevar_{{other_var}} }} The above DOES NOT WORK, if you need to use a dynamic variable use the hostvars or vars dictionary as appropriate: {{ hostvars[inventory_hostname][‘somevar_’ + other_var] }} So in your case: – debug: msg={{hostvars[inventory_hostname][Component].community_release_num}} Or: – debug: msg={{vars[Component].community_release_num}} Or (since … Read more