ansible-playbook

How to pass variables from one role downstream to other dependency roles with ansible?

喜你入骨 提交于 2019-12-06 21:42:32
问题 I have a generic webserver role that is using another nginx role to spawn new vservers. webserver/meta/main.yml looks like: allow_duplicates: yes dependencies: - role: nginx name: api vserver frontend_port: "{{ frontend_port }}" domain: "{{ api_domain }}" backend_host: 127.0.0.1 - role: nginx name: portal vserver domain: "{{ portal_domain }}" backend_host: 127.0.0.1 The problem is that these variables are supposed to be defined inside the webserver-role/vars/(test|staging).yml Is seems that

Accessing remote_user variable

我们两清 提交于 2019-12-06 18:39:14
问题 This seems to work, but is it fragile? I want the owner and group in the files command to be set to someguy . I'd expect to be able to use {{ remote_user }} but that doesn't work. This is an example playbook showing what I mean. --- - hosts: foobar remote_user: someguy tasks: - name: configure /usr/src/foo file: dest: /usr/src/foo state: directory owner: {{ ansible_ssh_user }} group: {{ ansible_ssh_user }} recurse: yes sudo: yes This doesn't work: --- - hosts: foobar remote_user: someguy

how to read json file using ansible

女生的网名这么多〃 提交于 2019-12-06 17:20:12
问题 I have a json file in the same directory where my ansible script is. Following is the content of json file: { "resources":[ {"name":"package1", "downloadURL":"path-to-file1" }, {"name":"package2", "downloadURL": "path-to-file2"} ] } I am trying to to download these packages using get_url. Following is the approach: --- - hosts: localhost vars: package_dir: "/var/opt/" version_file: "{{lookup('file','/home/shasha/devOps/tests/packageFile.json')}}" tasks: - name: Printing the file. debug: msg="

ansible - check if file exists on *local* machine

跟風遠走 提交于 2019-12-06 16:40:29
问题 I have a situation where I need to check the status of a file on the local machine (the one where I will call ansible-playbook ... ). If a file that is created by the user exists, it needs to be copied over to the remote host(s). If it doesn't exist, then none of the remote hosts need it. I know I've done things like : - name: Check for ~/.blah/config stat: path=/home/ubuntu/.blah/config register: stat_blah_config - name: Do something with blah config shell: ~/do_something_with_config.sh when

Ansible, Juniper CLI commands. Timeout Error?

前提是你 提交于 2019-12-06 10:50:35
I am trying to transfer an automation script I made in Python, to ansible (company request), and I have NEVER worked with ansible before. I have tried the "wait_for:", but I have not gotten that to work either. In the script, I could set dev.timeout=None or whatever I needed. I am finding it hard to figure out where I can do this in ansible. I have tried setting the timeout in the "ansible.cfg" file. But that doesnt work. I can do simple commands, like: cli="show version", or cli="show system firmware". The following is my playbook: - hosts: local roles: - Juniper.junos connection: local

Ansible writing output from multiple task to a single file

心不动则不痛 提交于 2019-12-06 09:54:23
In Ansible, I have written an Yaml playbook that takes list of host name and the executes command for each host. I have registered a variable for these task and at the end of executing a task I append output of each command to a single file. But every time I try to append to my output file, only the last record is getting persisted. --- - hosts: list_of_hosts become_user: some user vars: output: [] tasks: - name: some name command: some command register: output failed_when: "'FAILED' in output" - debug: msg="{{output | to_nice_json}}" - local_action: copy content='{{output | to_nice_json}}'

Ansible - Control commands when need users input when executing

蹲街弑〆低调 提交于 2019-12-06 08:20:54
How can i control user inputs when a command is executing and ask me something for example : sudo apt-get install mariadb-server when you run this command in ubuntu it asked you please enter new password for mysql user root and then again it ask to enter password again for confirmation . how can i pass a variable for example mariadbpass to this command because everytime ansible run this hangs and failed so i have to login to servers and run manually this dpkg --configure -a to enter prompted password and its confirmation. thank you Here is the Solution : add this before installing mariadb 10

Ansible: Can we run an include playbook Asynchronously?

你。 提交于 2019-12-06 08:15:46
问题 I'm interested to learn if Ansible can run an included playbook asynchronously? Basically what I'm trying to do is run a task "Fire and forget, check on it later." When I check on it later I also want to send a slack notification with the result. However I've notice the included playbook for slack notification takes a little longer than expected to complete and hence it holds up the rest of the playbook. What I want is to async the included playbook for slack notification so that the current

How to break `with_lines` cycle in Ansible?

依然范特西╮ 提交于 2019-12-06 04:40:22
I would like to use the following handler with Ansible: - name: force ntp update shell: ntpdate {{item}} with_lines: /etc/ntpd.serverlist But I want it to end execution after the first successful execution (the list contains ntpd servers with which you can attempt to sync. One is enough). How would I do that? That's a very interesting situation you have. I haven't tried this personally, but I wonder if something like this would work: - name: force ntp update shell: ntpdate {{item}} with_lines: /etc/ntpd.serverlist register: ntp_result when: ntp_result is not defined or ntp_result.rc != 0

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

依然范特西╮ 提交于 2019-12-06 03:51:43
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 # based on https://github.com/ansible/ansible/blob/v2.0.0.2-1/lib/ansible/cli/playbook.py pbex =