ansible-playbook

ansible - check if file exists on *local* machine

陌路散爱 提交于 2019-12-04 22:20:30
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: stat_aws_config.stat.exists == true But that will only work if the file exists remotely. Is there a

Ansible: copying one unique file to each server in a group

百般思念 提交于 2019-12-04 18:44:16
I have a series of numbered files to be processed separately by each server. Each split file made using linux split and then xz compressed to save transfer time. split_001 split_002 split_003 ... split_030 How can I push these files out to a group of 30 servers with ansible? It does not matter which server gets which file so long as they each have a single unique file. I had used a bash file but I am looking for a better solution. Hopefully using ansible. Then I plan to run a shell command to run an at command to start the several hours or days of computation. scp -oStrictHostKeyChecking=no bt

iteration using with_items and register

大憨熊 提交于 2019-12-04 18:01:32
问题 Looking for help with a problem I've been struggling with for a few hours. I want to iterate over a list, run a command, register the output for each command and then iterate with debug over each unique registers {{ someregister }}.stdout For example, the following code will spit out "msg": "1" and "msg": "2" --- - hosts: localhost gather_facts: false vars: numbers: - name: "first" int: "1" - name: "second" int: "2" tasks: - name: Register output command: "/bin/echo {{ item.int }}" register:

Ansible cannot make dir /$HOME/.ansible/cp

ε祈祈猫儿з 提交于 2019-12-04 16:52:35
I'm getting a very strange error when I run ansible: GATHERING FACTS *************************************************************** fatal: [i-0f55b6a4] => Could not make dir /$HOME/.ansible/cp: [Errno 13] Permission denied: '/$HOME' TASK: [Task #1] *************************************************************** FATAL: no hosts matched or all hosts have already failed -- aborting PLAY RECAP ******************************************************************** to retry, use: --limit @/home/ubuntu/install.retry i-0f55b6a4 : ok=0 changed=0 unreachable=1 failed=0 Normally, this playbook runs

Running a SELECT Query with an Ansible Task

落爺英雄遲暮 提交于 2019-12-04 16:44:38
问题 In this list of mysql db modules for Ansbile, there's one for creating a db, or creating a user, etc. I would like to run a query against a pre-existing table and use the results of that query to populate an Ansible variable (list of IP addresses, and node type) upon which I would run different tasks, depending on node type. How can that be done in Ansible? 回答1: This is approximately how to do it (but it is untested): - name: Retrieve stuff from mysql command: > mysql --user=alice --password

Cannot get ansible to recognize group variables

一世执手 提交于 2019-12-04 10:08:51
I'm trying to set up environment specific variables in ansible (e.g. production, staging, development). For some reason, ansible is not picking up variables in group_vars/[environment]. I'm using ansible 1.9.1 Here's a stripped down example of what I'm trying to do. Directory structure: . ├── group_vars │ └── staging ├── hosts │ └── staging └── site.yml group_vars/staging: test_var: "this is a test" site.yml: --- - hosts: localhost tasks: - debug: msg="test variable = {{ test_var }}" hosts/staging is an empty file Output of running ansible-playbook -i hosts/staging site.yml : PLAY [localhost]

How to remove user from a specified group in Ansible?

被刻印的时光 ゝ 提交于 2019-12-04 09:18:22
Let's assume user01 has two groups defined: groupA and groupB (in addition to the primary group). I can add the account to groupC (ensure user01 belongs to groupC ) using: - user: name=user01 groups=groupC append=yes How can I remove user01 from groupB (ensure user01 does not belong to groupB ) without specifying all the groups the account should belong to? As far as I can tell, you can't with just the normal user module. However, with some fairly crazy gyrations, you can do it in a playbook. I'm not sure I recommend this though; it was just an interesting exercise. (I did test this and it

How to get variables from ansible result

北城以北 提交于 2019-12-04 08:13:49
I have a shell script whose output is an echo of the following format <variable_1>;<variable_2>;<variable_3> etc I want to use these variables and run a mysql query to update a DB like so mysql -u<user> -p<password> -h<host> -e'insert into test_table values ("variable_1","variable_2","variable_3")' My ansible playbook looks like this. --- - hosts: infoServers sudo: yes gather_facts: no tasks: - name: gather info script: get_hostdata.sh register: result - name: print result local_action: command mysql -uuser -ppassword -h192.168.101.10 ansible_db -e'insert into test_table values ("{{ item[0] }}

How can Ansible loop over a sequence of tasks?

随声附和 提交于 2019-12-04 07:30:14
How can an Ansible playbook loop over a sequence of tasks? I wish to implement a polling loop that executes a task sequence until the task is successful. When it fails, an exception handler will attempt to fix the condition and then the loop will repeat the task sequence. Consider the following imaginary example: - action: - block: - debug: msg='i execute normally' - command: /bin/foo rescue: - debug: msg='I caught an error' - command: /bin/fixfoo always: - debug: msg="this always executes" register: result until: result retries: 5 delay: 10 As of Ansible 2.5, loop is recommended over with

How can I pass variable to ansible playbook in the command line?

做~自己de王妃 提交于 2019-12-04 07:29:32
问题 I'm new to ansible and wonder how to do so as the following didn't work ansible-playbook -i '10.0.0.1,' yada-yada.yml --tags 'loaddata' django_fixtures="tile_colors" Where django_fixtures is my variable. 回答1: Reading the docs I find the section Passing Variables On The Command Line, that give this sample: ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo" Others examples demonstrate how to load from JSON string (≥ 1.2 ) or file (≥ 1.3 ) 回答2: Other answers state how