ansible-playbook

Copy local file if exists, using ansible

老子叫甜甜 提交于 2020-01-01 03:55:06
问题 I'm working in a project, and we use ansible to create a deploy a cluster of servers. One of the tasks that I've to implement, is to copy a local file to the remote host, only if that file exists locally. Now I'm trying to solve this problem using this - hosts: 127.0.0.1 connection: local tasks: - name: copy local filetocopy.zip to remote if exists - shell: if [[ -f "../filetocopy.zip" ]]; then /bin/true; else /bin/false; fi; register: result - copy: src=../filetocopy.zip dest=/tmp/filetocopy

Ansible: Set variable only if undefined

喜欢而已 提交于 2019-12-31 12:56:10
问题 I would like to set an ansible variable to some default value but only if the variable is undefined. Otherwise I would like to keep it unchanged. I tried these two approaches and both of them produce recursive loop: namespace: "{{namespace|default(default_namespace)}}" namespace: "{% if namespace is defined %}{{namespace}}{% else %}{{default_namespace}}{% endif %}" 回答1: It seems like you are taking a wrong approach. Take a look at the Ansible documentation concerning variable precedence. It

How to continue execution on failed task after fixing error in playbook?

本秂侑毒 提交于 2019-12-31 08:33:22
问题 When writing and debugging Ansible playbooks, typical workflow is as follows: ansible-playbook ./main.yaml Playbook fails on some task Fix this task and repeat line 1, waiting for all previous tasks to execute again. Which takes a lot of time Ideally, i'd like to resume execution on failed task, having inventory and all facts collected by previous tasks. Is it even possible? How to make playbook writing/debugging faster? 回答1: Take a look at http://docs.ansible.com/playbooks_startnstep.html.

Get the pid of a running playbook for use within the playbook

笑着哭i 提交于 2019-12-30 17:22:43
问题 When we run a playbook, with verbose output enabled, in the ansible logs we can see something like this: 2016-02-03 12:51:58,235 p=4105 u=root | PLAY RECAP I guess that the p=4105 is the pid of the playbook when it ran. Is there a way to get this pid inside the playbook during its runtime (as a variable for example)? 回答1: This sounds a little like an XY problem, but one option may be to spawn a shell with the shell command and then ask for the parent PID: - name: get pid of playbook shell: |

ansible get aws ebs volume id which already exist

半城伤御伤魂 提交于 2019-12-30 10:43:16
问题 I'm trying to get the aws volume id which already exist and attached to ec2 instance using ansible. I have a lookup task using the ec2_remote_facts module that get details of the ec2 instance including the volume id details the task: - name: lookup ec2 virtual machines ec2_remote_facts: aws_access_key: "{{aws_access_key}}" aws_secret_key: "{{aws_secret_key}}" region: "{{ec2_region}}" filters: instance-state-name: running "tag:Name": "{{server_name}}" "tag:Environment": "{{environment_type}}"

Is there anyway to run multiple Ansible playbooks as multiple users more efficiently?

梦想与她 提交于 2019-12-30 06:58:34
问题 Currently my playbook structure is like this: ~/test_ansible_roles ❯❯❯ tree . . ├── checkout_sources │ └── tasks │ └── main.yml ├── install_dependencies │ └── tasks │ └── main.yml ├── make_dirs │ └── tasks │ └── main.yml ├── setup_machine.yml One of the roles that I have is to install dependencies on my box, so for this I need sudo . Because of that all of my other tasks I need to include the stanza: become: yes become_user: my_username Is there a better way to do this ? 回答1: You can set the

Is there anyway to run multiple Ansible playbooks as multiple users more efficiently?

我的未来我决定 提交于 2019-12-30 06:58:17
问题 Currently my playbook structure is like this: ~/test_ansible_roles ❯❯❯ tree . . ├── checkout_sources │ └── tasks │ └── main.yml ├── install_dependencies │ └── tasks │ └── main.yml ├── make_dirs │ └── tasks │ └── main.yml ├── setup_machine.yml One of the roles that I have is to install dependencies on my box, so for this I need sudo . Because of that all of my other tasks I need to include the stanza: become: yes become_user: my_username Is there a better way to do this ? 回答1: You can set the

using regex in jinja 2 for ansible playbooks

大兔子大兔子 提交于 2019-12-29 05:56:32
问题 HI i am new to jinja2 and trying to use regular expression as shown below {% if ansible_hostname == 'uat' %} {% set server = 'thinkingmonster.com' %} {% else %} {% set server = 'define yourself' %} {% endif %} {% if {{ server }} match('*thinking*') %} {% set ssl_certificate = 'akash' %} {% elif {{ server }} match( '*sleeping*')%} {% set ssl_certificate = 'akashthakur' %} {% endif %} based on the value of "server" i would like to evaluate as which certificates to use. ie if domain contains

Is there with_fileglob that works remotely in ansible?

为君一笑 提交于 2019-12-28 16:31:58
问题 Is there with_fileglob that works remotely in ansible? Mainly I do want to use something similar with the with_fileglob but that will glob the files on the remote/target machine, not on the one that is running ansible. 回答1: All of the with_* looping mechanisms are local lookups unfortunately so there's no really clean way to do this in Ansible. Remote operations by design must be enclosed in tasks as it would need to deal with connections and inventory etc. What you can do is generate your

ansible wget then exec scripts => get_url equivalent

不羁岁月 提交于 2019-12-28 13:47:53
问题 I always wonder what is the good way to replace the following shell tasks using the "ansible way" (with get_url , etc.): - name: Install oh-my-zsh shell: wget -qO - https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash - or - name: Install nodesource repo shell: curl -sL https://deb.nodesource.com/setup_5.x | bash - 回答1: This worked for me: - name: Download zsh installer get_url: url=https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh dest=/tmp/zsh