ansible-playbook

How to execute a shell script on a remote server using Ansible?

南楼画角 提交于 2019-12-03 02:10:30
问题 I am planning to execute a shell script on a remote server using Ansible playbook. test.sh: touch test.txt Playbook: --- - name: Transfer and execute a script. hosts: server user: test_user sudo: yes tasks: - name: Transfer the script copy: src=test.sh dest=/home/test_user mode=0777 - name: Execute the script local_action: command sudo sh /home/test_user/test.sh When I run the playbook, the transfer successfully occurs but the script is not executed. 回答1: local_action runs the command on the

Ansible: create a user with sudo privileges

拜拜、爱过 提交于 2019-12-03 02:08:11
问题 I have taken over a Ubuntu 14.04 server. It has a user called "deployer" (used with capistrano), and as such, it needs sudo privileges. With this setup, I can log into the server and do stuff like: workstation> ssh deployer@myserver myserver> sudo apt-get install git myserver> exit workstation> I am trying to figure out how to use Ansible (version 2.0.2.0 and python 2.7.3) to create a user called "deployer" and be able to log into the server with that id and then so sudo-ish things like "apt

How do I use remote machine's SSH keys in ansible git module

筅森魡賤 提交于 2019-12-03 01:44:40
I've been trying to get Ansible to provision a remote machine, and I want the remote machine to be set up with its own keys, and have the ability to clone git repositories from Bitbucket. The user is set up, has its own id_rsa.pub, and the key has been registered with bitbucket. But, when I use the Ansible Git module, it looks like the module always tries to use the keys from the machine running the playbook. How do I get the git module to use the id_rsa.pub from the remote machine? The relevant task is this: - name: be sure prom-king has an up-to-date clone of its own repository git: repo:

Override hosts variable of Ansible playbook from the command line

夙愿已清 提交于 2019-12-03 01:02:22
问题 This is a fragment of a playbook that I'm using ( server.yml ): - name: Determine Remote User hosts: web gather_facts: false roles: - { role: remote-user, tags: [remote-user, always] } My hosts file has different groups of servers, e.g. [web] x.x.x.x [droplets] x.x.x.x Now I want to execute ansible-playbook -i hosts/<env> server.yml and override hosts: web from server.yml to run this playbook for [droplets] . Can I just override as a one time off thing, without editing server.yml directly?

Ansible: In a playbook, filter a role by tag(s) without passing at the command-line

拜拜、爱过 提交于 2019-12-03 00:55:38
In Ansible 1.7, I can use --tags from the command-line to only run a subset of that playbooks tasks. But I'm wanting to bake into my playbook to run a set of roles with only tasks that match tags. That is, I don't want to have to pass this in via the command-line since it will be the same every time. At first I thought it was this command, but this does the opposite: tagging tasks with these tags instead of filtering them out based on this. roles: - { role: webserver, port: 5000, tags: [ 'web', 'foo' ] } I can imagine implementing this using conditionals but tags would be a much more elegant

Safely limiting Ansible playbooks to a single machine?

荒凉一梦 提交于 2019-12-03 00:02:07
问题 I'm using Ansible for some simple user management tasks with a small group of computers. Currently, I have my playbooks set to hosts: all and my hosts file is just a single group with all machines listed: # file: hosts [office] imac-1.local imac-2.local imac-3.local I've found myself frequently having to target a single machine. The ansible-playbook command can limit plays like this: ansible-playbook --limit imac-2.local user.yml But that seems kind of fragile, especially for a potentially

How to register a var in either one or another task

时光怂恿深爱的人放手 提交于 2019-12-02 22:36:29
问题 This task collection doesn't work as I hoped it would: - name: Find out whether syslog-ng is installed (yum) tags: syslog_forwarding command: yum -q list installed syslog-ng register: syslog_ng_check failed_when: False changed_when: False when: ansible_pkg_mgr == 'yum' - name: Find out whether syslog-ng is installed (apt) tags: syslog_forwarding command: dpkg -s syslog-ng register: syslog_ng_check failed_when: False changed_when: False when: ansible_pkg_mgr == 'apt' - name: Configure syslog

Ansible: Set variable only if undefined

喜你入骨 提交于 2019-12-02 22:27:38
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 %}" Henrik Pingel It seems like you are taking a wrong approach. Take a look at the Ansible documentation concerning variable precedence . It is a built-in feature of Ansible to use the default variable if the variable is not defined.

How to get current role name in an ansible task

不打扰是莪最后的温柔 提交于 2019-12-02 20:23:12
How can I get the current role name in an ansible task yaml file? I would like to do something like this --- # role/some-role-name/tasks/main.yml - name: Create a directory which is called like the current role name action: file path=/tmp/"{{ role_name }}" mode=0755 state=directory The result of this task should be a directory /tmp/some-role-name on the server jarv As of Ansible 2.2 : {{role_name}} As of Ansible 2.1 : {{role_path|basename}} Older versions: There is no way to do this in the current version of Ansible, here are a couple options that might work for you instead: 1) Use set_fact to

Ansible - On error, exit role and run cleanup

自古美人都是妖i 提交于 2019-12-02 18:52:35
问题 I'm trying to spin up an AWS deployment environment in Ansible, and I want to make it so that if something fails along the way, Ansible tears down everything on AWS that has been spun up so far. I can't figure out how to get Ansible to throw an error within the role For example: <main.yml> - hosts: localhost connection: local roles: - make_ec2_role - make_rds_role - make_s3_role 2. Then I want it to run some code based on that error here. <make_rds_role> - name: "Make it" - rds: params: etc <