ansible-2.x

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

Converting output of Python script to dict in Ansible

梦想与她 提交于 2019-12-23 04:48:36
问题 I have a Python script called test.py which is: #!/usr/bin/python a = "A:2\nB:5" print a Now in my Ansible playbook, I am running this script and registering the output to a variable using this task - name: Create variable from the command command: "python ./test.py" register: command_output I want to convert the output to a dictionary Dict in ansible so that in subsequent tasks I can access values like Dict.A or Dict.B . I tried all the options present here but none are working for me. While

Ansible loop using multi-ter group_vars

穿精又带淫゛_ 提交于 2019-12-23 02:54:15
问题 I'm trying to dynamically create templates in ansible using group_vars but cannot seem to get the nested loop working. In group_vars, I have my_environment: serv1: foo: 2 bar: 3 baz: 3 serv2: foo: 1 I'm trying to create the following structure: /serv1/foo1 /serv1/foo2 /serv1/bar1 /serv1/bar2 /serv1/bar3 /serv1/baz1 /serv1/baz2 /serv1/baz3 /serv2/foo1 Once the above is created, I want to put a template file into each directory so the final result would be: /serv1/bar1/template and /serv2/foo1

How to add a callback plugin to a PlaybookExecutor in Ansible 2

爷,独闯天下 提交于 2019-12-22 09:27:12
问题 How to specify a callback when calling ansible via its API? I have a callback plugin database_write.py for ansible 2.0.0.2 that logs into a database when this is run: ansible-playbook -i inventory.txt playbook.yml # callback is fired ok This works ok because in my $PWD i have ansible.cfg with this line: callback_plugins = ./src/callback Now I'm trying to make ansible to execute my playbook and my callback using the python API. I've basically copied what the ansible-playbook cli tool does #

How to use ansible 'expect' module for multiple different responses?

末鹿安然 提交于 2019-12-21 03:29:26
问题 Here I am trying to test my bash script where it is prompting four times. #!/bin/bash date >/opt/prompt.txt read -p "enter one: " one echo $one echo $one >>/opt/prompt.txt read -p "enter two: " two echo $two echo $two >>/opt/prompt.txt read -p "enter three: " three echo $three echo $three >>/opt/prompt.txt read -p "enter password: " password echo $password echo $password >>/opt/prompt.txt for this script I wrote the code below, and it is working fine - hosts: "{{ hosts }}" tasks: - name: Test

ansible returns with "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6))

被刻印的时光 ゝ 提交于 2019-12-20 05:53:56
问题 I am running myserver in ubuntu: + sudo cat /etc/os-release NAME="Ubuntu" VERSION="16.04.6 LTS (Xenial Xerus)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 16.04.6 LTS" VERSION_ID="16.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" VERSION_CODENAME=xenial UBUNTU_CODENAME=xenial I use ansible and when I run it I get the following error: fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the

accessing inventory host variable in ansible playbook

廉价感情. 提交于 2019-12-20 03:01:04
问题 I am using ansible 2.1. I have the following inventory host file and a role being called by a play that needs access to the host file variable. Any thoughts on how to access it (currently getting an error): host file [test1] test-1 ansible_ssh_host=abc.def.ghi.jkl ansible_ssh_port=1212 [test2] test2-1 ansible_ssh_host=abc.def.ghi.mno ansible_ssh_port=1212 [test3] test3-1 ansible_ssh_host=abc.def.ghi.pqr ansible_ssh_port=1212 test3-2 ansible_ssh_host=abc.def.ghi.stu ansible_ssh_port=1212 [all

Ansible not detecting Role default variables in its handler

浪子不回头ぞ 提交于 2019-12-20 02:11:03
问题 Does ansible pass Role Default variables to the Handlers within the same Role? Here's a minimal excerpt of the playbook that has the issue: Role hierarchy - playbook.yml - roles/ - gunicorn/ - defaults/ - main.yml - handlers/ - main.yml - code-checkout/ - tasks/ - main.yml Here's the file contents gunicorn/defaults/main.yml --- gu_log: "/tmp/gunicorn.log" gunicorn/handlers/main.yml --- - name: Clear Gunicorn Log shell: rm {{ gu_log }} finalize/tasks/main.yml --- - name: Test Handlers shell:

Ansible: How to add variables to “command” or “shell”

╄→尐↘猪︶ㄣ 提交于 2019-12-19 17:11:23
问题 Is it possible to use variables on command or shell modules? I have the following code, and I would like to use variable file to provide some configurations: I would like to read the Hadoop version from my variables file. On other modules of ansible I could use {{ansible_version}} , but with command or shell it doesn't works. - name: start ZooKeeper HA command: hadoop-2.7.1/bin/hdfs zkfc -formatZK -nonInteractive - name: start zkfc shell: hadoop-2.7.1/sbin/hadoop-daemon.sh start zkfc I would

How to convert a dictionary of dictionaries into a list of dictionaries in a Ansible vars file?

泄露秘密 提交于 2019-12-19 05:45:10
问题 Within an Ansible vars file, I want to convert a dict of dicts into a list of dicts that I can pass to an external role from Ansible Galaxy. Input: postgres_users: dc1: name: user_dc1 password: pass_dc1 dc2: name: user_dc2 password: pass_dc2 dc3: name: user_dc3 password: pass_dc3 Desired output: postgres_users: - name: user_dc1 password: pass_dc1 - name: user_dc2 password: pass_dc2 - name: user_dc3 password: pass_dc3 Is there a simple way to do this within an Ansible vars file? 回答1: {{