ansible-inventory

Ansible read multiple variables with same name from vars_file

不想你离开。 提交于 2020-01-05 13:00:30
问题 In my ~/ip_vars_file , I have ip : 10.20.30 ip : 10.20.31 ip : 10.20.32 This is created with lineinfile, lineinfile: line="ip{{':'}} {{item.public_ip}}" dest="{{ansible_env.HOME}}/ip_vars_file}}" with_items: servers.tagged_instances #servers is registered in previous task I am unable to read all three ips as with_items. I get only the last IP in my playbook. --- - hosts: localhost tasks: - name: print ips debug: var: item with_items: "{{ ip }}" vars_files: - ~/ip_vars_file The output I am

Is there a way to validate the number of hosts for a group in Ansible Inventory file?

时光怂恿深爱的人放手 提交于 2020-01-04 05:31:17
问题 My requirement is shown below, I have an Ansible inventory file which is divided into some groups based on the components shown below: [all] node1 node2 node3 node4 [webapp] node3 node4 [ui] node1 Is there a way to validate the number of hosts for a group in inventory file if condition fails then playbook should not run ? My condition is: ui group should always have only one host. Ex: [ui] node1 -- condition check pass proceed with playbook execution [ui] node1 node2 -- condition fails should

Ansible: How to declare global variable within playbook?

空扰寡人 提交于 2020-01-02 03:24:19
问题 How can I declare global variable within Ansible playbook. I have searched in google and found the below solution, but its not working as expected. - hosts: all vars: prod-servers: - x.x.x.x - x.x.x.x - hosts: "{{prod-servers}}" tasks: - name: ping action: ping When I'm trying the above code, it says variable prod-servers is undefined. 回答1: You cannot define a variable accessible on a playbook level (global variable) from within a play. Variable Scopes Ansible has 3 main scopes: Global: this

Run ansible on specific hosts group

你说的曾经没有我的故事 提交于 2019-12-25 07:49:46
问题 I am trying to run ansible with the following command, ansible-playbook provision.yml -l webserver And my hosts file contains the following host groups, [webclient] 172.29.4.75 [webserver] 172.29.4.76 My provision.yml also contains 2 hosts as below, - hosts: webclient user: centos roles: - nginx - nvm - hosts: webserver user: centos roles: - tomcat My issue here is even thought I use "-l webserver" roles specified for webclient also runs in webclient hosts. How can I control it to run only

How to set vars into ansible inventory?

痴心易碎 提交于 2019-12-25 03:45:12
问题 In playbook I'm using variable {{excluded_service}}. I want to run ansible playbook from python and provide this variable. And I can't use external inventory script to provide this variable. I am using to create inventory: hosts = ["127.0.0.1"] inventory=ansible.inventory.Inventory(hosts) but I don't understand where I can add value of variable? My code, that works with external inventory script: import sys import os import stat import json import ansible.playbook import ansible.constants as

Ansible nested loops through inventories plus outer loop of a list

空扰寡人 提交于 2019-12-24 15:09:47
问题 Suppose I have the following playbook: --- - hosts: all gather_facts: False vars: group: 'dev' tasks: - name: Just loop through a group and group_vars debug: msg: 'group is {{group}} target is {{item.0}} port is {{item.1}}' loop: > {{ groups[group] | product(hostvars[groups[group][0]]["ports"]) | list }} How can I change the loop part in case I have the variable named "group" defined as list and not as a single variable? For example: vars: group: - 'dev' - 'int' Thanks and regards. Stef 回答1:

ansible output printing unwanted things. how to format and display only specific data's

谁说我不能喝 提交于 2019-12-24 01:41:31
问题 I am using ansible 2.4 in centos, trying to run the below script in remote servers and getting the output. Here the problem is yum info output is showing with json format also. But i need to display only the output. How to remove the json format. --- - hosts: GeneralServer tasks: - name: Checking the service status shell: systemctl status {{ item }} with_items: - httpd - crond - postfix - sshd register: service - debug: var=service - name: Checking the package info shell : yum info {{ item }}

How to loop through inventory and assign value in Ansible

六眼飞鱼酱① 提交于 2019-12-24 00:55:03
问题 I have a task in my Ansible playbook that I'm wanting iterate over each host in the group that I have and for each host I would like to assign a name from the hostname list that I've created in the vars folder. I'm familiar with looping through inventory already by writing loop: "{{ groups['mygroup'] }}" and I have a list of hostnames I would like to assign each IP in 'mygroup' within the host file. # In tasks file - roles/company/tasks/main.yml - name: change hostname win_hostname: name: "{{

Ansible: Can't set variable for sequential execution

走远了吗. 提交于 2019-12-24 00:37:58
问题 I'm trying to build a playbook with rolling update use case (Sequential Execution) with the serial element. Since I have to use the serial value in multiple places in the playbook, I wanted to use it as a variable which can be used to define somewhere as a group variable. Inventory file [all] webserver1 ansible_host=10.1.1.1 ansible_user=root webserver2 ansible_host=10.1.1.2 ansible_user=root webserver3 ansible_host=10.1.1.3 ansible_user=root dbserver1 ansible_host=10.1.2.1 ansible_user=root

How can I break the with_items loop based on a condition

和自甴很熟 提交于 2019-12-23 20:27:33
问题 I want to break out of the with_items loop based on a condition. That condition for arguments sake is if the stdout of a command is equal to a particular string. Obviously the example below does not work but this is an idea of what I want to do. For example: - name: testing loop shell: "echo {{ item }}" with_items: - "one" - "two" - "three" register: shell_command # registering the shell command and it's attributes when: shell_command.stdout == "two" # break once the stdout of the run shell