Looping in hostvars
I'm wondering if it is possible to perform a loop in the hostvars folder when using Ansible?
Here is what I've tried but haven't had success in making it work - or is it just not possible to do?
---
list_pool: 'list ltm pool {{ items }}'
with_items:
- 'abc123'
- 'def456'
I would use the "list_pool" variable in a playbook afterward:
- name: List pool
bigip_command:
server: "{{ some_server }}"
user: "{{ some_user }}"
password: "{{ some_password }}"
commands:
- "{{ list_pool }}"
validate_certs: no
delegate_to: localhost
ansible f5
add a comment |
I'm wondering if it is possible to perform a loop in the hostvars folder when using Ansible?
Here is what I've tried but haven't had success in making it work - or is it just not possible to do?
---
list_pool: 'list ltm pool {{ items }}'
with_items:
- 'abc123'
- 'def456'
I would use the "list_pool" variable in a playbook afterward:
- name: List pool
bigip_command:
server: "{{ some_server }}"
user: "{{ some_user }}"
password: "{{ some_password }}"
commands:
- "{{ list_pool }}"
validate_certs: no
delegate_to: localhost
ansible f5
add a comment |
I'm wondering if it is possible to perform a loop in the hostvars folder when using Ansible?
Here is what I've tried but haven't had success in making it work - or is it just not possible to do?
---
list_pool: 'list ltm pool {{ items }}'
with_items:
- 'abc123'
- 'def456'
I would use the "list_pool" variable in a playbook afterward:
- name: List pool
bigip_command:
server: "{{ some_server }}"
user: "{{ some_user }}"
password: "{{ some_password }}"
commands:
- "{{ list_pool }}"
validate_certs: no
delegate_to: localhost
ansible f5
I'm wondering if it is possible to perform a loop in the hostvars folder when using Ansible?
Here is what I've tried but haven't had success in making it work - or is it just not possible to do?
---
list_pool: 'list ltm pool {{ items }}'
with_items:
- 'abc123'
- 'def456'
I would use the "list_pool" variable in a playbook afterward:
- name: List pool
bigip_command:
server: "{{ some_server }}"
user: "{{ some_user }}"
password: "{{ some_password }}"
commands:
- "{{ list_pool }}"
validate_certs: no
delegate_to: localhost
ansible f5
ansible f5
asked Nov 21 '18 at 0:42
PetePete
105
105
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Not sure what you mean when you say you want to loop over hostvars folder.
From what I can interpret from your tasks is: "You need to execute big-ip command list ltm <pool-name>
for multiple pools in the list list_pool
"
If that's what you're after, this should work:
- name: Set list_pool fact
set_fact:
list_pool: "{{ list_pool | default() + [item] }}"
with_items:
- 'abc123'
- 'def456'
- name: List pool
bigip_command:
server: "{{ some_server }}"
user: "{{ some_user }}"
password: "{{ some_password }}"
commands:
- "list ltm {{ item }}"
validate_certs: no
delegate_to: localhost
with_items: "{{ list_pool }}"
Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.
– Pete
Nov 21 '18 at 1:49
Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.
– Anant Naugai
Nov 21 '18 at 2:40
So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?
– Pete
Nov 21 '18 at 14:50
Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.
– Anant Naugai
Nov 26 '18 at 2:31
add a comment |
I got this working with the following solution:
hostvars file would look like this:
---
pre_checks:
checks:
pool:
- name: "pool_123"
- name: "pool_456"
...
And the play would look like this:
--output truncated---
- name: Fetch device host_vars
set_fact:
device_config: "{{ ((lookup('file','{{playbook_dir}}/host_vars/{{inventory_hostname}}.yml')) | from_yaml) }}"
- name: Check pool
bigip_command:
server: "{{ inventory_hostname }}"
user: "{{ remote_username }}"
password: "{{ remote_passwd }}"
commands:
- "list ltm pool {{ item }}"
validate_certs: no
with_items:
- "{{ device_config.pre_checks | json_query('checks.pool[*].name') }}"
delegate_to: localhost
when: "'active' in Active_LTM['stdout'][0]"
register: Pre_Checks
- name: Verify pool
debug: var=item
with_items: "{{ Pre_Checks.results | map(attribute='stdout_lines') | list }}"
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403735%2flooping-in-hostvars%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Not sure what you mean when you say you want to loop over hostvars folder.
From what I can interpret from your tasks is: "You need to execute big-ip command list ltm <pool-name>
for multiple pools in the list list_pool
"
If that's what you're after, this should work:
- name: Set list_pool fact
set_fact:
list_pool: "{{ list_pool | default() + [item] }}"
with_items:
- 'abc123'
- 'def456'
- name: List pool
bigip_command:
server: "{{ some_server }}"
user: "{{ some_user }}"
password: "{{ some_password }}"
commands:
- "list ltm {{ item }}"
validate_certs: no
delegate_to: localhost
with_items: "{{ list_pool }}"
Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.
– Pete
Nov 21 '18 at 1:49
Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.
– Anant Naugai
Nov 21 '18 at 2:40
So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?
– Pete
Nov 21 '18 at 14:50
Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.
– Anant Naugai
Nov 26 '18 at 2:31
add a comment |
Not sure what you mean when you say you want to loop over hostvars folder.
From what I can interpret from your tasks is: "You need to execute big-ip command list ltm <pool-name>
for multiple pools in the list list_pool
"
If that's what you're after, this should work:
- name: Set list_pool fact
set_fact:
list_pool: "{{ list_pool | default() + [item] }}"
with_items:
- 'abc123'
- 'def456'
- name: List pool
bigip_command:
server: "{{ some_server }}"
user: "{{ some_user }}"
password: "{{ some_password }}"
commands:
- "list ltm {{ item }}"
validate_certs: no
delegate_to: localhost
with_items: "{{ list_pool }}"
Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.
– Pete
Nov 21 '18 at 1:49
Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.
– Anant Naugai
Nov 21 '18 at 2:40
So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?
– Pete
Nov 21 '18 at 14:50
Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.
– Anant Naugai
Nov 26 '18 at 2:31
add a comment |
Not sure what you mean when you say you want to loop over hostvars folder.
From what I can interpret from your tasks is: "You need to execute big-ip command list ltm <pool-name>
for multiple pools in the list list_pool
"
If that's what you're after, this should work:
- name: Set list_pool fact
set_fact:
list_pool: "{{ list_pool | default() + [item] }}"
with_items:
- 'abc123'
- 'def456'
- name: List pool
bigip_command:
server: "{{ some_server }}"
user: "{{ some_user }}"
password: "{{ some_password }}"
commands:
- "list ltm {{ item }}"
validate_certs: no
delegate_to: localhost
with_items: "{{ list_pool }}"
Not sure what you mean when you say you want to loop over hostvars folder.
From what I can interpret from your tasks is: "You need to execute big-ip command list ltm <pool-name>
for multiple pools in the list list_pool
"
If that's what you're after, this should work:
- name: Set list_pool fact
set_fact:
list_pool: "{{ list_pool | default() + [item] }}"
with_items:
- 'abc123'
- 'def456'
- name: List pool
bigip_command:
server: "{{ some_server }}"
user: "{{ some_user }}"
password: "{{ some_password }}"
commands:
- "list ltm {{ item }}"
validate_certs: no
delegate_to: localhost
with_items: "{{ list_pool }}"
answered Nov 21 '18 at 1:16
Anant NaugaiAnant Naugai
34039
34039
Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.
– Pete
Nov 21 '18 at 1:49
Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.
– Anant Naugai
Nov 21 '18 at 2:40
So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?
– Pete
Nov 21 '18 at 14:50
Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.
– Anant Naugai
Nov 26 '18 at 2:31
add a comment |
Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.
– Pete
Nov 21 '18 at 1:49
Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.
– Anant Naugai
Nov 21 '18 at 2:40
So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?
– Pete
Nov 21 '18 at 14:50
Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.
– Anant Naugai
Nov 26 '18 at 2:31
Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.
– Pete
Nov 21 '18 at 1:49
Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.
– Pete
Nov 21 '18 at 1:49
Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.
– Anant Naugai
Nov 21 '18 at 2:40
Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.
– Anant Naugai
Nov 21 '18 at 2:40
So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?
– Pete
Nov 21 '18 at 14:50
So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?
– Pete
Nov 21 '18 at 14:50
Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.
– Anant Naugai
Nov 26 '18 at 2:31
Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.
– Anant Naugai
Nov 26 '18 at 2:31
add a comment |
I got this working with the following solution:
hostvars file would look like this:
---
pre_checks:
checks:
pool:
- name: "pool_123"
- name: "pool_456"
...
And the play would look like this:
--output truncated---
- name: Fetch device host_vars
set_fact:
device_config: "{{ ((lookup('file','{{playbook_dir}}/host_vars/{{inventory_hostname}}.yml')) | from_yaml) }}"
- name: Check pool
bigip_command:
server: "{{ inventory_hostname }}"
user: "{{ remote_username }}"
password: "{{ remote_passwd }}"
commands:
- "list ltm pool {{ item }}"
validate_certs: no
with_items:
- "{{ device_config.pre_checks | json_query('checks.pool[*].name') }}"
delegate_to: localhost
when: "'active' in Active_LTM['stdout'][0]"
register: Pre_Checks
- name: Verify pool
debug: var=item
with_items: "{{ Pre_Checks.results | map(attribute='stdout_lines') | list }}"
add a comment |
I got this working with the following solution:
hostvars file would look like this:
---
pre_checks:
checks:
pool:
- name: "pool_123"
- name: "pool_456"
...
And the play would look like this:
--output truncated---
- name: Fetch device host_vars
set_fact:
device_config: "{{ ((lookup('file','{{playbook_dir}}/host_vars/{{inventory_hostname}}.yml')) | from_yaml) }}"
- name: Check pool
bigip_command:
server: "{{ inventory_hostname }}"
user: "{{ remote_username }}"
password: "{{ remote_passwd }}"
commands:
- "list ltm pool {{ item }}"
validate_certs: no
with_items:
- "{{ device_config.pre_checks | json_query('checks.pool[*].name') }}"
delegate_to: localhost
when: "'active' in Active_LTM['stdout'][0]"
register: Pre_Checks
- name: Verify pool
debug: var=item
with_items: "{{ Pre_Checks.results | map(attribute='stdout_lines') | list }}"
add a comment |
I got this working with the following solution:
hostvars file would look like this:
---
pre_checks:
checks:
pool:
- name: "pool_123"
- name: "pool_456"
...
And the play would look like this:
--output truncated---
- name: Fetch device host_vars
set_fact:
device_config: "{{ ((lookup('file','{{playbook_dir}}/host_vars/{{inventory_hostname}}.yml')) | from_yaml) }}"
- name: Check pool
bigip_command:
server: "{{ inventory_hostname }}"
user: "{{ remote_username }}"
password: "{{ remote_passwd }}"
commands:
- "list ltm pool {{ item }}"
validate_certs: no
with_items:
- "{{ device_config.pre_checks | json_query('checks.pool[*].name') }}"
delegate_to: localhost
when: "'active' in Active_LTM['stdout'][0]"
register: Pre_Checks
- name: Verify pool
debug: var=item
with_items: "{{ Pre_Checks.results | map(attribute='stdout_lines') | list }}"
I got this working with the following solution:
hostvars file would look like this:
---
pre_checks:
checks:
pool:
- name: "pool_123"
- name: "pool_456"
...
And the play would look like this:
--output truncated---
- name: Fetch device host_vars
set_fact:
device_config: "{{ ((lookup('file','{{playbook_dir}}/host_vars/{{inventory_hostname}}.yml')) | from_yaml) }}"
- name: Check pool
bigip_command:
server: "{{ inventory_hostname }}"
user: "{{ remote_username }}"
password: "{{ remote_passwd }}"
commands:
- "list ltm pool {{ item }}"
validate_certs: no
with_items:
- "{{ device_config.pre_checks | json_query('checks.pool[*].name') }}"
delegate_to: localhost
when: "'active' in Active_LTM['stdout'][0]"
register: Pre_Checks
- name: Verify pool
debug: var=item
with_items: "{{ Pre_Checks.results | map(attribute='stdout_lines') | list }}"
answered Feb 15 at 19:47
PetePete
105
105
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403735%2flooping-in-hostvars%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown